【问题标题】:Android Gradle Cannot build and cannot resolve RAndroid Gradle 无法构建也无法解析 R
【发布时间】:2019-03-20 03:19:20
【问题描述】:

问题 1: 在 MainActivity.java 中出现语法错误“无法解析符号 R”。

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

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

我尝试了一些方法,包括

  1. 正在删除 .gradle 和 .idea/libraries。

  2. 添加以下行

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


这是我的应用和 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
}

【问题讨论】:

  • 我建议使用您正在使用的所有库的最新版本;支持注解 - com.android.support:support-annotations:28.0.0
  • 这通常意味着,您在资源布局文件中存在 XML 问题。
  • MD Naseem Ashraf,我不知道为什么,但这确实解决了问题。老实说,我很感激和困惑。
  • 如果是 XML 问题,我如何才能找到源以及为什么它会构建到我的手机中?
  • @KurtPrice 我会把我的评论变成答案,请接受它作为答案。

标签: android android-gradle-plugin build.gradle


【解决方案1】:

您必须在存储库部分添加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'

【讨论】:

  • 我仍然遇到同样的问题。来自 gradle 的错误信息仍在谈论 com.android.support:support-annotations:26.1.0
  • 您尝试过第二种解决方案吗?
  • 我现在正在测试它,而没有像我从顶部评论中所做的更改也修复了它。
  • 第二部分确实解决了第一个问题。但是,R 仍未解决。
【解决方案2】:

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

原因:org.gradle.api.GradleException: 找不到版本 'com.android.support:support-annotations' 满足版本 约束:

因为-

实现'com.android.support:support-annotations:26.1.0'

升级到最新的稳定版本的库可以解决这个问题。

最新版本为:

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

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

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

【讨论】:

    【解决方案3】:

    这是因为 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 需要使用相同的版本,并支持库以避免该问题。

    【讨论】:

      【解决方案4】:

      每个版本的 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
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-25
        • 1970-01-01
        • 2014-06-03
        • 1970-01-01
        • 2018-05-24
        • 2021-02-26
        • 1970-01-01
        相关资源
        最近更新 更多