Android Gradle无法构建且无法解析R.

问题描述 投票:1回答:4

问题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
}
android android-gradle build.gradle
4个回答
1
投票

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

引起: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


2
投票

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

0
投票

这是因为Android Studio工具假设您使用最新的buildToolsVersion,这是28.0.3,当您没有明确地将buildToolsVersion添加到您的app 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和支持库使用相同的版本以避免此问题。


0
投票

每个版本的Android Gradle Plugin现在都有一个默认版本的构建工具。 Android Gradle Plugin 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

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

© www.soinside.com 2019 - 2024. All rights reserved.