简体   繁体   English

在android项目和库中使用支持库(Android Studio)

[英]Use support library in android projects and library (Android Studio)

I'm using the new Android Studio, I'm looking for a way to use the support library from multiple projects. 我正在使用新的Android Studio,正在寻找一种使用多个项目中的支持​​库的方法。 Basically I have a project that uses the ActionBarSherlock this projects requires the support library. 基本上,我有一个使用ActionBarSherlock的项目,该项目需要支持库。 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? 任何想法如何解决这个问题?

I fixed this by going into File > Project Structure... and selecting Modules then click on the module's Dependencies tab. 我通过进入“ File > Project Structure...并选择“ Modules然后单击模块的“ Dependencies选项卡来修复此问题。 Then next to the library I changed the scope from Compile to Provided . 然后在库旁边,将范围从Compile更改为Provided

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. 这意味着该模块仍然可以使用它,但是无法编译,因为我假设您已经在主项目中使用了该JAR。

Hope this helps! 希望这可以帮助!

I had a similar issue happen to me. 我也遇到过类似的问题。 Select actionbarsherlock in the project tab/bar/whatever (on the left, so you can see the project tree). 在项目标签/栏/任意actionbarsherlock中选择actionbarsherlock (在左侧,因此您可以看到项目树)。 hit F4 to open the module settings. 点击F4打开模块设置。
Make sure that you are not using the same dependencies twice in actionbarsherlock and your project. 确保在actionbarsherlock和项目中两次没有使用相同的依赖项
Also make sure that you're using the version of the support library that's in the android-studio sdk ( android-studio/sdk/extras/android ). 还要确保您使用的是android-studio sdk中的支持库版本( android-studio/sdk/extras/android )。

Finally, if you're like me, make sure you didn't accidentally make actionbarsherlock dependent on itself, thus loading the library twice THAT way! 最后,如果您像我一样,请确保您不会意外使actionbarsherlock依赖于自身,从而以这种方式两次加载了库! >.< >。<

Take a look at these images showing my module preferences: http://imgur.com/a/JupWp 看看这些显示我的模块首选项的图像: http : //imgur.com/a/JupWp 动作栏夏洛克在此处输入图片说明

Also, don't forget to make sure libs isn't hiding any of the same ones! 另外,不要忘记确保libs没有隐藏任何相同的库!

Did this use to be an Eclipse project? 这曾经是Eclipse项目吗? If so try going into Eclipse, clean the project, exit, rebuild in IntelliJ. 如果是这样,请尝试进入Eclipse,清理项目,退出,然后在IntelliJ中重建。 I believe this is a state-issue, not a configuration issue. 我相信这是一个状态问题,而不是配置问题。

Instead of importing android support library as jar, I changed the libraries (in my case facebook sdk) dependencies in build.gradle to: 我没有将android支持库导入jar,而是将build.gradle中的库(在我的情况下为facebook sdk)依赖项更改为:

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM