Android gradle build and the support library
Asked Answered
S

5

34

I have a project that uses a few other library projects (SlidingMenu, ActionbarSherlock) and both of these use the android support library, when building I am getting the following:

UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/app/LoaderManager;
    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)

Both library projects have a dependency on support lib:

dependencies {
    compile files('libs/android-support-v4.jar')
}
Storm answered 25/5, 2013 at 4:56 Comment(0)
R
19

Until we have the support library has a repository artifact you cannot include it in more than one library project. You could create a library project that only contains the support library, and have all other libraries depend on it.

Update: this is now possible.

Replica answered 25/5, 2013 at 5:20 Comment(4)
I tried to do that, but I get a number of Could not find element /manifest/application. errors..Ripleigh
Should the library project with the support library be an android-library project?Ripleigh
Oh, I see you mentioned it will be fixed in 0.5. Cool!Ripleigh
Android Studio - always make sure you update the dependencies as well. Many times, the error now is caused by the reference to just be out of date. There will be a warning typically if a newer version is available.Babysitter
I
51

This is now possible by downloading Android Support Repository from the SDK Manager, and replacing

compile files("libs/android-support-v4.jar")

with

compile 'com.android.support:support-v4:13.0.0'

This has to be done for all projects that use the support library. The Android Support Repository is automatically added to your list of repositories by the build system (Unsure of which part adds it, don't know enough gradle yet).

Source

Istanbul answered 28/6, 2013 at 23:31 Comment(7)
Do I need to reimport my project for this to take effect? I tried it but it's not working.Exsiccate
@Exsiccate Potentially but the "Import from Gradle" button in android studio should do the trick. Try building from the command line once and seeing if that fixes it first.Istanbul
@Vitaly: Now that this is possible, consider making this the accepted answer.Jacquie
For some reason the "is automatically added to your list of repositories" part doe snot seem to work for me. Although I've installed the "Android Support Repository" and verified that the "m2repository" directory and needed files are there, resolution fails. I'm building form the command line, BTW.Namely
thanks but the issue persists for me, could you update your answer? I'm using Android Studio 1.0Renteria
@fuzzybee: Huh? Which issue persists. The same one as @sschhuberth? Can you please post your build.gradle somewhere? The android support repository should be added to the list of gradle's repositories via the 'android' or 'android-library' plugin.Istanbul
@Istanbul the issue in the original question. My question including the build.gradle file is here: #27739939Renteria
R
19

Until we have the support library has a repository artifact you cannot include it in more than one library project. You could create a library project that only contains the support library, and have all other libraries depend on it.

Update: this is now possible.

Replica answered 25/5, 2013 at 5:20 Comment(4)
I tried to do that, but I get a number of Could not find element /manifest/application. errors..Ripleigh
Should the library project with the support library be an android-library project?Ripleigh
Oh, I see you mentioned it will be fixed in 0.5. Cool!Ripleigh
Android Studio - always make sure you update the dependencies as well. Many times, the error now is caused by the reference to just be out of date. There will be a warning typically if a newer version is available.Babysitter
W
13

Based in the answer from Xav, if you have other modules that depends on android-support-v4.jar, create a library project which contains the android-support-v4.jar and reference this project instead the jar file.

E.g.:

Add a project with this structure:

- android-support
  - libs
    - android-support-v4.jar
  - AndroidManifest.xml
  - build.gradle

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.example.support.lib">

    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7"/>

<application />

</manifest>

build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}
apply plugin: 'android-library'

dependencies {
    compile files ("libs/android-support-v4.jar")

}

android {
    compileSdkVersion 17
    buildToolsVersion "17"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 7
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
        }

    }
}

remember to include this project in your projects settings.gradle:

include  ':android-support'

now, for each project that requires the support library, instead of

compile files ("libs/android-support-v4.jar")

use the following line:

compile project (':android-support')
Wrench answered 17/6, 2013 at 17:21 Comment(0)
R
2

FYI, I had to add this to exclude the android-support-v4.jar in my gradle build because I added it as an artifact:

compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-v4.jar')

I created the build.gradle using the project export feature in Eclipse's ADT plugin.

Remillard answered 2/12, 2013 at 22:24 Comment(1)
This caused bunch of compiler error due to lack of the lib.Loralyn
P
1

The ADT will throw an exception like UNEXPECTED TOP-LEVEL EXCEPTION if your Eclipse classpath contains more than one class of the same name/package/jars. In this case it is encountering more than one instance of the LoaderManager class.

Solution : You have same jar library included twice. Check your application and all referenced Android libraries and make sure you have all jars included exactly once.

Parada answered 25/5, 2013 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.