简体   繁体   中英

Android Studio - R class unknown after Android Studio Update

I needed to perform an update of my android app to update the targetSdkVersion from 29 to 31 (in a first step). In this context, I have updated my Android Studio from 4.0.2 to Android Studio Electric Eel | 2022.1.1. Additionally, I upgraded my Gradle from 6.x to 7.5 (Want to upgrade to 8 at a later stage).
After I performed the update, Android Studio does not find or generate the R class anymore. I get the error "Cannot resolve symbol 'R'" when I import the class with import my.package.R;
Anyhow, I am still able to successfully build my project and run it on an emulator. I also deployed the application on my phone and it works fine. I searched the inte.net for help and I found that the editor of Android Studio and the build function have different compilers which is why this probably works. However, how can I fix my problem with the editor?

This is what I already did:

  1. I checked my XML for errors (Found and corrected two errors. I only corrected those errors listed in the "Problems" View in the tab "CUrrent file". There are still errors in the "Layout and Qualifiers" tab, but I think those are not critical because they are errors like this: "Touch target size too small - This item's size is 40dp x 40dp. Consider making this touch target 48dp wide and 48dp high or larger.")
  2. I cleaned the project and rebuild it.
  3. I clicked on File -> Sync Project with Gradle Files
  4. I emptied the Cache with File -> Invalidate Caches

Any help to fix this is appreciated. FYI: my gradle looks like this:

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

If problem still exists try invalidateCache & Restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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