Gradle in Android Studio: Failed to resolve third-party libraries - android

I have been trying to switch my project from Intellij to Android Studio, which has required me to create a build.gradle file. I know I can add each of these as a library dependency, but I ideally want to be able to get the maven repository dependency working.
Every time I sync, my support libraries are synced fine, but for each third-party library, I get something like
"Error:(30, 13) Failed to resolve:
com.facebook.android:facebook-android-sdk:3.23.1"
for each library.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
// Google Play Services
compile 'com.google.android.gms:play-services:6.5.87'
// Support Libraries
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:gridlayout-v7:21.0.3'
compile 'com.android.support:mediarouter-v7:21.0.3'
compile 'com.android.support:palette-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'
compile 'com.android.support:support-annotations:21.0.3'
compile 'com.android.support:support-v13:21.0.3'
compile 'com.android.support:support-v4:22.0.0'
// third-party libraries
compile 'com.amazonaws:aws-java-sdk:1.9.24'
compile 'com.facebook.android:facebook-android-sdk:3.23.1'
compile 'com.github.markushi:android-ui:1.2'
compile 'de.hdodenhof:circleimageview:1.2.2'
compile 'it.neokree:MaterialNavigationDrawer:1.3.2'
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}

Add:
repositories {
mavenCentral()
}
to the build.gradle. Now you have repositories defined only in build script which resolves dependencies only for the buildscript itself not for the project.

Just to share infomation, I got same problem and the solution was different.
In my case, proxy server was used and it causes the problem. I needed to configure https proxy settings, as discussed in gradle behind proxy in Android Studio 1.3.

If you use VPN | Proxy on your system then use your proxy info in the gradle.properties file in your project like the following lines of code:
# HTTP Proxy
systemProp.http.proxyHost={Host Address}
systemProp.http.proxyPort={Port Number}
systemProp.http.proxyUser={Proxy Username}
systemProp.http.proxyPassword={Proxy Password}
systemProp.http.nonProxyHosts={NonProxy Hosts Address} # like: 127.0.0.1,localhost
# HTTPS Proxy
systemProp.https.proxyHost={Host Address}
systemProp.https.proxyPort={Port Number}
systemProp.https.proxyUser={Proxy Username}
systemProp.https.proxyPassword={Proxy Password}
systemProp.https.nonProxyHosts={NonProxy Hosts Address} # like: 127.0.0.1,localhost
Now just replace {.....} in the above code with appropriate data
Also you can set Android studio proxies like the following image by your proxy info in File>Settings:
Now test again...!

repositories {
jcenter {
url "http://jcenter.bintray.com/"
}
// mavenCentral()
}

this helps
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
in projects's gradle

Maybe this help
allprojects {
repositories {
jcenter()
}
}

well , if your network connection is fine(wheter using proxy | VPNs or not), simply just try turn off'offline mode' and sync when using android studio with gradle 2.3,this worked for me :)

Most of the settings do not currently work with new Android updates, so the solution currently is to add this section to buuild.Gradle(Project:)
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
}
}
******************************************
// Add this section if it does not exist
repositories {
mavenCentral()
}
******************************************
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
**********************************************
// Add this section if it does not exist
gradlePluginPortal()
**********************************************
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

Related

Android Gradle Why do we need "allprojects"?

I faced a problem when I tried to add Gson to my existing Android gradle project.
The exception was something like this
" failed to resolve:com.google.code.gson:gson:2.3.1"
My build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
.
.
After some searching and trial and error I found that you need to add the following to fix the issue:
allprojects {
repositories {
jcenter()
}
}
Why to do we need to reference the jcenter two times and why was the "allprojects" important to fix this issue ?
After some searching and trail and error i found that you need to add the following to fix the issue
Only if you have a traditional Android Studio project, where you have the application divided into modules (e.g., app/). What is really required is for the module to know where to pull dependencies from.
Why to do we need to reference the jcenter two times
The repositories closure in your buildscript closure says "these are the artifact repositories from which to pull Gradle plugins and other dependencies listed for the buildscript closure itself".
The repositories closure that you have in allprojects closure says "these are the artifact repositories from which to pull your own application dependencies".
In a traditional Android Studio project, the allprojects closure goes in the build.gradle file that is in the project root directory. allprojects says "everything in this closure, please apply to the Gradle configuration for all of the modules in this project, such as that app/ module over there".
Now, your build.gradle snippet in your question hints that perhaps you do not have a traditional Android Studio project, but instead have one where you do not have an app/ module and you have only one build.gradle file. In that case, allprojects itself is not necessary (as there are no modules), but you still need to specify what the repositories are.
For example, here is a build.gradle file from a module-less project:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
dependencies {
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.android.support:support-v13:21.0.3'
compile 'com.commonsware.cwac:wakeful:1.0.+'
}
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Here, in addition to a repositories closure inside buildscript (to define where the plugins come from), I have a top-level repositories closure, to define where the top-level dependencies come from.
They are just different repositories where the libraries are stored. GSON is obviously in the JCentral and others are using the Maven Ventral.
From reading up on JCenter, it is the repository behind Bintray, from the company JFrog (who I've come across before, and I guess that's where the 'J' comes from). According to the Bintray blog, Bintray is a superset of Maven Central.

Add GitHub library as dependency to Android Studio project

I am trying to implement ActionBar-PullToRefresh from https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/QuickStart-ABC. I just made the switch from Eclipse to Android Studio so I am totally new to AS and Gradle.
chrisbanes writes on the site:
The easiest way to add ActionBar-PullToRefresh to your project is via
Gradle, you just need to add the following dependency to your
build.gradle:
dependencies {
mavenCentral()
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Does this mean that I don't have to download the library and Gradle takes care of it so that I always have the latest version? I just don't know where to put the above line. I have two gradle.build files one in my root that looks like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
and the one in my project which looks like:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Do I have to add a repository somewhere?
It will work when you put this line in your project build.gradle, in the dependencies section:
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
Also, add:
repositories {
mavenCentral()
}
So:
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Gradle will download the needed resources automatically for you.
Use https://jitpack.io/
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.User:Repo:Tag'
}
This is for Kotlin DSL (build.gradle.kts):
repositories {
// Other repositories...
maven("https://jitpack.io")
// OR maven { url = uri("https://jitpack.io") }
}
There are two places inside which you can insert the above code block:
The traditional way: inside top-level build.gradle.kts file
New way: inside a dependencyResolutionManagement { block in settings.gradle.kts file;
read more about this new feature at Gradle user guide: Centralized Repository Declaration

"Could not resolve all dependencies" with 3rd party libs (from Maven Central)

In my build.gradle, I define some 3rd party libs, all of which are available in Maven Central.
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.guava:guava:15.0'
compile 'com.netflix.rxjava:rxjava-core:0.14.2'
compile 'com.netflix.rxjava:rxjava-android:0.14.2'
}
(We also have manually managed jars under libs (for Eclipse users), and I used to use those
for Gradle builds too, with compile fileTree(dir: 'libs', include: '*.jar'), but I'm trying to move away from that towards simpler, automated dependency management.)
Anyway, for some reason fetching the deps fails:
* What went wrong:
A problem occurred configuring root project 'MyApp-Android'.
> Could not resolve all dependencies for configuration ':_debugCompile'.
> Could not find com.google.code.gson:gson:2.2.4.
Required by:
:MyApp-Android:unspecified
> Could not find com.google.guava:guava:15.0.
Required by:
:MyApp-Android:unspecified
> Could not find com.netflix.rxjava:rxjava-core:0.14.2.
Required by:
:MyApp-Android:unspecified
> Could not find com.netflix.rxjava:rxjava-android:0.14.2.
Required by:
:MyApp-Android:unspecified
What am I doing wrong?
I was wondering if I should have
repositories {
mavenCentral()
}
...also on the top level of the build file (not just inside buildscript), but adding that didn't help.
Resolution
I was too hasty; that did help with "Could not resolve dependencies". After that I got compilation errors, but that was due to in fact having some more dependendies in the code (and in libs) than the four entries in dependendies above.
So in my case I had to add the top-level repositories pointing to Maven Central plus some additional dependiencies that we actually had:
compile 'com.squareup.okhttp:okhttp:1.2.1'
compile 'com.android.support:support-v4:13.0.0'
compile 'com.android.support:support-v13:13.0.0'
The (original, broken) full build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.1'
}
}
apply plugin: 'android'
dependencies {
// compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.guava:guava:15.0'
compile 'com.netflix.rxjava:rxjava-core:0.14.2'
compile 'com.netflix.rxjava:rxjava-android:0.14.2'
}
android {
compileSdkVersion 19
buildToolsVersion "19"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Indeed you need to add a
repositories {
mavenCentral()
}
at the top level of your build.gradle to specify where to search for your dependencies.
buildscript {
repositories {
google()
=> mavenCentral() <=
}

Android Gradle library dependency with library dependency using Nexus

I'm switching my project over to using Gradle and an internal SonaType Nexus for hosting my dependencies. My core project depends on library project A and library project A has a dependency on library project B.
My issue is that as soon as I add LibA to my main project I get this error:
"Module version com.example:LibA:1.1 depends on libraries but is not a library itself"
I have no issues adding library projects with jar dependencies with the same build script. I have seen people doing this successfully with LOCAL (in the project) android libraries but no one doing it with maven repos.
Is this a bug in gradle or did I misconfigure the library builds?
Core Project Build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
maven {
url "http://localhost:8081/nexus/content/repositories/releases/"
}
maven {
url "http://localhost:8081/nexus/content/repositories/central/"
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
}
dependencies {
compile 'com.android.support:support-v4:+'
compile('com.example:LibA:1.+')
}
LibA Build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
versionCode = "3"
versionName = "1.2"
}
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aild.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile ('com.example:LibB:1.+')
} ...
LibB Build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
versionCode = "1"
versionName = "1.0"
}
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aild.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
repositories {
mavenCentral()
}
dependencies {
} ...
Edit: Adding -info output for the error.
* What went wrong:
A problem occurred configuring project ':GradleTest'.
> Failed to notify project evaluation listener.
> Module version com.example:LibA:1.+ depends on libraries but is not a library itself
Edit 2: Adding my local maven upload script for LibA
apply plugin: 'maven'
apply plugin: 'signing'
group = "com.example"
version = defaultConfig.versionName
configurations {
archives {
extendsFrom configurations.default
}
}
signing {
required { has("release") && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
uploadArchives {
configuration = configurations.archives
repositories.mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: sonatypeRepo) {
authentication(userName: sonatypeUsername,
password: sonatypePassword)
}
pom.project {
name 'com-example'
packaging 'aar'
description 'none'
url 'https://internal github link'
scm {
url 'scm:git#https://internal github link'
connection 'git#https://internal github link'
developerConnection 'git#https://internal github link'
}
licenses {
license {
name 'example'
url 'example'
distribution 'example'
}
}
developers {
developer {
id 'example'
name 'example'
email 'example'
}
}
groupId "com.example"
artifactId rootProject.name //LibA
version defaultConfig.versionName
}
}
}
Your line in the dependencies to include LibA is wrong. To include a library project, use this:
compile project(':LibA')
If the library's directory isn't at the root of your project directory, you'll need to specify a colon-delimited path. For example, if your directory structure is:
projectFolder
|
+--coreProject
|
+--libraries
|
+--LibA
|
+--LibB
your dependency will be:
compile project(':libraries:LibA')
This is the same as the notation you use in your settings.gradle file.
Maybe problem is that you use mavenCentral as your repository for library projects
repositories {
mavenCentral()
}
and not yours nexus repository where actual dependencies exists
repositories {
maven {
url "http://localhost:8081/nexus/content/repositories/releases/"
}
maven {
url "http://localhost:8081/nexus/content/repositories/central/"
}
}
If you uploaded library artifact for both jar and aar, try this.
compile 'com.example:LibA:1.1.1#aar'
In my work, I have used compile project(':google-play-services_lib') instead of compile ('google-play-services_lib') when I declare dependent projects in my build.gradle file. I think that is the right way to do this with Gradle: http://www.gradle.org/docs/current/userguide/dependency_management.html#sub:project_dependencies
if you don't want to have it as sub-module in the first build.gradle file you can add your local maven repository
mavenLocal()
//repositories
repositories {
mavenCentral()
mavenLocal()
}
but you need to run install on libA first.
I had a similar error message after introducing by mistake a cyclic dependency between libraries:
build.gradle in commons-utils
dependencies {
...
instrumentTestCompile project(':test-utils')
}
build.gradle in test-utils
dependencies {
...
compile project(':commons-utils')
}
Fixing this solved the problem. The error message is not very explicit.
Don't know for sure, just a couple of thoughts:
Have you tried running gradle assemble instead gradle build? This should skip tests, as I see error is related to test task.
Maybe stupid, but try to remove dependcy on 2nd lib from the first and put it to your main build file listing before the first. I have a memory of something related. This way the second lib may be added to classpath allowing the first to compile.
Try to create .aar files by hand and upload it to repo also by hand.
It's a hack, but maybe it'll work: have you considered to exclude this :GradleTest module? See section 50.4.7
This issue has gone away with the later versions of Gradle and the Android Gradle Plugin. Seems to have just been an early release bug.

Android Gradle library dependency

Hi I have following android project:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
}
repositories {
mavenCentral()
}
android {
buildToolsVersion "17.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
Now I would like to add another dependency: https://github.com/bauerca/drag-sort-listview.
I tried adding
compile 'com.mobeta.android.dslv:drag-sort-listview:0.6.1-SNAPSHOT'
but it doesn't work. How can I add this project as a Gradle dependency?
I saw that there is an option to copy this library as a subdirectory in my project dir. How should I include such a project?
The author of the library has to upload #aar bundle to maven central repository to make it work. As you can see drag sort listview is no longer mainted by author. You can use repo from the community as temporal solution.
repositories {
mavenCentral()
maven {
url 'https://github.com/Goddchen/mvn-repo/raw/master/'
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'com.mobeta.android.dslv:drag-sort-listview:0.6.1'
}
In general case you have to download sources and add them as library to your project.
I like to recommend you to use this library instead.
I thinks https://github.com/ened is thanksfully made the library for gradle and maintained it for a while.
compile 'asia.ivity.android:drag-sort-listview:1.0'
<dependency>
<groupId>asia.ivity.android</groupId>
<artifactId>drag-sort-listview</artifactId>
<version>1.0</version>
</dependency>
http://mvnrepository.com/artifact/asia.ivity.android/drag-sort-listview/1.0
https://github.com/ened/drag-sort-listview

Categories

Resources