Android gradle build: duplicate files during packaging of APK LICENSE.txt - android

I am trying to build an Android test using the following gradle build file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
dependencies {
instrumentTestCompile "junit:junit:4.+"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
instrumentTest.setRoot('tests')
instrumentTest {
java.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
resources.srcDirs = ['tests/src']
}
}
}
When run I get the following error:
Error: duplicate files during packaging of APK ... Path in archive: LICENSE.txt
Origin 1: ....gradle/caches/artifacts-24/filestore/junit/junit/4.11/jar/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar
Origin 2: ....gradle/caches/artifacts-24/filestore/org.hamcrest/hamcrest-core/1.3/jar/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
:packageTest FAILED
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':packageTest'.
Duplicate files at the same path inside the APK: LICENSE.txt

Junit v4.5 has packaged all necessary dependencies into the JUnit jar. Hence no need for hamcrest.jar, and no resulting double LICENSE.txt file.
just change dependencies to:
instrumentTestCompile "junit:junit:4.5+"
The basic issue still remains - android not accepting two files names the same in its build tree.
This is a good workaround, though.

I noticed this commit comment in AOSP, the solution will be to exclude some files using DSL. Probably when 0.7.1 is released.
commit e7669b24c1f23ba457fdee614ef7161b33feee69
Author: Xavier Ducrohet <--->
Date: Thu Dec 19 10:21:04 2013 -0800
Add DSL to exclude some files from packaging.
This only applies to files coming from jar dependencies.
The DSL is:
android {
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
}

Related

What gradle task pulls dependencies from remote repositories?

I am making the switch to Gradle for my Android project. I have imported the project into Android Studio from Eclipse and have wrapped it using Gradle 1.8.
My assumption was Gradle handled dependencies the way as Maven, that is if you specify a dependency that does not exist in your local repository it will pull it from the remote repository. Android Studio is not pulling in my dependencies. I attempted to run ./gradlew androidDependencies --debug but I do not see any downloading of the dependencies however they are being acknowledged they are there and no warnings/errors are thrown.
11:11:36.833 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.ivyresolve.CachingModuleVersionRepository] Using cached module metadata for module 'com.nostra13.universalimageloader#parent;1.8.6' in 'MavenRepo'
11:11:36.834 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.ivyresolve.UserResolverChain] Using module 'com.nostra13.universalimageloader:parent:1.8.6' from repository 'MavenRepo'
11:11:36.855 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.DependencyGraphBuilder] Visiting configuration com.nostra13.universalimageloader:parent:1.8.6(default).
11:11:36.857 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.resolveengine.DependencyGraphBuilder] Attaching com.nostra13.universalimageloader:parent:1.8.6(default) to its parents.
Gradle does take the dependency and create metadata from it in this location
/home/wil/.gradle/caches/artifacts-26/filestore/com.nostra13.universalimageloader/parent/1.8.6/pom/41669498c2bec505b61772b083cab341653cb86b
Interestingly I have found that if I have the maven dependencies locally it will pull it into Android Studio as an External Library and it will also add the jar to the gradle cache filestore it will just not do this for remote dependencies I specifiy.
Here is my build.gradle
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.3'
}
}
repositories{
mavenCentral()
}
apply plugin: 'android'
dependencies {
compile 'com.nostra13.universalimageloader:parent:1.8.6'
}
android {
compileSdkVersion 19
buildToolsVersion "19"
defaultConfig {
minSdkVersion 14
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']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
I am running Android Studio 0.3.6, Ubuntu 13.04, Gradle 1.8, JDK 6
What gradle command should I run that should pull down remote dependencies from a maven repository? Am I missing a configuration setting somewhere that allows downloading of dependencies?
By using gradle dependencies command line, you can pull down all everything specified in your build.gradle
Remote dependencies are downloaded automatically as soon as a task needs them. If you, for example, compile your project's code, compile dependencies will be downloaded, unless they already exist in Gradle's dependency cache. Out of the box, Gradle doesn't come with a task that merely downloads all dependencies without doing anything with them, but it would be easy to write one.

Gradle and multiple projects with Roboguice dependency

I'm trying to migrate an old IntelliJ project to use gradle. However, assembleDebug fails during the dx step:
java.lang.IllegalArgumentException: already added: Lcom/google/inject/AbstractModule;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
My project is split into two subprojects: a main project and a library project. Both these project have Roboguice and Guice as dependencies.
I tried Xav's suggested workaround for including the support library in multiple projects as mentioned in this answer. The workaround should probably not even be necessary, given that roboguice/guice are both picked up from maven central. I created a dummy library project that's the only project that depends on roboguice/guice. I made it so that my main project and the (true) library project both depend on this dummy project. However, I get the same error.
How can this be fixed?
settings.gradle in the root directory:
include 'MainApp'
include 'library'
include 'common-library'
build.gradle in the root directory:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
allprojects {
version = '1.0'
repositories {
mavenCentral()
}
}
apply plugin: 'android-reporting'
build.gradle in the main project and the real library project:
apply plugin: 'android'
android {
compileSdkVersion 15
buildToolsVersion "17.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
dependencies {
compile project(':library') // only in main, not in real library
compile project(':common-library')
}
build.gradle in the dummy library project:
apply plugin: 'android-library'
android {
compileSdkVersion 15
buildToolsVersion "17.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
dependencies {
compile 'org.roboguice:roboguice:2.0'
compile 'com.google.inject:guice:3.0'
}
You're right that the dummy library isn't necessary since you're using Maven. If you look at the .pom for Roboguice, it lists Guice as a dependency with the "no_aop" classifier. However, you're also listing Guice explicitly without the classifier. My guess is that Maven is pulling in both versions, which results in the dexing merge conflict.
Try removing compile 'com.google.inject:guice:3.0' from your Gradle build file and see if it builds.
Ok, guys I founded!
To solve this issue you have use 'exclude module' on build.gradle for library-project.
Here is my examples:
compile ('oauth.signpost:signpost-commonshttp4:1.2.1.2') {
exclude module: 'httpcore' }

Android Studio: Gradle build fails -- Execution failed for task ':compileDebugAidl'

After changes to source and building with gradle in Android Studio (I/O preview) AI - 130.677228 the build fails with the following error:
Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileDebugAidl'.
> No signature of method: com.android.ide.common.internal.WaitableExecutor.waitForTasks() is applicable for argument types: () values: []
Possible solutions: waitForAllTasks()
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
The second time running a build the build will succeed.
Using a gradle wrapper with version 1.6
This really sucks because it does a long build (non-incremental) after it fails the first time.
Is there a way to not have this failure?
EDIT to include build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
task wrapper(type: Wrapper) {
gradleVersion = '1.6'
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion "Google Inc.:Google APIs:17"
buildToolsVersion "17"
defaultConfig {
minSdkVersion 11
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']
}
instrumentTest.setRoot('tests')
}
}
Link to issue on Google Code: https://code.google.com/p/android/issues/detail?id=56158
I solved this issue by setting buildToolsVersion in my build.gradle file to match the latest version of the Android SDK Build-tools in the SDK manager.
In my case, I have the Android SDK Build-tools version 22.0.1 installed, so I set buildToolsVersion accordingly:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
...
After making that change, my app builds uneventfully.
I'm not sure how this is possible. It looks like you have a mismatch between the Gradle plugin itself and its dependencies that provides the WaitableExecutor class.
However you mention Gradle 1.5 and this is a problem.
The plugin version 0.3 was compatible with Gradle 1.3-1.4
The new version release last week, 0.4 is compatible with Gradle 1.6+
Make sure you use 0.4 and the new Gradle version.
I was facing the same issue "Failed to execute the task: compileDebugaidl aidl/debug/".
I saw further in Gradle Console for the specifics and it read as below:
Failed to capture snapshot of output files for task 'prepareComAndroidSupportAppcompatV72103Library' during up-to-date check.
Could not remove entry '/Users/..../.../.../..../build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3' from cache outputFileStates.bin (/Users/..../..../..../.gradle/2.2.1/taskArtifacts/outputFileStates.bin).
I resolved it by deleting the outputFileStates.bin file from the terminal and allowed Gradle to recreate it.
Hope it helps somebody.
Add:
compileSdkVersion 17 to your buid.gradel file (below).
And use version 3 of the plugin: com.android.tools.build:gradle:0.3 (or higher for future questions,etc)
Edit: reference project I just created. Builds, signs,etc https://github.com/yegdroid/gradle_demo
//
// A basic Android application that follows all the conventions
//
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
}
}
apply plugin: 'android'
android {
testBuildType = "debug"
defaultConfig {
versionCode = 1
versionName = "0.1"
minSdkVersion = 9
targetSdkVersion = 17
compileSdkVersion 17
buildConfig "private final static boolean DEFAULT = true;", \
"private final static String FOO = \"foo\";"
}
buildTypes {
debug {
packageNameSuffix = ".debug"
buildConfig "private final static boolean DEBUG2 = false;"
}
}
aaptOptions {
noCompress "txt"
}
sourceSets {
main {
manifest {
srcFile 'AndroidManifest.xml'
}
java {
srcDir 'src'
}
res {
srcDir 'res'
}
assets {
srcDir 'assets'
}
resources {
srcDir 'src'
}
}
}
}
Add the code below into your build.gradle file. This works for me.
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Please, try checking "Use default gradle wrapper" option in the Project-level settings.
Android Studio --> File --> Settings --> Build, Execution, Deployment --> Build Tools --> Gradle
Like to register my problem and solution here since it is almost relevent to the issue posted that if someone stumbles across the error could overcome it quickly.
I faced a similar issue with Failed to execute the task: compileDebugaidl aidl/debug/.. Access is denied ...
I overcame the issue by deleting the build directory and rebuilding it again[I'm using the Gradle 0.14.4]
This works for me
edit in your project build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
//delete this line below
classpath 'com.android.tools.build:gradle:1.0.1'
//add this line below
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
The missing AIDL something is google android studio problem to not update major gradle dependencies class path.
Fix:
- open project gradle file (no app, project !)
- replace:
classpath 'com.android.tools.build:gradle:1.0.1' or whatever
with
classpath 'com.android.tools.build:gradle:1.3.1'
If you can not compile a time before, compilable project, the google cat & dog are not sleeping and theire making changes, updates, therefore you have to wake up and made changes where they forget to.
And gradle is quite unstable project and buggy.
Can i see gradle (error filtered) output? (toolwindow gradle, gradle tab)
Looks like there is problem with functions inside the aidl files, which are mostly for outside application interface, & services.
Etc to transfer data to widget, or if you need data transfer between two applications.
Second posibility is two libraries with the same aidl structure, just one function is differrent, than one, or you are using the same library twice.
Another reason i newer saw with this message

Gradle can't find dependency (Android support library)

I have a problem that Gradle can't find my dependency (Android support library).
My build.gradle looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/FlurryAgent.jar')
compile group: 'com.google.android', name: 'support-v4', version: 'r7'
compile files('libs/YouTubeAndroidPlayerApi.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 11
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']
}
instrumentTest.setRoot('tests')
}
}
When I build (on commandline, no IDE) I get the following message:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'AndroidCalculator'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':compile'.
> Could not find com.google.android:support-v4:r7.
Required by:
:AndroidCalculator:unspecified
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Why am I not allowed to add the Android Support library like this?
You have declared a repository dependency, but haven't declared a repository. Hence the dependency cannot be resolved. (Repositories/dependencies in the buildscript block are strictly separated from repositories/dependencies in the main build script.)
http://pastebin.com/FmcCZwA5
This paste is elaborate project with AndroidAnnotations, Dagger, Jackson and Robolectric.
all you need is add
repositories {
mavenCentral()
}
replace
compile group: 'com.google.android', name: 'support-v4', version: 'r7'
with (line 44 of the code linked above)
compile 'com.android.support:support-v4:18.0.+'
Gotchas: Last bit will works on Android Studio 0.2+ only if you had a fresh install. Since 0.2 Studio is shipped with its internal m2 repo to provide support and google api libraries so if you upgraded from previous versions your SDK doesn't have it.
also make sure local.properties file is present in root folder and sdk.dir points to SDK
You need to add additional dependency in dependencies tag. If you have android-support-v4.jar library in your libs folder, try to add code listed below:
dependencies {
compile files('libs/android-support-v4.jar')
}

Gradle build failing due to androidannotations plugin

I am integrating andoirdannotations into a gradle build process into a generic android project. When I attempt to build the project with the addition of apply plugin: 'androidannotations I get the following failure:
$ gradle clean
FAILURE: Build failed with an exception.
* What went wrong:
Main Manifest missing from /tmp/RunTest/src/main/AndroidManifest.xml
Note(1): I want to maintain the generic android project structure.
Note(2): I have successfully build/cleaned this project without the androidannotations plugin
build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
classpath 'net.ealden.gradle.plugins:gradle-androidannotations-plugin:0.3.0'
}
}
apply plugin: 'android'
apply plugin: 'androidannotations'
repositories {
mavenCentral()
}
android {
def target = 'android-21'
def androidAnnotationsVersion = '2.7.1'
sourceSets {
main {
manifest {
srcFile 'AndroidManifest.xml'
}
java {
srcDir 'src'
}
res {
srcDir 'res'
}
assets {
srcDir 'assets'
}
resources {
srcDir 'src'
}
}
test {
java {
srcDir 'tests/src'
}
}
}
}
So, I've bailed out on trying to maintain the generic android project structure and forced the project directory structure into the structure noted here: http://tools.android.com/tech-docs/new-build-system/using-the-new-build-system.
As expected this resolved my Main Manifest missing from /tmp/RunTest/src/main/AndroidManifest.xml however I am still getting no joy.
Now I am getting:
MyBox:RunTest $ gradle clean
Download http://repo1.maven.org/maven2/net/ealden/gradle/plugins/gradle-androidannotations-plugin/0.3.0/gradle-androidannotations-plugin-0.3.0.pom
Download http://repo1.maven.org/maven2/org/gradle/api/plugins/gradle-android-plugin/1.1.0/gradle-android-plugin-1.1.0.pom
Download http://repo1.maven.org/maven2/net/ealden/gradle/plugins/gradle-androidannotations-plugin/0.3.0/gradle-androidannotations-plugin-0.3.0.jar
Download http://repo1.maven.org/maven2/org/gradle/api/plugins/gradle-android-plugin/1.1.0/gradle-android-plugin-1.1.0.jar
FAILURE: Build failed with an exception.
* What went wrong:
Cannot add task ':processTestResources' as a task with that name already exists.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 9.28 secs
Looks like the androidannotations plugin applies the full Java gradle plugin that creates a processTestResources task which collides with the task we are trying to create.
It's unfortunate that we used the same name, but really you do not want to apply the full Java plugin to a project using the android plugin.
The android plugin only applies the base Java plugin (which provides the ability to create Java compilation tasks but does not create the default ones created by the full Java plugin).
To be honest, our plugin does things very very differently from the regular Java and plugins that are meant to extend Java projects won't work on android projects (yet).
The first error you got looks like an issue we've had (but are having trouble reproducing) where some exception got thrown but Gradle ignored it, skipped evaluating the rest of the DSL, and kept going to trying to build. Since the sourceSets remapping was skipped, it's looking in the wrong place for the Manifest.
To integrate Android Annotations into your project add the following to your project's build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
// other classpathes like gradle here, in my case
classpath 'com.android.tools.build:gradle:1.1.0'
// APT Dependency for annotation processing
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
Then apply not androidannotation as plugin but 'android-apt' in your module's build.gradle
// apply apt plugin from global gradle file
apply plugin: 'android-apt'
and also add the following to the build file:
// Tell apt where to find sources
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
// adjust the path to your module here
resourcePackageName 'de.company.app'
}
}
dependencies {
// Android Anotations for clean and readable code
apt "org.androidannotations:androidannotations:3.2"
compile 'org.androidannotations:androidannotations-api:3.2'
}
Then you should be able to use Android Annotations in your project.
To keep an eclipse based project structure you can adjust the folders as you already did:
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
you are using wrong gradle android plugin (com.google.tools plugin developed by Google)
androidannotations plugin uses com.jvoegele.gradle.plugins.android plugin and you don't have to add it to build.gradle as androidannotations plugin will fetch it itself.
Use ADT 21 or earlier as gradle-android-plugin cannot understand version which is not integer number (1.2.2-SNAPSHOT can)
As an example you can look at the build.gradle file from
https://github.com/ealden/android-annotations-idea-test/blob/master/build.gradle

Categories

Resources