75

I am building a sample project from Udacity. This was working fine till now, but after upgrading to Android Studio 3.2.1, I am facing the build error below.

Gradle version: 4.6

Project link: https://github.com/udacity/ud851-Sunshine/tree/student/S02.02-Solution-Menus

Could not find com.android.tools.build:aapt2:3.2.1-4818971**. Searched in the following locations:
    file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971.pom
    file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-windows.jar
    file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971.pom
    file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-windows.jar
    file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971.pom
    file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-windows.jar
    https://jcenter.bintray.com/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971.pom
    https://jcenter.bintray.com/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-windows.jar Required by:
    project :app

How can I fix it?

Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
sandeep kakkar
  • 923
  • 1
  • 9
  • 12
  • Welcome to SO. Please add more details about what version of gradle you are using and which repositories you have in your project by adding the root build.gradle in your question maybe? That would be great. – ʍѳђઽ૯ท Oct 13 '18 at 06:07
  • @Mohsen I have added the link to the Udacity project. –  Oct 13 '18 at 06:13
  • The gradle seems pretty old: `classpath 'com.android.tools.build:gradle:2.2.3'`. Have you tried `classpath 'com.android.tools.build:gradle:3.2.1'` or updating the dependencies? Also, use `implementation` instead of `compile`. – ʍѳђઽ૯ท Oct 13 '18 at 06:17
  • Did that. Still same error. –  Oct 13 '18 at 06:22
  • Issue is fixed after referring https://stackoverflow.com/a/50290877/7103964 Thanks @Mohsen –  Oct 13 '18 at 06:30
  • Well I thought the same so since the gradle version was pretty old. However, I'll add an explanation as an answer. – ʍѳђઽ૯ท Oct 13 '18 at 06:33
  • 3
    Found a solution: https://stackoverflow.com/a/50290877/7103964 Adding google repo to allprojects fixed my problem. –  Oct 13 '18 at 06:28

9 Answers9

164

For Android Studio 3.2.1 update

Just add google() in root level build.gradle

buildscript {
    repositories {
        google()   //  <--here
        jcenter()
    }
 }

allprojects {
    repositories {
        google()   //  <-- here
        jcenter()
    }
}

and see the magic - error is gone.

Community
  • 1
  • 1
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
8

The project gradle version is pretty old:

classpath 'com.android.tools.build:gradle:2.2.3'

And you are using Android Studio v3.2.1 so, update the gradle:

classpath 'com.android.tools.build:gradle:3.2.0' // or 3.2.1 maybe

Also, as you can see, it was looking for some packages in :

file:/C:/Users/sandeepk2/AppData/Local/Android/Sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971.pom

Which means you probably forgot to add google() as the top level repository. Just add google()

to your repositories in your root build.gradle.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
6

To get this solved

Firstly download the missing Jar file from the link below. I see you are missing version 3.2.1-4818971

https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-windows.jar

Switch your folder structure from Android to Project.

FolderStructure

Now navigate to the libs folder inside the app - build folder. (If the libs folder does not exist, you can create it by right clicking on the app folder - select New - select Directory.)

LibsFolder

Paste the downloaded.jar file inside libs folder. Right click on the jar file and at the bottom click on Add as library. This will take care of adding implementation files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].

AddAsLibrary

Everything should be okay once you sync after doing the above. Here is the source link to this solution: https://developer.android.com/studio/command-line/aapt2#download_aapt2

Let me know if you run into any other issues whilst doing the above.

d4rkcon
  • 143
  • 1
  • 8
  • 3
    Why Google is not bundling this in their repo? Android dev exp is the worst I had – mike Oct 24 '18 at 13:15
6

Yeah, as d4rkcon said download https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-windows.jar But you can do simplier - just put this file in directory where Andoid Studio is trying to find it. If you don't have directories like /tools/build/aapt2/3.2.1-4818971/ in AndroidSDK folder just create them and then put aapt2-3.2.1-4818971-windows.jar file in.

Alesha170
  • 61
  • 2
  • Thanks, this worked for me. I had to add 3 levels of directories. Also there are 3 flavors of the jar file. On a mac it is: https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/3.2.1-4818971/aapt2-3.2.1-4818971-osx.jar (substitute osx or linux for windows in the jar file) – Vicky T Oct 19 '18 at 20:55
3

Add Google repository in your build.gradle(Project:xxxxx)

allprojects {
    repositories {
        google()
    }
}
Jim Lin
  • 31
  • 2
2

clear gradle cache

rm -rf $HOME/.gradle/caches/

and resync to download all dependencies

Hamid Zandi
  • 2,714
  • 24
  • 32
  • 1
    yes, this is the solution, after upgrading gradle, must resync all dependencies for run this command successfully: react-native run-android – nima Feb 26 '19 at 14:48
1

Change your build.gradle as follows.

android {
    compileSdkVersion 26
    buildToolsVersion "28.0.3"

    defaultConfig {
        applicationId "your package name here"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:26.1.0'
}
Pang
  • 9,564
  • 146
  • 81
  • 122
0

add google() on your build script > repositories add google on allprojects > repositories

use implementation as replacement of compile keyword, also on your filetree.

EX.

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    implementation 'com.android.support:appcompat-v7:25.3.1'
    implementation 'com.android.support:design:25.3.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:25.3.1'
Mostafa Rostami
  • 1,906
  • 20
  • 29
fMadTech
  • 308
  • 3
  • 5
0

For Android Studio 3.6.3 update : just adding google maven tag in buildscript and all projects repositories in build.gradle file of project file solves the issue.

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'

    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}
Abeni
  • 1
  • 1
  • Would you mind updating your answer with additional information? What differs in this configuration from other, previous answers? Do the previous approaches not work in Android Studio 3.6.3? What specific parts of your configuration are key to the solution? Is it primarily the `maven {}` configurations? – Jeremy Caney May 19 '20 at 19:43
  • yes , other answers do not solve issues on the latest android studio update. – Abeni May 19 '20 at 19:59