[英]Android gradle build and the support library
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: 这两个库项目都依赖于support lib:
dependencies {
compile files('libs/android-support-v4.jar')
}
This is now possible by downloading Android Support Repository
from the SDK Manager, and replacing 现在可以通过从SDK Manager下载
Android Support Repository
,然后替换
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支持系统信息库会由构建系统自动添加到您的系统信息库列表中(不确定是哪一部分添加了它,还没有足够的知识。)
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 . 更新 : 现在有可能 。
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-support-v4.jar的模块,请创建一个包含android-support-v4.jar的库项目,然后引用此项目而不是jar文件。
Eg: 例如:
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: 请记住在您的项目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')
FYI, I had to add this to exclude the android-support-v4.jar in my gradle build because I added it as an artifact: 仅供参考,我必须添加它以在gradle构建中排除android-support-v4.jar,因为我将其添加为工件:
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插件中的项目导出功能创建了build.gradle。
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. 如果您的Eclipse类路径包含多个具有相同名称/包/罐子的类,则
ADT
将引发异常,例如UNEXPECTED TOP-LEVEL EXCEPTION
。 In this case it is encountering more than one instance of the LoaderManager
class. 在这种情况下,它遇到了
LoaderManager
类的多个实例。
Solution : You have same jar library included twice. 解决方案:您有两次包含相同的jar库。 Check your application and all referenced Android libraries and make sure you have all jars included exactly once.
检查您的应用程序和所有引用的Android库,并确保您将所有jar都准确地包含了一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.