为什么将具有运行时范围的库添加到 debugCompileClasspath?

Why libs with runtime scope are added to debugCompileClasspath?

如果我将我的库添加到项目中,并且 运行 命令:

> gradlew -q app:dependencies --configuration debugCompileClasspath > app_dependencies_compile.txt

为什么 com.github.kolyall:utils:1.0.4 库在 CompileClasspath 中获得具有 runtime 作用域的 com.android.support:appcompat-v7 库(和其他库)?如果它们的范围应该是 runtime,为什么要将它们添加到 CompileClasspath

输出为:

\--- com.github.kolyall:utils:1.0.4
     +--- net.danlew:android.joda:2.8.2
     |    \--- joda-time:joda-time:2.8.2
     +--- com.android.support:appcompat-v7:28.0.0-alpha3 -> 28.0.0 (*)
     +--- com.android.support:design:28.0.0-alpha3
     |    +--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |    +--- com.android.support:support-compat:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:support-core-ui:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:support-core-utils:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:support-fragment:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:transition:28.0.0-alpha3
     |    |    +--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |    |    \--- com.android.support:support-compat:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:appcompat-v7:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:cardview-v7:28.0.0-alpha3
     |    |    \--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |    \--- com.android.support:recyclerview-v7:28.0.0-alpha3
     |         +--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |         +--- com.android.support:support-compat:28.0.0-alpha3 -> 28.0.0 (*)
     |         \--- com.android.support:support-core-ui:28.0.0-alpha3 -> 28.0.0 (*)
     +--- io.reactivex:rxjava:1.3.0
     +--- io.reactivex:rxandroid:1.2.1
     \--- javax.annotation:jsr250-api:1.0

完整输出为:

Executing tasks: [dependencies, --configuration, debugCompileClasspath]

:app:dependencies

------------------------------------------------------------
Project :app
------------------------------------------------------------

debugCompileClasspath - Resolved configuration for compilation for variant: debug
+--- com.android.support:appcompat-v7:28.0.0
|    +--- com.android.support:support-annotations:28.0.0
|    +--- com.android.support:support-compat:28.0.0
|    |    +--- com.android.support:support-annotations:28.0.0
|    |    +--- com.android.support:collections:28.0.0
|    |    |    \--- com.android.support:support-annotations:28.0.0
|    |    +--- android.arch.lifecycle:runtime:1.1.1
|    |    |    +--- android.arch.lifecycle:common:1.1.1
|    |    |    |    \--- com.android.support:support-annotations:26.1.0 -> 28.0.0
|    |    |    +--- android.arch.core:common:1.1.1
|    |    |    |    \--- com.android.support:support-annotations:26.1.0 -> 28.0.0
|    |    |    \--- com.android.support:support-annotations:26.1.0 -> 28.0.0
|    |    \--- com.android.support:versionedparcelable:28.0.0
|    |         +--- com.android.support:support-annotations:28.0.0
|    |         \--- com.android.support:collections:28.0.0 (*)
|    +--- com.android.support:collections:28.0.0 (*)
|    +--- com.android.support:cursoradapter:28.0.0
|    |    \--- com.android.support:support-annotations:28.0.0
|    +--- com.android.support:support-core-utils:28.0.0
|    |    +--- com.android.support:support-annotations:28.0.0
|    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    +--- com.android.support:documentfile:28.0.0
|    |    |    \--- com.android.support:support-annotations:28.0.0
|    |    +--- com.android.support:loader:28.0.0
|    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    +--- android.arch.lifecycle:livedata:1.1.1
|    |    |    |    +--- android.arch.core:runtime:1.1.1
|    |    |    |    |    +--- com.android.support:support-annotations:26.1.0 -> 28.0.0
|    |    |    |    |    \--- android.arch.core:common:1.1.1 (*)
|    |    |    |    +--- android.arch.lifecycle:livedata-core:1.1.1
|    |    |    |    |    +--- android.arch.lifecycle:common:1.1.1 (*)
|    |    |    |    |    +--- android.arch.core:common:1.1.1 (*)
|    |    |    |    |    \--- android.arch.core:runtime:1.1.1 (*)
|    |    |    |    \--- android.arch.core:common:1.1.1 (*)
|    |    |    \--- android.arch.lifecycle:viewmodel:1.1.1
|    |    |         \--- com.android.support:support-annotations:26.1.0 -> 28.0.0
|    |    +--- com.android.support:localbroadcastmanager:28.0.0
|    |    |    \--- com.android.support:support-annotations:28.0.0
|    |    \--- com.android.support:print:28.0.0
|    |         \--- com.android.support:support-annotations:28.0.0
|    +--- com.android.support:support-fragment:28.0.0
|    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    +--- com.android.support:support-core-ui:28.0.0
|    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    +--- com.android.support:support-core-utils:28.0.0 (*)
|    |    |    +--- com.android.support:customview:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    \--- com.android.support:support-compat:28.0.0 (*)
|    |    |    +--- com.android.support:viewpager:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    |    \--- com.android.support:customview:28.0.0 (*)
|    |    |    +--- com.android.support:coordinatorlayout:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    |    \--- com.android.support:customview:28.0.0 (*)
|    |    |    +--- com.android.support:drawerlayout:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    |    \--- com.android.support:customview:28.0.0 (*)
|    |    |    +--- com.android.support:slidingpanelayout:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    |    \--- com.android.support:customview:28.0.0 (*)
|    |    |    +--- com.android.support:interpolator:28.0.0
|    |    |    |    \--- com.android.support:support-annotations:28.0.0
|    |    |    +--- com.android.support:swiperefreshlayout:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    +--- com.android.support:support-compat:28.0.0 (*)
|    |    |    |    \--- com.android.support:interpolator:28.0.0 (*)
|    |    |    +--- com.android.support:asynclayoutinflater:28.0.0
|    |    |    |    +--- com.android.support:support-annotations:28.0.0
|    |    |    |    \--- com.android.support:support-compat:28.0.0 (*)
|    |    |    \--- com.android.support:cursoradapter:28.0.0 (*)
|    |    +--- com.android.support:support-core-utils:28.0.0 (*)
|    |    +--- com.android.support:support-annotations:28.0.0
|    |    +--- com.android.support:loader:28.0.0 (*)
|    |    \--- android.arch.lifecycle:viewmodel:1.1.1 (*)
|    +--- com.android.support:support-vector-drawable:28.0.0
|    |    +--- com.android.support:support-annotations:28.0.0
|    |    \--- com.android.support:support-compat:28.0.0 (*)
|    \--- com.android.support:animated-vector-drawable:28.0.0
|         +--- com.android.support:support-vector-drawable:28.0.0 (*)
|         \--- com.android.support:support-core-ui:28.0.0 (*)
+--- com.android.support.constraint:constraint-layout:1.1.3
|    \--- com.android.support.constraint:constraint-layout-solver:1.1.3
\--- com.github.kolyall:utils:1.0.4
     +--- net.danlew:android.joda:2.8.2
     |    \--- joda-time:joda-time:2.8.2
     +--- com.android.support:appcompat-v7:28.0.0-alpha3 -> 28.0.0 (*)
     +--- com.android.support:design:28.0.0-alpha3
     |    +--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |    +--- com.android.support:support-compat:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:support-core-ui:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:support-core-utils:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:support-fragment:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:transition:28.0.0-alpha3
     |    |    +--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |    |    \--- com.android.support:support-compat:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:appcompat-v7:28.0.0-alpha3 -> 28.0.0 (*)
     |    +--- com.android.support:cardview-v7:28.0.0-alpha3
     |    |    \--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |    \--- com.android.support:recyclerview-v7:28.0.0-alpha3
     |         +--- com.android.support:support-annotations:28.0.0-alpha3 -> 28.0.0
     |         +--- com.android.support:support-compat:28.0.0-alpha3 -> 28.0.0 (*)
     |         \--- com.android.support:support-core-ui:28.0.0-alpha3 -> 28.0.0 (*)
     +--- io.reactivex:rxjava:1.3.0
     +--- io.reactivex:rxandroid:1.2.1
     \--- javax.annotation:jsr250-api:1.0

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

app.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'

    implementation 'com.github.kolyall:utils:1.0.4'
}

这是*.pom文件from my lib:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.github.kolyall</groupId>
  <artifactId>utils</artifactId>
  <version>1.0.4</version>
  <packaging>aar</packaging>
  <name>utils</name>
  <description>A wrapper for utils on Android</description>
  <url>https://github.com/Kolyall/BaseUtils</url>
  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <id>nickunuchek</id>
      <name>Nick Unuchek</name>
      <email>kolyall@tut.by</email>
    </developer>
  </developers>
  <scm>
    <connection>https://github.com/Kolyall/BaseUtils.git</connection>
    <developerConnection>https://github.com/Kolyall/BaseUtils.git</developerConnection>
    <url>https://github.com/Kolyall/BaseUtils</url>
  </scm>
  <dependencies>
    <dependency>
      <groupId>net.danlew</groupId>
      <artifactId>android.joda</artifactId>
      <version>2.8.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>appcompat-v7</artifactId>
      <version>28.0.0-alpha3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>design</artifactId>
      <version>28.0.0-alpha3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.reactivex</groupId>
      <artifactId>rxjava</artifactId>
      <version>1.3.0</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.reactivex</groupId>
      <artifactId>rxandroid</artifactId>
      <version>1.2.1</version>
      <scope>runtime</scope>
      <exclusions>
        <exclusion>
          <artifactId>rxjava</artifactId>
          <groupId>io.reactivex</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>jsr250-api</artifactId>
      <version>1.0</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

之前 Gradle 5.0 gradle 不支持 IMPROVED_POM_SUPPORT,因此它默认将所有传递依赖项视为 compile。 (不支持非 jar 包装。)

Related Gradle Bug

这已在 Gradle 5.0 中修复,其中 IMPROVED_POM_SUPPORT 已成为默认行为。

虽然这不是强制性的,但告诉 gradle 所提供的依赖项是 android 归档使用

是一个好习惯
implementation 'com.github.kolyall:utils:1.0.4@aar'{
    transitive=true
}

同时在 gradle 中声明依赖项。注意@aar在library

的版本之后

现在 gradle 升级到 5.0 或更高版本后,

compileClasspath 输出是

+--- com.google.code.gson:gson:2.8.5
+--- org.apache.commons:commons-lang3:3.8.1
+--- io.reactivex:rxjava:1.1.0
\--- com.github.kolyall:utils:1.0.4
     \--- net.danlew:android.joda:2.8.2
          \--- joda-time:joda-time:2.8.2