安卓依赖配置

3
我一直在阅读Gradle文档,以了解Gradle如何管理Android项目中的依赖关系。
最终,我了解到Java Library插件使用以下配置来决定如何构建和运行一个项目。
- api - implementation - compileOnly - runtimeOnly
然而,我试图检查这些配置之间的差异,使用像retrofit、glide或okHttp这样的Android库,但是我没有找到其中的区别。例如,假设我想尝试OkHttp。
使用API
api "com.squareup.okhttp3:okhttp:4.6.0"

使用实现
implementation "com.squareup.okhttp3:okhttp:4.6.0"

我看不出在Project -> External Libraries -> com.squareup.okhttp3或使用./gradlew app:androidDependencies有任何区别。我不确定这些配置是否仅在多模块项目中有用,在这种情况下,检查差异更容易(至少是api vs implementation)。如果我深入研究OkHttp的pom.xml,我不知道使用哪个配置:apiimplementationcompileOnlyruntimeOnly
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>parent</artifactId>
    <version>3.14.7</version>
  </parent>

  <artifactId>okhttp</artifactId>
  <name>OkHttp</name>

  <dependencies>
    <dependency>
      <groupId>com.squareup.okio</groupId>
      <artifactId>okio</artifactId>
    </dependency>
    <dependency>
      <groupId>org.conscrypt</groupId>
      <artifactId>conscrypt-openjdk-uber</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.robolectric</groupId>
      <artifactId>android-all</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>jsr305</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>animal-sniffer-annotations</artifactId>
      <version>1.17</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>templating-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>filter-sources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>3.0.1</version>
        <configuration>
          <excludePackageNames>okhttp3.internal:okhttp3.internal.*</excludePackageNames>
          <links>
            <link>http://square.github.io/okio/</link>
          </links>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <archive>
            <manifestEntries>
              <Automatic-Module-Name>okhttp3</Automatic-Module-Name>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

问题

  1. 如何使用远程库而不是子模块来检查配置之间的差异。
  2. pom.xml 如何知道其配置依赖关系?
  3. 也许 OkHttp 不是最好的例子,有没有更好的例子来解释这些问题?

有人能帮帮我吗?如果需要,我可以提供更多细节。或者我可以付费获得支持哈哈。

1个回答

1

您的Gradle项目有Okhttp依赖。

Okhttp是一个Maven项目。

Gradle和Maven都是构建工具,它们本质上做相同的事情,pom.xml是Maven等效于build.gradle文件。

如果您查看OKhttp的pom.xml,您可以看到如下的依赖:

<dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>animal-sniffer-annotations</artifactId>
    <version>1.17</version> 
    <scope>provided</scope>
</dependency>

<scope> 是 Maven 中与 Gradle 配置(例如 implementationapi 等)相对应的概念。

请查看 this 以进行比较:

maven - gradle
compile - compile
provided - compileOnly, testCompileOnly (only gradle)
system (maven only, local JAR)
runtime - runtime
test - testCompile, testRuntime

查找scopes的官方文档可以在这里找到。

远程库是您只需导入项目即可使用的库。它上传到像jcenter或maven central一样的存储库中。

子模块是项目中也位于项目中的一部分。如果编译父项目,子模块也会被编译。


如果我漏掉了什么,请随意评论。在那种情况下,我会尝试编辑我的答案。


非常棒的答案,我刚刚阅读了您提供的链接,它引发了更多的问题。在Maven中没有与apiimplementation等价的内容,因此两者都应被视为已弃用的compile,是吗? - Ricardo
谢谢您提供如此有用的答案,我还有更多问题,但请告诉我您是否希望我创建一个新问题,或者如果您感到舒适,继续在这里更新我的问题。 - Ricardo
通常鼓励创建新问题,如果您有的话。如果一个问题中包含多个问题,甚至会成为关闭原因(“需要更多关注”)。 - dan1st
我认为Maven总是解析传递依赖项(因此两者都应该是“编译”),除非它们被声明为“可选”。 - dan1st
如果我的回答符合您的需求,我将非常感谢您授予我悬赏奖励。 - dan1st
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接