In the 3rd answer here:
How do I add a library project to Android Studio?
I found informations about how we can add ABSherlock library to project using gradle in Android Studio. But in this way we use "import module" option which doesn't exist any more in Android Studio 0.4.0. So how should I add ABSherlock or other library now ? (using gradle)
You can add this part to your build.gradle script
dependencies {
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
}
EDIT:
If you are using also the support library you can use it:
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile ('com.actionbarsherlock:actionbarsherlock:4.4.0#aar'){
// Need to specifically exclude this as it is specified in ActionBarSherlock pom
exclude group: 'com.google.android', module: 'support-v4'
}
}
EDIT2:
If you would like to work with abs with a local copy ( I suggest you to use the maven dependency ) you can do this:
-root
-lib
-abs
build.gradle
src
res
-myModule
build.gradle
settings.gradle
In settings.gradle:
include ':myModule', ':lib:abs'
In lib/abs/build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion XX
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.0'
}
Remove the supportV4.jar from your local abs library.
In myModule/build.gradle you should add:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion XX
targetSdkVersion 19
}
}
dependencies {
// Libraries
compile project(':lib:abs')
}
If in myModule/build.gradle you need to use the support library, you should add:
dependencies {
compile 'com.android.support:support-v4:19.0.0'
// Libraries
compile project(':lib:abs')
}
Working with gradle you should prefer to use dependencies in Maven.
However you can use local libraries with this structure above, editing your gradle files.
Related
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.
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
I have a problem and i cannot solve it about 2 days.
I analysed nearly all questions about this error but i cannot handle it.
Here my tree:
MyAppRoot
-MyApp
-libs
-jar4.jar // it does not work, if i don't put here.
-libraries
-actionbarsherlock//library project
-myOwnLibraryTree// project tree
-libs
-myjar1.jar
-android-support-v4.jar
-myjar2.jar
-myjar3.jar
-infiniteloopindicator//library project
Here, i deleted all sup libraries from all projects included library ones. And i put support library at just one place under libs folder in myOwnLibraryTree.
Here, infiniteloopindicator, uses support lib, to do this, i add support lib as a .jar dependency to infiniteloopindicator. And i did it for MyApp,too. There is no compile errors. But i get, dexDebug error.
System message :
Gradle: UNEXPECTED TOP-LEVEL EXCEPTION:
Gradle: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoStubImpl;
Gradle: at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
Gradle: at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
Gradle: at com.android.dx.command.dexer.Main.processClass(Main.java:490)
Gradle: at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
Gradle: at com.android.dx.command.dexer.Main.access$400(Main.java:67)
Gradle: at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
Gradle: at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
Gradle: at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
Gradle: at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
Gradle: at com.android.dx.command.dexer.Main.processOne(Main.java:422)
Gradle: at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
Gradle: at com.android.dx.command.dexer.Main.run(Main.java:209)
Gradle: at com.android.dx.command.dexer.Main.main(Main.java:174)
Gradle: at com.android.dx.command.Main.main(Main.java:91)
Gradle: 1 error; aborting
Compilation completed with 1 error and 0 warnings in 24 sec
1 error
0 warnings
Gradle: Execution failed for task ':MyApp:dexDebug'.
> Could not call IncrementalTask.taskAction() on task ':MyApp:dexDebug'
Here settings.gradle
include ':libraries:infiniteloopindicator',':libraries:actionbarsherlock',':libraries:myOwnLibraryTree', ':MyApp'
Here actionbarsherlock : build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
dependencies {
compile project(":libraries:myOwnLibraryTree")
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Here infiniteloopindicator : build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
dependencies {
compile project(":libraries:myOwnLibraryTree")
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Here myOwnLibraryTree : build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
}
And here is MyApp : build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
}
dependencies {
compile files('/libs/jar4.jar')
compile project(":libraries:infiniteloopindicator")
compile project(":libraries:actionbarsherlock")
compile project(":libraries:myOwnLibraryTree")
}
Things will work better if you include Support library using a Maven-style include statement in all your build.gradle files instead of linking the jar directly. To do that:
In your SDK manager, make sure you have the "Android Support Repository" installed. If you have more than one Android SDK, make sure you've installed it in the right one -- multiple SDKs are a cause of frequent Android Studio confusion.
In all your build.gradle files, put this in your dependencies block:
compile 'com.android.support:support-v4:+'
It should automatically look in the SDK for the support repository, and the Android Gradle plugin will dedup the library if it's depended on in multiple places.
I am attempting to add a directory as a dependency in Android Studio(GameBaseUtils). I have seen other SO answers simply posting the correct gradle configuration for their particular issue, however I don't understand how I can adapt their answers to my situation.
Here is what I did:
Step one: File-> Import Module ->Navigate to directory and select it.
Step Two-: File-> Project Structure-> Modules-> Select my application->Dependencies->Add the module as a dependency to my project.
Now my code doesn't have any red lines indicating an error importing the module. However when I select build I get the following errors:
Gradle: package com.google.example.games.basegameutils does not exist
Gradle: cannot find symbol class BaseGameActivity
Gradle: cannot find symbol variable super
...
Here is the build.gradle file for my application
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
}
}
How can I correctly import this external library and can you please explain how and why your solution works?
so here is how I solved my problem:
instead of adding
dependencies {
compile files('libs/android-support-v4.jar')
compile project(':Module')
}
You have to write:
dependencies {
compile files('libs/android-support-v4.jar', '../Module')
}
the 2 dots say that the Module (or directory) can be found in 1 directory above the actual one. so if you want to access a module which is 2 directories above you just have to write: '../../ModuleName'
You have to add the modules manually to the build.gradle because Android Studio is still in development and doesn't have finished the UI for editing the Project Structure.
If this does not solve your problem try to make it like this: (I would recommend this method. This is how I actually do it)
Examplestructure:
Project
libraries (normal folder)
Module2
Module1
settings.gradle
include ':Module1', ':libraries:Module2'
build.gradle (Module1)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile project(':libraries:Module2')
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 11
}
}
build.gradle (Module2)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 11
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
This should work well now.
To make everything work 100% follow this steps:
delete .idea folder
delete all *.iml files
Restart Android Studio and press Import Project
Select the directory with your gradle project
Import project from external model > Gradle > next > finish
With this steps everything should work well. If there are any problems just tell me :)
Do not add modules through the Studio interface. Always make the changes directly in build.gradle and then reimport into Studio.
Also, update the plugin dependency to com.android.tools.build:gradle:0.4.+ to get the latest 0.4.* version.
I've never used Gradle before so I'm completely lost!
I've added SlidingMenu as a library and I have access from my project to all the SlindingMenu stuff, but trying to compile will give me this error:
Gradle: package com.jeremyfeinstein.slidingmenu.lib does not exist
I'm using Android Studio (so IntelliJ) and this is my gradle.build
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
}
}
Thanks in advance
Assuming you have added SlidingMenu.jar into libs folder, right click on it -> Add as library. Then change in gradle.build:
Before:
dependencies {
compile files('libs/android-support-v4.jar')
}
After:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
This will include all your jar files.
I had the same problem. Adding sliding-menu-lib from with gradle-build as android library did help me.
My project structure is as:
-MyDemoProject
-build.gradle
-settings.gradle
--MyDemo
--build.gradle
--libs
---sliding-menu-lib
----res
----src
----AndroidManifest.xml
----build.gradle
--src
To make all the stuff working your settings.bundle should have this contents:
include ':MyDemo' ':MyDemo:libs:sliding-menu-lib'
There is a trick here, which allows you avoid errors while building project with gradle using Android Studio, as according to Android Tools Manual you should use ':libs:sliding-menu-lib' but that does not work due to issue with relative projectDir paths.
Your MyDemo/build.gradle should contain dependencies like:
dependencies {
compile 'com.android.support:support-v4:18.0.0'
...
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':MyDemo:libs:sliding-menu-lib')
}
And your sliding-menu-lib/build.gradle should be like:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 14
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
Most important part deals with sourceSets section as you may not want change sliding-menu-lib file structure (non-default for current gradle)
I added all of my previous libraries using the default import from source tool. For SlidingMenu I used the import with Maven then deleted all of the Maven dependancies from the Project Settings for SlidingMenu and reimported the Support libraries. This seemed to clear most issues up for me.
If the module is just a library and not a stand-alone app, it's gradle should contain
apply plugin: 'android-library'
instead of
apply plugin: 'android'
You can Sync Project with Gradle Files:
Tools -> Android -> Sync Project with Gradle Files
Recently found better solution for SlidingMenu separately:
You can add SlidingMenu as generated #aar file if you do not need to make any changes to it. Just use https://github.com/jzaccone/SlidingMenu-aar and make changes as in Readme file there.
Be careful with order of repos. This one should be above mavenCentral()