繁体   English   中英

Android Studio - R class 在 Android Studio 更新后未知

[英]Android Studio - R class unknown after Android Studio Update

我需要执行我的 android 应用程序的更新,以将 targetSdkVersion 从 29 更新到 31(第一步)。 在这种情况下,我已将我的 Android Studio 从 4.0.2 更新到 Android Studio Electric Eel | 2022.1.1. 此外,我将我的 Gradle 从 6.x 升级到 7.5(稍后想升级到 8)。
在我执行更新后,Android Studio 不再找到或生成 R class。 使用import my.package.R;
无论如何,我仍然能够成功构建我的项目并在模拟器上运行它。 我还在我的手机上部署了应用程序,它运行良好。 我在 inte.net 上搜索帮助,发现 Android Studio 的编辑器和 function 版本有不同的编译器,这就是为什么这可能有效。 但是,如何解决编辑器的问题?

这是我已经做过的:

  1. 我检查了我的 XML 是否有错误(发现并更正了两个错误。我只更正了“当前文件”选项卡中“问题”视图中列出的那些错误。“布局和限定符”选项卡中仍然存在错误,但我认为那些并不重要,因为它们是这样的错误:“触摸目标尺寸太小 - 此项目的尺寸为 40dp x 40dp。考虑将此触摸目标设为 48dp 宽和 48dp 高或更大。”)
  2. 我清理了项目并重建了它。
  3. 我单击文件 -> 将项目与 Gradle 文件同步
  4. 我用 File -> Invalidate Caches 清空缓存

解决此问题的任何帮助表示赞赏。 仅供参考:我的 gradle 看起来像这样:

apply plugin: 'com.android.application'
def versionMajor = 1
def versionMinor = 0
def versionPatch = 1
android {
    compileSdkVersion 31

    defaultConfig {
        applicationId "my.app.id"
        versionCode versionMajor * 10000
        + versionMinor * 100
        + versionPatch
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
        minSdkVersion 26
        targetSdkVersion 31
        versionCode 4
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        debug {
            versionNameSuffix ".debug"
            resValue "string", "app_version", "${defaultConfig.versionName}${versionNameSuffix}"
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            resValue "string", "app_version", "${defaultConfig.versionName}"
        }
    }
    namespace 'my.app.id'
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
    }
    buildToolsVersion '30.0.2'
}

repositories {
    mavenCentral()
    maven { url 'https://maven.google.com' }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'androidx.navigation:navigation-fragment:2.3.1'
    implementation 'androidx.navigation:navigation-ui:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation "androidx.room:room-runtime:2.2.5"
    annotationProcessor "androidx.room:room-compiler:2.2.5"
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}
step 1: clean 
step 2: gradle sync
step 3: rebuild the project

如果问题仍然存在,请尝试invalidateCache & Restart

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM