在 android 项目和库(Android Studio)中使用支持库 [英] Use support library in android projects and library (Android Studio)
问题描述
我正在使用新的 Android Studio,我正在寻找一种方法来使用来自多个项目的支持库.基本上我有一个使用 ActionBarSherlock
的项目,这个项目需要支持库.所以我在这个问题中添加了一个参考.
I'm using the new Android Studio, I'm looking for a way to use the support library from multiple projects. Basically I have a project that uses the ActionBarSherlock
this projects requires the support library. So I added a reference as like in this question.
现在我的问题是我的主项目也使用了支持库,所以我以某种方式将它包含了两次.如果我从两个项目之一中删除该库,我将收到错误,即某些支持库相关的类未知,这对我来说很清楚,但如果我在两个项目中都有参考,我将面临此错误:
Now I have the problem that my main project also uses the support library so I have it includes twice in some way. If I remove the library from one of both projects I'll get errors that some support library related classes are unknown which is clear for me, but if I have a reference in both projects I'll face this error:
Android Dex: [ProjectName] UNEXPECTED TOP-LEVEL EXCEPTION:
Android Dex: [ProjectName] java.lang.IllegalArgumentException: already added: Landroid/support/v4/app/ShareCompat$ShareCompatImplJB;
Android Dex: [ProjectName] at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
Android Dex: [ProjectName] at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
Android Dex: [ProjectName] at com.android.dx.command.dexer.Main.processClass(Main.java:490)
Android Dex: [ProjectName] at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
...
知道如何解决这个问题吗?
Any idea how to fix this?
推荐答案
我通过进入 File > 解决了这个问题项目结构...
并选择 Modules
然后单击模块的 Dependencies
选项卡.然后在库旁边,我将范围从 Compile
更改为 Provided
.
I fixed this by going into File > Project Structure...
and selecting Modules
then click on the module's Dependencies
tab. Then next to the library I changed the scope from Compile
to Provided
.
这意味着模块仍然可以使用它,但它无法编译,因为我假设您已经在主项目中使用了该 JAR.
This means the module can still use it, but it doesn't reach compile, since I assume you are already using that JAR in your main project.
希望这有帮助!
这篇关于在 android 项目和库(Android Studio)中使用支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!