Android gradle构建和支持库。

时间:2021-12-06 00:17:06

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:

我有一个项目,它使用了一些其他的库项目(SlidingMenu, ActionbarSherlock),这两个都使用了android支持库,在构建时,我得到了如下内容:

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:

两个库项目都依赖于支持lib:

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

5 个解决方案

#1


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.

更新:这是可能的。

#2


51  

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

现在可以从SDK管理器下载Android支持存储库并替换。

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).

对于使用支持库的所有项目都必须这样做。Android支持存储库会被构建系统自动添加到您的存储库列表中(不确定哪个部分添加了它,还不知道足够的级别)。

Source

#3


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.

基于Xav的答案,如果您有其他依赖于android- supportv4的模块。jar,创建一个包含android- supportv4的库项目。jar并引用这个项目,而不是jar文件。

E.g.:

例如:

Add a project with this structure:

添加这个结构的项目:

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

AndroidManifest.xml:

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:

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')

#4


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:

FYI,我必须加上这个来排除android- supportv4。在我的gradle构建中,因为我添加了它作为一个工件:

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

编译fileTree(dir:“libs”,包括:*)。jar”,排除:“android-support-v4.jar”)

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

我创建了构建。在Eclipse的ADT插件中使用项目导出功能的gradle。

#5


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.

如果您的Eclipse类路径包含多个相同的名称/包/jar类,那么ADT将抛出一个异常,比如意外的*异常。在这种情况下,它将遇到LoaderManager类的多个实例。

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.

解决方案:同一个jar库包含两次。检查您的应用程序和所有引用的Android库,确保所有的jar都包含一次。

#1


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.

更新:这是可能的。

#2


51  

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

现在可以从SDK管理器下载Android支持存储库并替换。

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).

对于使用支持库的所有项目都必须这样做。Android支持存储库会被构建系统自动添加到您的存储库列表中(不确定哪个部分添加了它,还不知道足够的级别)。

Source

#3


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.

基于Xav的答案,如果您有其他依赖于android- supportv4的模块。jar,创建一个包含android- supportv4的库项目。jar并引用这个项目,而不是jar文件。

E.g.:

例如:

Add a project with this structure:

添加这个结构的项目:

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

AndroidManifest.xml:

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:

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')

#4


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:

FYI,我必须加上这个来排除android- supportv4。在我的gradle构建中,因为我添加了它作为一个工件:

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

编译fileTree(dir:“libs”,包括:*)。jar”,排除:“android-support-v4.jar”)

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

我创建了构建。在Eclipse的ADT插件中使用项目导出功能的gradle。

#5


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.

如果您的Eclipse类路径包含多个相同的名称/包/jar类,那么ADT将抛出一个异常,比如意外的*异常。在这种情况下,它将遇到LoaderManager类的多个实例。

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.

解决方案:同一个jar库包含两次。检查您的应用程序和所有引用的Android库,确保所有的jar都包含一次。