Android Gradle 无法构建也无法解析 R

Android Gradle Cannot build and cannot resolve R

问题 1: 在 MainActivity.java 中出现语法错误 'cannot resolve symbol R'.

问题 2: 当我尝试清理或重建项目时,我收到一个很长的链接错误 here

还值得注意的是,我仍然可以 运行 应用程序并将其部署到我的 phone。只是构建应用程序的选项失败。

我尝试了一些方法,包括

  1. 删除 .gradle 和 .idea/libraries.

  2. 添加以下行

    implementation 'com.android.support:support-annotations:26.1.0'
    
  3. 做一个项目,做一个app


这是我的应用程序和 MyApp Gradle 个文件。

应用级别:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "engineer.myname.myapp.myapp"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-annotations:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

项目级别:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

您必须在存储库部分添加 maven { url 'https://maven.google.com' }

项目级别:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

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

如果您使用版本 26,则内部依赖项版本应为 1.0.13.0.1 即,如下所示

androidTestImplementation 'com.android.support.test:runner:1.0.1'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

如果您使用版本 27,则内部依赖项版本应为 1.0.23.0.2 即,如下所示

androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

每个版本的 Android Gradle 插件现在都有一个默认版本的构建工具。 Android Gradle 插件 3.3.2 的最低支持版本是 28.0.3可能 你的依赖版本,例如com.android.support:support-annotations:28.0.0,应与此版本匹配以消除以下错误:

>Caused by: org.gradle.api.GradleException: Cannot find a version of 'com.android.support:support-annotations' that satisfies the version constraints:
   Dependency path 'MyApp:app:unspecified' --> 'com.android.support:support-annotations:'
   Constraint path 'MyApp' --> 'com.android.support:support-annotations' strictly '26.1.0' because of the following reason: debugRuntimeClasspath uses version 26.1.0

以下链接可能与您的问题相关:

这是因为 Android Studio 工具假定您使用的是最新的 buildToolsVersion,即 28.0.3,而您并未明确将 buildToolsVersion 添加到你的应用 build.gradle 像这样:

android {
    compileSdkVersion 26
    // you didn't add any buildToolsVersion
    defaultConfig {
        applicationId "engineer.myname.myapp.myapp"
        minSdkVersion 22
        targetSdkVersion 26

        ...
    }

    ...
}

因此,Android Studio 将隐式添加 buildToolsVersion "28.0.3":

android {
    compileSdkVersion 26
    buildToolsVersion "28.0.3" // added implicitly by Android Studio.
    defaultConfig {
        applicationId "engineer.myname.myapp.myapp"
        minSdkVersion 22
        targetSdkVersion 26

        ...
    }

    ...
}

当你构建应用程序时,它会给你错误 'cannot resolve symbol R' 因为 buildToolsVersion 28.0.3 无法正确使用 compileSdkVersion 26 和支持库 26.

因此,您需要为 buildToolsVersioncompileSdkVersion 使用相同的版本,并支持库以避免该问题。

错误与使用的支持注释版本有关。它在您的错误日志中,最后由行 -

Caused by: org.gradle.api.GradleException: Cannot find a version of 'com.android.support:support-annotations' that satisfies the version constraints:

因为-

implementation 'com.android.support:support-annotations:26.1.0'

升级到最新稳定版本的库将解决问题。

最新版本为:

implementation 'com.android.support:support-annotations:28.0.0'

需要升级到最新的库版本可能有很多原因;较新的 Android 工具或 Android Studio 需要最新的库,或者您的一个库需要另一个库才能运行等。

最好尽可能使用最新的稳定版本库。此外,请考虑使用 AndroidX 库,因为将来 Google 将不再使用旧编号版本。从 Google Android 文档中阅读 here and here