【问题标题】:Failed to resolve variable '${animal.sniffer.version}' when migrate to AndroidX迁移到 AndroidX 时无法解析变量“${animal.sniffer.version}”
【发布时间】:2019-01-18 16:50:25
【问题描述】:

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

错误:[TAG] 无法解析变量“${animal.sniffer.version}”

错误:[TAG] 无法解析变量“${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'

【问题讨论】:

标签: android android-studio android-studio-3.0 androidx android-studio-3.2


【解决方案1】:

尝试删除此行:

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

来自 build.gradle 文件的 buildscript / repositories 部分。

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

【讨论】:

【解决方案2】:

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

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

2) 构建 -> 清理项目

【讨论】:

  • 不知何故进行了几次重建,但我现在又遇到了同样的错误..
  • 对我来说清洁项目已经足够了
  • Clean Project 也是我所需要的。感谢@f4bo 的提示。
  • 它有效。我只清理项目。此答案应设置为已接受的答案。 @nhoxbypass
【解决方案3】:

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

【讨论】:

  • 不推荐,因为可能有第三方库还没有升级到AndroidX!正如文档所说:“android.enableJetifier:当设置为 true 时,Android 插件会自动迁移现有的第三方库以通过重写其二进制文件来使用 AndroidX。如果未指定,默认情况下该标志为 false。” developer.android.com/jetpack/androidx
【解决方案4】:

这似乎是滑行的问题。

我遇到了同样的错误,我刚刚将 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

希望对您有所帮助!

【讨论】:

  • 这个错误在我看来也没有 Glide 作为依赖项
【解决方案5】:

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

尝试改变:

类路径'com.google.gms:google-services:4.0.1'

通过这个版本:

类路径'com.google.gms:google-services:4.2.0'

希望这行得通...

【讨论】:

  • 这对我有用以及更新依赖项。
【解决方案6】:

我通过将 firebase 依赖项更新到最新来解决此问题。

【讨论】:

    【解决方案7】:

    为我添加 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
         }
    }
    

    【讨论】:

    • 找不到后靠。我正在构建一个电视应用程序,它在任何关于leanback 支持 v17 的内容上都变红了
    • 使缓存失效并重启android studio
    • 不,不是,我认为你必须使用 androidx 包。
    • 我猜添加 Java 8 支持会强制 Android Studio 使其缓存无效,从而达到与副作用相同的结果...嗯,好吧...
    【解决方案8】:

    在使用AndroidX Test dependencies 更新我的 build.gradle 文件后,我遇到了同样的错误。原来我忘了删除旧的 junit 依赖项。所以对我来说,解决方法只是删除以下依赖项:

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

    【讨论】:

    • 这对我来说是个问题。
    • 是的,已修复。我遇到了 Udacity Kotlin 课程猜猜游戏的问题
    • 您能解释一下为什么会这样吗?
    【解决方案9】:

    如果您使用的是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 依赖项。

    【讨论】:

    • kapt 也可以处理java文件,所以不需要两个都用,只要kapt就可以了。
    【解决方案10】:

    我通过刷新 cahche 来修复它(而不是使其无效 - 这也会清除本地历史记录):

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

    就是这样!

    【讨论】:

      【解决方案11】:

      删除 testInstrumentationRunner 对我有用:

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

      【讨论】:

      • 绝妙的解决方案!为我修好了
      【解决方案12】:

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

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

      【讨论】:

        【解决方案13】:

        通过转到主目录并输入flutter clean来修复它

        【讨论】:

        • 太好了,如果你不使用颤振怎么办 ;)
        • @KevinGalligan 然后使用 Flutter ;)
        【解决方案14】:

        如果您使用匕首或黄油刀,请确保将其更新到最新版本。或者,如果您的项目中使用了另一个注入库,您可以尝试检查它是否支持 androidx。

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

        【讨论】:

          【解决方案15】:

          安卓版本:4.10.2

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

          module:
            androidX: true 
          

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

          android.useAndroidX=true
          android.enableJetifier=true
          

          然后我用终端写了:

          flutter clean
          

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

          【讨论】:

            【解决方案16】:

            在我的项目中添加黄油刀后,我遇到了这个错误。

            为了解决这个错误,您应该使用 Java8。

            我会推荐this 答案。

            我是如何解决的:

            1.在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. 清理项目并运行。

            【讨论】:

              猜你喜欢
              • 2019-03-23
              • 1970-01-01
              • 2019-08-24
              • 2019-06-27
              • 2019-07-08
              • 2019-11-07
              • 2019-11-29
              • 2021-12-03
              • 2019-06-21
              相关资源
              最近更新 更多