Use support library in android projects and library (Android Studio) - android

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?

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.
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.
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). hit F4 to open the module settings.
Make sure that you are not using the same dependencies twice in actionbarsherlock and your project.
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).
Finally, if you're like me, make sure you didn't accidentally make actionbarsherlock dependent on itself, thus loading the library twice THAT way! >.<
Take a look at these images showing my module preferences:
http://imgur.com/a/JupWp
Also, don't forget to make sure libs isn't hiding any of the same ones!

Did this use to be an Eclipse project? If so try going into Eclipse, clean the project, exit, rebuild in 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:
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}

Related

Using cwac-camera via JARs

I'm using the commonsguy cwac-camera library, as per the demo-layout example, documented in "Working directlly with cameraview".
All is fine referencing camera/ and camera-v9/ as Android library projects in source form (I need Android 2.3 compatibility, that's what camera-v9 is for).
When I switch to using the library via JARs:
- cwac-camera-v9-0.6.8.jar only includes CameraFragment and BuildConfig classes, so I need also cwac-camera-0.6.8.jar with the other classes.
- including both JARs causes the following self-explaining error when running the project (not at compile time) Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/commonsware/cwac/camera/BuildConfig;
I could just use the source as library project, or use Gradle, but I want to know if this is a bug to open an issue on Github, or if I'm doing something wrong.
To replicate the error, just clone the demo-layout example add both .jar files to libs folder, and run the project.
No, this appears to be my fault. They must have changed something in the Gradle build process that I am using to create the JARs. I will try to fix this tomorrow. In the meantime, you could go into the cwac-camera-v9 JAR and try removing the classes in com.commonsware.cwac.camera, leaving only those classes incom.commonsware.cwac.camera.acl.
My apologies for this, and thanks for pointing it out!

Project Building successfully in Eclipse but not in Intellij

I have an Android project which uses the following library projects along with using the Android Support v4:
ActionBarSherlock
SlideMenuLib
Facebook
Google Play services
My project is working fine in eclipse and im able to launch the porject directly from the IDE but when I try to import and run the same project in IntelliJ (13.0.2), it gives me the following error
Android Dex: [Splash] com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
I know from the error that somehow IntelliJ is picking up the Supportv4 library multiple times from all the three places but any attempt to play around with the Support library imports, the project stops compiling saying com.android.support.v4 is missing in both Eclipse and Intellij.
Fixed the problem. Thanks to the solution found here: Intellij: android-support-v4.jar in included in actionBarSherlock and Project => IllegalArgumentException
I changed the scope of the support library in ActionbarSherlock and SlideMenuLib to 'provided' and that did the trick.
You have problems with your imports probably your project are using the same two libraries (older and new) and do not know with one should be used.
Check your Libs files, lib-project and references.
Try to: build path -> Order and Export, Uncheck the jar that is the same that the imported library uses.

Android Dex: UNEXPECTED TOP-LEVEL EXCEPTION: already added when i rebuild the project

i know that this problem is with libraries. In my project i want to use sign in with facebook, and i need to add facebooksdk.jar and add facebook project like library. But i get errors. I'm working in Intellij IDEA. There you can see my exceptions and project properties.

Jar mismatch scenario

I'm encountering a lot of jar mismatch errors lately and I'd like to avoid them in the future.
So, here's my question: suppose I have an app that uses 2 library projects, both of which use the v4 support library jar. In total, I have 3 jar files. What can I do to avoid the jar mismatch error?
All I've read so far is the case where you have a project and a single library project. In this case, I think your supposed to delete the jar file in the project and keep the one in the library project.
This is quite common if your project references several libraries, and each library references the same other library.
An example of this, your app is referencing the android support library v4, and you have 2 libraries which are also referencing the support library v4.
If you update your apps reference to the support library, but you don't update the support library v4 in your other libraries you will see this error.
The best thing is on each referenced library and your app, right click on the project > android tools > add support library. This will get the latest version and you should no longer see these errors.
If you're facing mismatch issue then simply copy one of them and paste in remaining libraries. E.g. as you've 3 v4 support library jars then copy one of them and paste at place of remaining two jars.
Try to understood by my example I used Map and Facebook in My project now Facebook,Map and my project have Android support jar so i removed Android Support jar from Facebook,My project but remain jar on Map,now i'm addded Android-Map-Extension as library project on Facebook And finally add Facebook library project on My project .....

Building Android project with ant having a library project dependency on another library project

I have maybe this not so common setup:
(> = dependency)
Android project > Android library project 1 > Android library project 2
So I have an Android library project which has a dependency to another library project.
When I'm building project in Eclipse everythings works fine but I cannot get my build working with Ant.
First Ant compiles Android library project 2 which generates a classes.jar and puts this file in the bin folder.
Then Ant tries to compile the Android library project 1 but then I'm getting errors becouse it is missing classes from Android library project 2.
Well this is not so weird becouse the jar file is not included in the libs folders.
But in project.properties i've made a dependency to the library project 2 so why does Ant not copy the classes.jar to the libs folders of library project 1?
Well I can think of a solution to use an Ant task to copy the file to the libs folder, but then I have to modify the build.xml which I do not prefer.
** EDIT
The problem is that the R class is missing, when I look in classes.jar this java file does not contain the R class. So my solution would proberly not work.
This behaviour was caused by a change in R17 of the build tools: http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
In a nutshell: R files for libraries are no longer packaged in the classes.jar for that library. However, since the pareent.R for the parent-library (project1 in your example) also contains the resource-references for the 'child' library (project2 in your example), you don't have to refer to the child-R anyway.
Replace all project2.R-import statements in project1 with project1.R import statements and you should be fine.
For ant to compile add dependency in ant.properties.
eg:
android.library.reference.1=../path/to/library
This sounds like a very brittle setup - you may have good reason for this, but could you instead decouple the libraries dependence on each other?
For example; implement a bridge pattern to translate the calls between both libraries, and make the calling Android project attach them. This way, you have no code in either library that depends on the other, and the only project that needs to handle dependency setup is your main project.
One of the main reasons for using a library is to make the code re-usable, this approach ensures someone (you, a colleague, your successor...) could plug in just one library and create their own implementation of the other one.
Here is another good article on applying the bridge pattern in Java: http://java.dzone.com/articles/design-patterns-bridge
Well the problem was that the R class was missing.
So i removed the R class dependency between the two library projects.
I don't know if this is fixable but i think it is bad practice any way.
Without this dependency Ant builds fine.
Old question, but like me, others might be banging their head on this...
The official answer is "it cannot be done", specifically:
At build time, the libraries are merged with the application one at a time, starting from the lowest priority to the highest. Note that a library cannot itself reference another library and that, at build time, libraries are not merged with each other before being merged with the application.
(extracted from the official documentation: "Referencing a Library Project").
Which means that anything goes, as there is no "clean" way to do it with the tools (and dirty methods are in order).
Hope it helps

Categories

Resources