android-jetifier: "无法解析所有工件";新项目上的“改造工件失败”

Posted

技术标签:

【中文标题】android-jetifier: "无法解析所有工件";新项目上的“改造工件失败”【英文标题】:android-jetifier: "Could not resolve all artifacts"; "Failed to transform artifact" on new project 【发布时间】:2020-01-16 13:55:49 【问题描述】:

我正在尝试构建一个相对较新的项目。 Gradle 同步正常,但每当我尝试构建时,都会收到如下错误:

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
> Could not resolve all artifacts for configuration ':app:debugCompileClasspath'.
   > Failed to transform artifact 'common.jar (com.android.tools:common:26.4.0)' to match attributes artifactType=android-classes, org.gradle.usage=java-runtime-jars.
      > Execution failed for JetifyTransform: C:\Users\Dave\.gradle\caches\modules-2\files-2.1\com.android.tools\common\26.4.0\539939e284fba9fe343b890a6e21c9333767c886\common-26.4.0.jar.
         > Failed to transform 'C:\Users\Dave\.gradle\caches\modules-2\files-2.1\com.android.tools\common\26.4.0\539939e284fba9fe343b890a6e21c9333767c886\common-26.4.0.jar' using Jetifier. Reason: The given artifact contains a string literal with a package reference 'android.support.design.widget' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.. (Run with --stacktrace for more details.)
   > Failed to transform artifact 'bundletool.jar (com.android.tools.build:bundletool:0.7.2)' to match attributes artifactType=android-classes, org.gradle.usage=java-runtime-jars.
      > Execution failed for JetifyTransform: C:\Users\Dave\.gradle\caches\modules-2\files-2.1\com.android.tools.build\bundletool\0.7.2\31f71b66edcbe41de0268e14c961a7799a03d42a\bundletool-0.7.2.jar.
         > Failed to transform 'C:\Users\Dave\.gradle\caches\modules-2\files-2.1\com.android.tools.build\bundletool\0.7.2\31f71b66edcbe41de0268e14c961a7799a03d42a\bundletool-0.7.2.jar' using Jetifier. Reason: The given artifact contains a string literal with a package reference 'android.support.design.widget' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.. (Run with --stacktrace for more details.)

我一周前才开始做这个项目,一直在使用 Android Studio 的 stable 分支,并使用 Gradle 插件 3.5。那么,有人知道如何解决这个问题吗?

我的尝试:

在 gradle.properties 中禁用 Jetifier 给我留下了有关新旧数据绑定依赖项的错误。 回滚到 Gradle 插件 3.4.2 时出现与上述相同的错误。 将android.jetifier.blacklist = pass 添加到 gradle.properties 给我留下了与上述相同的错误。 将android.databinding.enableV2=true 添加到 gradle.properties 给我留下了与上述相同的错误。 在 Android Studio 中运行“迁移到 AndroidX”重构实际上给我留下了这个奇怪的重构预览: 从我的应用级 build.gradle 中删除 apply plugin: 'kotlin-kapt' 后,我遇到了与上述相同的错误。 清理和重建不起作用。 无效并重新启动不起作用。

编辑 1: Gradle 依赖项,对于好奇的人......我不认为我在这里使用了任何特别奇怪的东西。

dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //Kotlin stuff
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
    //Test tools
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //UI
    implementation 'com.google.android.material:material:1.1.0-alpha10'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.activity:activity:1.1.0-alpha03'
    //Lifecycle
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha04'
    //Moshi for serialization
    implementation 'com.squareup.moshi:moshi:1.8.0'
    implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.retrofit2:converter-moshi:2.6.1'
    //Glide
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.github.bumptech.glide:okhttp3-integration:4.9.0'
    //Navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
    implementation 'androidx.navigation:navigation-common-ktx:2.1.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
    implementation "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0"
    //Diff utils
    implementation group: 'io.github.java-diff-utils', name: 'java-diff-utils', version: '4.0'
    //Timber for logging
    implementation 'com.jakewharton.timber:timber:4.7.1'

编辑 2: 将我的 Gradle 依赖项复制粘贴到一个新的空项目中并得到相同的错误。它可能是这些依赖项之一吗?

编辑 3: 从我的 build.gradle 中注释掉各种库(Moshi、Retrofit、Timber、Glide)也没有效果。所以,我可能应该向 Google 提交一份错误报告。在那之前,有什么解决方法?

编辑 4: 根据 cmets 的建议,我尝试删除我的 .gradle 文件夹并回滚到早期版本的 Material Design 库。两者都没有效果,但后者在构建日志中添加了一些有趣的东西:

> Transform artifact kotlin-build-common.jar (org.jetbrains.kotlin:kotlin-build-common:1.3.20) with DexingNoClasspathTransform
AGPBI: "kind":"error","text":"Invoke-customs are only supported starting with Android O (--min-api 26)","sources":[],"tool":"D8"

【问题讨论】:

你在用黄油刀吗? @ZaferCelaloglu 不。从来没有听说过。 尝试删除 .gradle 文件夹并重建项目(同时备份 .gradle 文件夹) @AdeelTurk 查看编辑。我对新的空项目进行了尝试,得到了相同的结果。 你可以尝试将 matreial 版本从 alpha 10 更改为 07 -> 1.1.0-alpha07 【参考方案1】:

您不应该将androidx.navigation:navigation-safe-args-gradle-plugin 放在您的应用级别build.gradle 中。那需要删除。

【讨论】:

【参考方案2】:

仅从 Android O (--min-api 26) 开始支持调用自定义。

这意味着,您至少需要:

compileSdkVersion 26
targetSdkVersion 26

【讨论】:

【参考方案3】:

在我的情况下,我更新了 android studio,它自动更新了我的 gradle 版本并且构建失败,返回旧版本解决了问题。

【讨论】:

以上是关于android-jetifier: "无法解析所有工件";新项目上的“改造工件失败”的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 无重复字符的最长子串

滑动窗口-3. 无重复字符的最长子串

力扣Leetcode 3. 无重复字符的最长子串

编程练习:无重复字符的最长字串

圆角无图像

[LeetCode] 3. 无重复字符的最长子串