Kurt Price
Kurt Price

Reputation: 303

Android Gradle Cannot build and cannot resolve R

Issue 1: In MainActivity.java I get a syntax error 'cannot resolve symbol R'.

Issue 2: When I try to clean or rebuild the project I get a very long error linked here

It is also worth noting that I can still run the app and have it deployed to my phone. Just the option to build the app fails.

I have tried a few things including

  1. deleting .gradle and .idea/libraries.

  2. adding the following line

    implementation 'com.android.support:support-annotations:26.1.0'
    
  3. Make a project and make an app


Here are my app and MyApp Gradle files.

App Level:

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'
}

Project Level:

// 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
}

Upvotes: 1

Views: 458

Answers (4)

VIISHRUT MAVANII
VIISHRUT MAVANII

Reputation: 12698

You have to add maven { url 'https://maven.google.com' } in repositories section.

Project Level:

// 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
}

OR

If you use version 26 then inside dependencies version should be 1.0.1 and 3.0.1 i.e., as follows

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

If you use version 27 then inside dependencies version should be 1.0.2 and 3.0.2 i.e., as follows

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

Upvotes: 2

shizhen
shizhen

Reputation: 12583

Each version of the Android Gradle Plugin now has a default version of the build tools. The minimum supported version for Android Gradle Plugin 3.3.2 is 28.0.3. And probably your dependency versions, e.g. com.android.support:support-annotations:28.0.0, should match with this version in order to get rid of below error:

>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

Here are links that may be related to your question:

Upvotes: 0

MD Naseem Ashraf
MD Naseem Ashraf

Reputation: 1028

The error is with the version of Support Annotations used. It's in your error log, the last caused by line -

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

Because of -

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

Upgrading to the latest stable version of the library will fix the problem.

Latest version as:

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

The need to upgrade to the latest library versions can be because of many reasons; newer Android Tools or Android Studio would require up to date libraries or one of your libraries require another library to function etc.

It is best to use the latest stable release libraries when you can. Also, consider using AndroidX libraries as older numbered versions will not be used anymore by Google in the future. Read here and here from Google Android Documentation.

Upvotes: 1

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29794

This is because Android Studio tools is assuming that you're using the latest buildToolsVersion which is 28.0.3 when you're not explicitly add the buildToolsVersion to your app build.gradle like this:

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

        ...
    }

    ...
}

So, Android Studio will implicitly adding 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

        ...
    }

    ...
}

When you're building the app, it will gives you error 'cannot resolve symbol R' because the buildToolsVersion 28.0.3 cannot correctly working with compileSdkVersion 26 and support libraries 26.

So, you need to use the same version for buildToolsVersion, compileSdkVersion, and support libraries to avoid the problem.

Upvotes: 0

Related Questions