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.
Related
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
}
I am working on an Android project, which used Gradle as mentioned below.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:darkmoon:darul-android:vitamio:vitamio')
compile project(':Dev:adt-bundle-mac-x86_64:sdk:extras:google:google_play_services:libproject:google-play-services_lib')
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
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')
}
}
But when I build it, keep receiving this error: "Gradle DSL method not found", and it pointed to the
following line:
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:epsilonmobile:darul-android:vitamio:vitamio')
compile project(':Dev:adt-bundle-mac-x86_64:sdk:extras:google:google_play_services:libproject:google-play-services_lib')
}
Apologize if this question is a bit noob, I'm new to both Gradle and Android Studio
A classpath configuration is only available for buildscript dependencies. You need to get rid of the line classpath 'com.android.tools.build:gradle:0.12.+' in the top-level dependencies block. (Gradle plugins need to be declared under buildscript { dependencies { ... } }.)
In a gradle script, the buildscript is a special section where you can declare dependencies of the build script itself (i.e. binaries required by the build process).
The gradle build process is nothing more than a java process and so it supports normal classpath dependencies.
com.android.tools.build:gradle:0.12.+ identify a binary required by the build process (it contains code able to understand/execute the android section of the build script).
The android apk that will be build by this script don't needs the binary com.android.tools.build:gradle:0.12.+ to run on your android device (i.e. the apk is of course already build when it run on the device) : there is no reason to declare it again in the top level dependencies
(those are the dependencies required by your app)
I am migrating my legacy project from Eclipse to Android studio. I did the following things:
Export and generate gradle build files in eclipse
Imported the Gradle.build file in Android Studio
Changed my gradle version dependencies to: classpath 'com.android.tools.build:gradle:0.9.+'
My root folder of the project contains 3 library projects: Actionbarsherlock, Authbahn and library. My root folder also contains the main project called JohelpenHulpverlener.
After resolving a lot of errors I get the following error:
Error:A problem was found with the configuration of task ':checkDebugManifest'.
> File 'C:\android\jeugdformaat\JohelpenHulpverlener\AndroidManifest.xml' specified for property 'manifest' does not exist.
My root build.gradle looks like this:
// 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.9.+'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile project(":actionbarsherlock")
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
My build.gradle in the main project looks like this:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':library')
compile project(':actionbarsherlock')
compile project(':autobahn')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
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')
}
}
I am trying to understand this, hopefully someone can help me out here!
You've got a bunch of stuff in your root-level Gradle build file that's making the build system think there's an Android module there (it's not; it's a subdirectory below inside JohelpenHulpverlener) , and it's failing because it's not finding the manifest.
I think if you take a lot of stuff out of your root-level build.gradle file and replace it with the default version for new projects, which is this:
// 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.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
it will work.
Make sure you have tags on the outside like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
...
</manifest>
In build.gradle file specify package name like
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 11
targetSdkVersion 19
versionCode 27
versionName "1.5.6"
}
}
applicationId "com.myapp" Here "com.myapp" is packagename of my app.
I hope this will help you.
I had the same problem when migrating eclipse project to android studio.
The problem is that the root Gradle build file created by android studio is wrong and it should be like this :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
Update : It seems the problem has been fixed by Android Studio's latest version 1.4, now we can easily import eclipse project into studio
Build->Rebuild solved it for me.
I know this is an old post, but I helped a friend who is new to programming with this problem last night. Your suggestions were examined but did not apply. Their last action before the error was to add a list view layout to the project. I asked them to delete it and the errors vanished after a few moments.
I have not found any other post with this scenario and this conclusion so I thought sharing it here might help someone else.
Conclusion: Adding a new component to the wrong place may be another reason for such an error.
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
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.