迁移到 AndroidX 时无法解析变量“${animal.sniffer.version}”

Failed to resolve variable '${animal.sniffer.version}' when migrate to AndroidX

我正在使用 Android Studio 3.2 Beta5 将我的项目迁移到 AndroidX。当我重建我的应用程序时,我遇到了这些错误:

ERROR: [TAG] Failed to resolve variable '${animal.sniffer.version}'

ERROR: [TAG] Failed to resolve variable '${junit.version}'

完全清理和重建无效!有人知道如何解决这个问题吗?


gradle.properties

android.enableJetifier=true
android.useAndroidX=true

build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-beta05'

        classpath 'com.google.gms:google-services:4.0.1'
        classpath "io.realm:realm-gradle-plugin:5.3.1"
        classpath 'io.fabric.tools:gradle:1.25.4'
        classpath 'com.google.firebase:firebase-plugins:1.1.5'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'com.google.firebase.firebase-perf'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.0"
    defaultConfig {
        applicationId "com.iceteaviet.fastfoodfinder"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
        }
    }
    aaptOptions {
        cruncherEnabled = false
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'

    implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'

    implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
    implementation 'com.google.android.material:material:1.0.0-rc01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
    implementation 'androidx.cardview:cardview:1.0.0-rc01'

    implementation 'com.google.maps.android:android-maps-utils:0.5'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-location:15.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-auth:16.0.1'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'

    implementation 'com.github.bumptech.glide:glide:4.7.1'

    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

    implementation 'org.greenrobot:eventbus:3.1.1'

    implementation 'de.hdodenhof:circleimageview:2.2.0'

    implementation 'io.realm:realm-android-library:5.3.1'

    implementation 'com.facebook.android:facebook-android-sdk:4.34.0'

    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.2'

    implementation 'androidx.multidex:multidex:2.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.4'
    implementation 'com.google.firebase:firebase-perf:16.0.0'

    implementation 'com.jakewharton.timber:timber:4.7.1'

    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}

apply plugin: 'com.google.gms.google-services'

尝试删除这一行:

maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

来自 build.gradle 文件的构建脚本/存储库部分。

当我添加该行时,出现了您描述的错误。当我删除它时,不再。该行应该只在 allprojects / repositories 部分。

我用两个步骤解决了这个问题

1) 文件 -> 使缓存无效/重新启动...

2) 构建 -> 清理项目

尝试在 gradle.properties 中设置 android.enableJetifier=false。然后在 Android Studio

中使缓存无效/重新启动 ...

好像是Glide的问题

我有同样的错误,我只是将 Glide 的依赖项更新为 4.8,没有构建错误。

科特林:

// Glide
def glide_version = "4.8.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"

Java :

// Glide
def glide_version = "4.8.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"

请务必在您的 gradle.properties 中启用:

android.useAndroidX=true
android.enableJetifier=true

来源:https://github.com/bumptech/glide/issues/3124

希望对您有所帮助!

修复在 4.2.0 中,使用更高版本的 google gms jar。

尝试更改:

classpath 'com.google.gms:google-services:4.0.1'

通过此版本:

classpath 'com.google.gms:google-services:4.2.0'

希望这能奏效...

我通过将 firebase 依赖项更新到最新版本来解决这个问题。

添加 Java 8 支持build.gradle 文件解决了我的问题

android {
     ...

     //Add the following configuration in order to target Java 8.
     compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
     }
}

AndroidX Test dependencies 更新我的 build.gradle 文件后,我遇到了同样的错误。结果我忘了删除旧的 junit 依赖项。所以对我来说,修复只是删除以下依赖项:

dependencies {
    ...
    testImplementation 'junit:junit:4.12'
}

如果您正在使用 Kotlin,如果您在项目中使用的任何注释处理器未使用 kapt 版本,则会弹出此问题。
正如@Vince 提到的 Glide 的情况,Dagger2Butterknife 等可能会发生这种情况
如果您同时使用 JavaKotlin,则需要保留这两个依赖项,如下所示($glideVersionGlide 的预定义版本):

implementation "com.github.bumptech.glide:glide:$glideVersion"

kapt "com.github.bumptech.glide:compiler:$glideVersion"

如果您在一个只有 Kotlin 的项目上,kapt 依赖项应该单独工作。

编辑
您应该记住的另一件事是您是否已经在使用 AndroidxAndroidx 是一个很棒的重构,但在迁移时它可能会导致您的某些依赖项崩溃。主流库已经更新到 Androidx,但是,其中一些没有,甚至不会。
如果此编辑上方我提供的解决方案没有解决问题,您可以查看您的依赖项并确保它们也使用 Androidx

编辑 2
正如@Ted 提到的,我进行了研究,他是对的 kapt 也处理 java 文件。 kapt 单独就可以了,不需要同时保留 kaptannotationProcessor 依赖项。

我通过刷新缓存修复了它(而不是使它失效 - 这也会清除本地历史记录):

  1. 在 gradle.properties 文件中,注释行 org.gradle.caching=true.
  2. 清理,重建。
  3. 在gradle.properties文件中,取消注释该行 org.gradle.caching=真。
  4. 清理,重建。

就是这样!

删除 testInstrumentationRunner 对我有用:

defaultConfig {
...
...
//        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

转到文件并单击“使缓存无效并重新启动”。

重启后,您可以在应用的 build.gradle 文件中增加最低 SDK 版本。

通过转到主目录并键入 flutter clean

来修复它

如果您使用的是dagger 或butterknife,请务必将其更新至最新版本。或者,如果你的项目中使用了其他注入库,你可以尝试检查它是否支持androidx。

我发现了同样的错误,问题出在我的匕首和黄油刀上。已通过将其更新到最新版本来修复它。

Android版本:4.10.2

我通过三个简单的步骤解决了这个问题: 首先,我在 pubspec.yml 中添加了以下内容: (有两个空格)

module:
  androidX: true 

在 gradle.properties 下面添加这两行,我在 android/gradle.properties 的项目文件夹中有这个。

android.useAndroidX=true
android.enableJetifier=true

然后我用终端写了:

flutter clean

也许您将不得不停止设备并再次运行。

我在将 butterknife 添加到我的项目后遇到了这个错误。

要解决此错误,您应该使用 Java8。

我会推荐 答案。

我是如何解决的:

1.add 以下代码在 build.gradle(模块:app)

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
  1. 清理项目和 运行。