3

I've been reading the Gradle docs to learn more about how Gradle manage the dependencies in an Android project.

Finally I understand The Java Library Plugin decide how to build and run a project using the following configurations.

  • api
  • implementation
  • compileOnly
  • runtimeOnly

However I'm trying to check the differences of those configurations using android libraries as retrofit, glide or okHttp and I'm not able to find one. For example, let's say I want to try OkHttp.

Using API

api "com.squareup.okhttp3:okhttp:4.6.0"

Using implementation

implementation "com.squareup.okhttp3:okhttp:4.6.0"

I don't see any difference in Project -> External Libraries -> com.squareup.okhttp3 or using ./gradlew app:androidDependencies

I'm not sure if this configurations are only useful in a multimodule project, where its easier check the differences (at least api vs implementation).

If I go deeper in the OkHttp pom.xml I don't know which configuration is used: api, implementation, compileOnly, runtimeOnly

<?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>

Questions

  1. How can check the difference between configurations using a remote library instead of submodules.
  2. How pom.xml knows about its configurations dependencies?
  3. Maybe OkHttp is not the best example, is there any better to explain this questions?

Could someone give me a hand? I can provide more details if needed. Or I can pay for support haha.

Ricardo
  • 7,921
  • 14
  • 64
  • 111

1 Answers1

1

Your gradle project has the dependency Okhttp.

Okhttp is a maven project.

Gradle and maven are both build tools and they essentially do the same thing, the pom.xml is the maven equivalent to the build.gradle file.

If you look at the pom.xml from OKhttp, you can see dependencies like this:

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

The <scope> is the maven equivalent to the gradle configuration (e.g. implementation, api,...).

Take a look at this in order to compare them:

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

The official documentation of scopes can be found here.

A remote library is a library that you just import in your project. It is uploaded to a repository like jcenter or maven central.

A submodule is a part of the project that is also located in the project. If the parent project is e.g. compiled, the submodule will be compiled too.


Feel free to comment if I forgot anything. I will try to edit my answer in that case.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Amazing answer, I've just read the link that you provided and it raise more questions to me. There isn't an equivalent to `api` and `implementation` in Maven hence both should be consider as the deprecated `compile`, isn't it? – Ricardo May 09 '20 at 09:39
  • Thanks you for such a useful answer, I have more questions but tell me if you prefer I create a new questions or if you feel comfortable continue here and updating my question. – Ricardo May 09 '20 at 10:01
  • It is generally encouraged to create new questions if you have them. It is even a close reason (`Needs more focus`) that there are multiple questions in one. – dan1st May 09 '20 at 10:46
  • I think maven does always resolve transitive dependencies(so both would be `compile`), except they are declared `optional`. – dan1st May 09 '20 at 10:52
  • I create a new question here: https://stackoverflow.com/questions/61696863/gradle-compileonly-and-runtimeonly – Ricardo May 09 '20 at 13:06
  • I would appreciate you awarding my answer with the bounty if it is what you looked for. – dan1st May 09 '20 at 13:17
  • done. I have created another question here https://stackoverflow.com/questions/61699706 – Ricardo May 09 '20 at 16:01