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

Posted

技术标签:

【中文标题】为啥将具有运行时范围的库添加到 debugCompileClasspath?【英文标题】:Why libs with runtime scope are added to debugCompileClasspath?为什么将具有运行时范围的库添加到 debugCompileClasspath? 【发布时间】:2018-11-08 15:38:11 【问题描述】:

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

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

为什么com.github.kolyall:utils:1.0.4 lib 在CompileClasspath 中获得runtime 范围的com.android.support:appcompat-v7 lib(和其他)?如果它们的作用域应该是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>

【问题讨论】:

【参考方案1】:

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库版本后

现在将 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

【讨论】:

现在为什么 lib &lt;dependency&gt;&lt;groupId&gt;net.danlew&lt;/groupId&gt;&lt;artifactId&gt;android.joda&lt;/artifactId&gt;&lt;version&gt;2.8.2&lt;/version&gt;&lt;scope&gt;compile&lt;/scope&gt;&lt;/dependency&gt; compile 范围不包括在内?

以上是关于为啥将具有运行时范围的库添加到 debugCompileClasspath?的主要内容,如果未能解决你的问题,请参考以下文章

ad16为啥原理图和pcb图自动改编号

为啥将刚体添加到门并且在运行游戏时自动打开门时是不是启用了运动学?

为啥我不应该发布用 Ivy 编译的库?

为啥将 OFFSET 添加到 clickhouse 查询会增加执行时间?

在运行时将一个游戏对象组件添加到另一个具有值的游戏对象中

无法将 ReactNativeNavigation.xcodeproj 添加到 Xcode 中的库