类描述符'Landroid/support/customtabs/ICustomTabsCallback /;' 不能用dex格式表示

Edi*_*sar 9 android android-studio build.gradle android-gradle-plugin

我将我的android工作室更新为版本3.3 canary 13(截至本文撰写时的最新版本).我被提示更新我的项目的gradle版本,并将其更新为版本3.3.0-alpha13

classpath 'com.android.tools.build:gradle:3.3.0-alpha13'

现在,当我尝试运行我的项目时,它失败并出现错误

Error: Class descriptor 'Landroid/support/customtabs/ICustomTabsCallback/;' cannot be represented in dex format.

我试图使缓存无效,清理并重建项目但没有任何效果.下面是我的应用程序的build.gradle

dependencies {

implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha3', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'

}
Run Code Online (Sandbox Code Playgroud)

Edi*_*sar 5

我决定尝试使用./gradlew build --stacktrace命令,并看到该库ICustomTabsCallback正在使用该类androidx.browser:browser:1.0.0-rc01

> Transform browser.aar (androidx.browser:browser:1.0.0-rc01) with DexingTransform
AGPBI: {"kind":"error","text":"Class descriptor \u0027Landroid/support/customtabs/ICustomTabsCallback/;\u0027 cannot be represented in dex format.","sources":[{}],"tool":"D8"}

> Task :app:mergeExtDexDebug FAILED
AGPBI: {"kind":"error","text":"Class descriptor \u0027Landroid/support/customtabs/ICustomTabsCallback/;\u0027 cannot be represented in dex format.","sources":[{}],"tool":"D8"}

FAILURE: Build failed with an exception.
Run Code Online (Sandbox Code Playgroud)

然后,我使用./gradlew app:dependencies命令查看依赖项中是否存在冲突,并且发现了错误。

 +--- androidx.appcompat:appcompat:1.0.0-rc01 -> 1.0.0 (*)
|    \--- androidx.browser:browser:1.0.0-rc01
|         +--- androidx.core:core:1.0.0-rc01 -> 1.0.0 (*)
|         +--- androidx.annotation:annotation:1.0.0-rc01 -> 1.0.0
|         +--- androidx.interpolator:interpolator:1.0.0-rc01 -> 1.0.0 (*)
|         +--- androidx.collection:collection:1.0.0-rc01 -> 1.0.0 (*)
|         \--- androidx.legacy:legacy-support-core-ui:1.0.0-rc01 -> 1.0.0 (*)
Run Code Online (Sandbox Code Playgroud)

上面的摘录显示了一些debugCompileClasspath配置依赖性。我们可以看到,androidx.appcompat:appcompat包含androidx.browser:browser作为传递依赖。

androidx.appcompat:appcompat:1.0.0-rc01 -> 1.0.0表示1.0.0将使用version 1.0.0-rc01而不是version,但情况并非如此androidx.browser:browser。版本1.0.0-rc01将代替版本1.0.0

为了解决此错误,我通过在应用程序的代码中添加以下代码块来删除传递依赖项 build.gradle

configurations {
    compile.exclude group: 'androidx.browser', module: 'browser'
}
Run Code Online (Sandbox Code Playgroud)

所以我的应用程序的build.gradle看起来像这样

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android {
    //....
}

configurations {
    compile.exclude group: 'androidx.browser', module: 'browser'
}

dependencies {
// ....
}
Run Code Online (Sandbox Code Playgroud)

之后,我只是同步,清理并重建了我的项目。

更新

如果答案不能解决您的问题,则另一个选择是使用android studio稳定版本(根据本文撰写,为3.2.1)和gradle 3.2.1 classpath 'com.android.tools.build:gradle:3.2.1'

  • 如果您实际上正在使用customtab,那么这不是有效的解决方案。 (2认同)
  • 就像@AdamHurwitz所说的那样,这不是一个有效的答案,因为您要删除一个传递性依赖关系,这可能会破坏您所依赖的库。这是AGP 3.3.0-alpha13的错误,请参阅https://issuetracker.google.com/issues/117145286 (2认同)