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')
}
Vitaly Babiy
  • 6,114
  • 4
  • 26
  • 24

5 Answers5

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

abhishekmukherg
  • 754
  • 1
  • 4
  • 12
  • Do I need to reimport my project for this to take effect? I tried it but it's not working. – dannyroa Jul 29 '13 at 19:22
  • @dannyroa 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. – abhishekmukherg Aug 01 '13 at 21:13
  • 1
    @Vitaly: Now that this is possible, consider making this the accepted answer. – Jonik Jan 05 '14 at 13:39
  • 1
    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. – sschuberth Aug 18 '14 at 11:53
  • thanks but the issue persists for me, could you update your answer? I'm using Android Studio 1.0 – ericn Jan 02 '15 at 06:12
  • @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. – abhishekmukherg Jan 02 '15 at 18:24
  • @abhishekmukherg the issue in the original question. My question including the `build.gradle` file is here: http://stackoverflow.com/questions/27739439/unexpected-top-level-exception-multiple-library-projects – ericn Jan 04 '15 at 03:02
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.

Community
  • 1
  • 1
Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
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')
rafaello
  • 2,385
  • 1
  • 18
  • 16
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.

kenyee
  • 2,309
  • 1
  • 24
  • 32
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.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107