Androidx 迁移后出现的未解决引用 R

5
在Gradle Scripts中更新了build.gradle(Module: app)中的依赖项后,所有由R定义的布局、字符串和引用都不可用。我在模块app中有以下代码:
subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex')) {
                details.useVersion "28.0.0"
            }

            if(details.requested.group == 'androidx.lifecycle'  && !details.requested.name.contains('multidex'))
            {
                details.useVersion "2.0.0"
            }

        }
    }
}

dependencies 
{


 implementation 'androidx.test:runner:1.1.0'
    implementation 'androidx.test.espresso:espresso-core:3.1.0'
        //    androidTestImplementation 'com.android.support.test:runner:1.0.2'
        //    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // lifecycle components
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-core:2.0.0'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0'

    //    implementation 'android.arch.lifecycle:extensions:1.1.1'
    //    kapt "android.arch.lifecycle:compiler:1.1.1"

    // room components
        //    implementation 'android.arch.persistence.room:runtime:1.1.1'
    implementation 'androidx.room:room-runtime:2.0.0'

    //    data binding components
    annotationProcessor "com.android.databinding:compiler:3.1.4"
    implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

    //implementation 'com.google.dagger:dagger-android:2.16'
    implementation 'com.google.dagger:dagger-android-support:2.16'
    // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'
    compile project(path: ':data')
}

通过项目浏览器搜索项目时,该项目似乎不再具有R文件。


@zapl 那个方法没有起作用。 - George
我遇到了以下Gradle错误:Android依赖项'com.android.support:animated-vector-drawable'在编译时(25.0.0)和运行时(28.0.0)类路径上有不同的版本。你应该通过DependencyResolution手动设置相同的版本。 - George
你不能在data项目或任何依赖项来源中修复依赖关系吗?要找出来怎么做,请访问以下链接:https://dev59.com/8lkT5IYBdhLWcg3wIsOw - zapl
你对上面的问题有什么进展吗?我在将现有项目迁移到androidx命名空间后遇到了类似的问题 - Android依赖项'androidx.appcompat:appcompat'在编译(1.0.0)和运行时(1.1.0-alpha01)classpath中具有不同的版本。你应该通过依赖解决来手动设置相同的版本。 - Namrata Bagerwal
@NamrataBagerwal 希望不算晚,解决你的问题的方法是在你的依赖项中添加1.1.0-alpha01,使用implementation或api "androidx.appcompat:appcompat:1.10-alpha01"。 - George
显示剩余2条评论
1个回答

7

我在迁移到androidx后遇到了这样的问题。经过一些努力,我发现问题是由于我使用最新的Gradle插件(如下所示)而我的Android Studio版本不是最新的(它是3.2)。

com.android.tools.build:gradle:3.3.0

当我将Gradle插件更改为较低版本(如下所示)时,一切都很好。
com.android.tools.build:gradle:3.2.1

解决方案:

因此,解决方案是使用与您的Android Studio版本匹配的Gradle插件(和Gradle包装器)版本。


1
@jeppla Gradle插件版本是在项目级别的build.gradle文件中设置的。 - Mostafa Arian Nejad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接