Debugging external libs in Android - android

I have several .jar files I want to reference into my Android project, and I have the sources for them. The point is that in order to build .jar's from sources, I have to use Maven - the build process is pretty complicated, and as I am new to both Android and Maven, I am trying to reuse the script for building these libraries that was written before me, and do not add the sources directly.
So my intentions were pretty simple:
Build the .class files via Maven script
Compile .jar from these .class files via jar tool in the command line
Reference these libraries from my android project.
But as soon as I copy .jar files to the libs folder in the android project - I cannot add sources on them - and I cannot debug those.
Looks like this issue has been several times on SO here or here. Still nothing works for me. .properties file doesn't seem to be recognized by Eclipse, when I go to Java Build Path - the path to the sources is marked as (None) and it is not modifiable, and if I reference the libraries like it was before ADT17 (adding a custom directory like lib with no s in the end, and referencing these jars as external libraries) - this fixes the debugging, but the compiler doesn't seem to include all the source code due to this
I've read that should be fixed in ADT20, but I am using ADT21 and still no luck. Any usable workaround of this?

Finally did it! That still looks like a bug in ADT. The only solution I found is to reference the libs as it was before ADT17 (put them into the separate lib folder without 's', add them to build path, and mark the checkboxes in the Configure Build Path -> Order and Export).
The thing I was missing is actually marking the checkboxes - for some reason when you change anything in that dialog, the checkboxes are becoming unmarked. I guess I just didn't notice that...

Related

How to include external libs in Android source controlled project?

How should external libraries be included into Android projects?
I see this documentation from Google:
http://developer.android.com/tools/support-library/setup.html#libs-with-res
...which says they should be kept outside the source tree for the project, and referenced as dependencies.
The guide for Facebook libraries says the same thing:
https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/
What about when the project is going into source control, and will be worked on by multiple developers? Is it possible to be sure other developers will have the correct versions of libraries if they're not included in source control?
It seems as though it might be better to check in the whole tree of these external libraries under say an "external" folder in the project and then reference them as libraries from there? The above links don't say this is wrong, but is there any reason not to do that?
I could not find anything against this approach, but maybe my search skills are off.
Thanks!
You have basically tree options (referring to git):
Putting the source or binaries into your git repository.
You can create/clone extra repositories and link these as submodule into your main repository.
Use gradle/android-studio to maintain remote binary dependencies.
In my opinion, option 3. is the best. It speeds up build time and reduces the date saved in your internal repository. Referencing most open source projects, googles libraries and even the Facebook API is just a one liner in your build.gradle file.
For internal libraries or anything not uploaded to some maven repository, you can create a local maven repository and link that.
And in the end, you have the option 2. to create a library submodule within git and gradle to handle it efficiently.
If you want to stick to eclipse + ant, try 2. first.
At least ant will work out of the box for building all things.
Setting up eclipse is a bit more difficult but can be done.
Option 1. is easy to implement, but It might get messy at some point.
Copy jar file in android project libs forlder and right click on jar file and click on bulid path-> add to build path.
If you want to add jar file then copy your jar file and put in to libs folder, and if you want to add external library then import your library project go to project properties and select android tab and add external library with add button.

Exporting .jar to use in Unity with 3rd-party libs included: NoClassDefFoundError

This seems to be a common issue when exporting jars with Eclipse. Now, my context.
I'm attempting to write Java plugins to use in Unity applications. When I did a simple plugin with no external libraries (except the classes.jar so I can call a UnityPlayerActivity), it worked pretty well. "Now, let's use some utils libraries". So I wrote a plugin which uses Jackson libraries to parse and process JSON data, so I can serialize it to a Java object, or just pass RAW data, then build a Unity (C#) object.
I add the libraries directly from the file system to the 'libs' folder (copying the .jars, not linking them), add to build path, check every one in the Order and Export, clean and build, and export to JAR file (not runnable, but simple JAR file). Then, I add my new .jar to Unity Assets/Plugins/Android folder, then build my .apk. As soon as the game starts, I get the classic NoClassDefFoundError because of a constructer using a Jackson class. Checking the .jar contents, I find that Jackson libraries are, indeed, exported and within the 'libs' folder, but still it won't "find" it. The error will be thrown by the main thread, thus the application will crash.
I'm using ADT with API 17, so the libs folder must be named 'libs' (even Eclipse does create this folder when creating a new Android project), JRE 1.6 and Jackson libraries ver. 2.2.0. Already followed a lot of questions here, but none of their suggestions has worked for me.
Any pointers here? Has the Android API version anything to do here? Is there something I'm not doing?
Thanks in advance.
I'm quite new to Java, but as far as I can tell when you use an external JAR file it is dynamically linked. In other words, it stays independent - it is not merged into your library.
This was obvious in my case: The external library I used was Google Analytics. Its JAR file, libGoogleAnalyticsV2.jar, is 126 KB. After I added it to my libs directory and built my library I got an output JAR file of only 2 KB...
My solution was to copy the external library JAR file to Assets/Plugins/Android, together with the JAR file I created.

Adding a JAR into the Android project's /libs folder

There are tons of questions about this, but none gives a clear solution to this problem (is it really so difficult?)
I'm trying to add a jar to my Android project. Since r17, Google says we should add the jars to /libs folder, and that the andriod tools will take care of the rest.
I have included a very big library, and it works good, except of one class, so I decided to try and make my own jar.
I created a new java project, and added a simple Test class. Then I exported it as a jar and manually copied the jar into the /libs folder. I cleaned and rebuilt my android app without problems, but at runtime, referencing the Test class, i get the following errors:
java.lang.NoClassDefFoundError: mytestpackage.Test
When I include the source of the jar into my project directly, it works (of course).
So what am I doing wrong?
Since r17, Google says we should add the jars to /libs folder, and that the andriod tools will take care of the rest.
Correct.
So what am I doing wrong?
If I had to guess, in addition to copying the JAR to libs/, you also did "Add to Build Path" in Eclipse (per the other answer). Not only is that not necessary, but reports indicate that doing this somehow suppresses actually packaging the JAR from libs/ into the APK. If indeed you did this, undo the manual build path change, and see if that helps.
Well, after struggling with it for a day now, I found out that I had the compiler level set to 1.7 in the library project, so when I exported the jar, it was exported with a higher compiling level than what android supports (1.6).
So the solution, in the project of the jar to export:
Properties -> Java Compiler -> Enable specific settings -> Compiler compilance level -> 1.6
That fixed it for me, I hope it's useful.
You need to update your .classpath. In Eclipse, mark the library (put in /libs) and Right click->Build Path->Add to build path

How to automate a complex build including multiple projects and jar creation + obfuscation for Android using Ant?

I'm sure that this has been asked many times (as I have found many similar questions and answers) however, somehow, the solution still eludes me. Basically, I have found tutorials online but none work for me -> they are either out of date and my Ant install doesn't like the build.xml files that exist in those tutorials OR the other tutorials just don't give me all the information that I need to accomplish this (gosh, things should really be made easier).
So, say I have 3 projects -> project 'Initial', project 'DependsOnInitial', and project 'FinalDependsOnBoth'. So, I need to create a script that automates the process of "building the 'Initial' project which depends on other, 3rd party jar files residing in its libs directory and creating an obfuscated jar file" (the .class files from the 3rd party jars don't necessarily need to be obfuscated) which can then be copied over to the 'libs' folder of the 'DependsOnInitial' project. Then, the script will "build another jar file for the 'DependsOnInitial' project which also depends on both the jar file created from 'Initial' as well as other 3rd party jars and then obfuscates the result and packages it into a jar". Finally, the script will "build the project 'FinalDependsOnBoth' which depends on the 2 jar files created as well as other 3rd party jars and obfuscates and signs the result creating an executable .apk file".
I'm actually able to do this right now with my script that I have but it seems like there is some problem in referencing the obfuscated items as my application crashes.
Also, please keep in mind that I am using the latest Android tools (I'm using Rev. 20.0.3 of the SDK tools and Rev. 14 of the SDK Platform-tools); many of the ant build.xml examples I find on the 'net are using older ant systems (my ant system will use ant.properties and local.properties to set many of it's internal values, etc.).
Preferably your reply will include a full build.xml or, otherwise, a bash script which invokes the ant on the relevant projects' build.xml files (or some combination).
(the reason for what I am doing is that the intermediate jar files will need to be distributed to other partners and therefore need to be obfuscated themselves in addition to the final application that will go to the marketplace)

How to include JAR in APK without Eclipse?

I maintain an Android app and am not using Eclipse. I am not using Eclipse. I am using ant and build.xml and build.properties.
I have places my .jar file into the libs/ directory. My code compiles just dandy. But when I run it on the emulator, the output APK does not include the .jar, so I get a runtime stacktrace:
ERROR/AndroidRuntime(470): java.lang.NoClassDefFoundError: com.google.ads.AdView
my build.properties looks like this:
jar.libs.dir=libs
And the libs/ directory contains my .jar file.
What needs to be in build.xml so that the external .jar file is included in the APK?
Edit: In theory this answer should work, but it doesn't for me. Is it out of date? What gives? How to add external jar libraries to an android project from the command line
I just came over a similar problem and noticed that libraries should not be placed in "myprojectdir\lib". When I moved them to "myprojectdir\libs" everything started to work.
It turns out that I needed to upgrade the version of ant I was using to 1.8. During the compile process, I had been getting this error message:
Warning: Reference out.dex.jar.input.ref has not been set at runtime,
but was found duringbuild file parsing, attempting to resolve. Future
versions of Ant may support referencing ids defined in non-executed
targets.
I googled it, and found that I needed to upgrade Ant, and now I don't get this warning, and my application does not force close.
What needs to be in build.xml so that the external .jar file is included in the APK?
Just putting it in libs/ is sufficient.
my build.properties looks like this:
That line should not be necessary. It does not appear in my build.properties files that build successfully with JAR files.
If you use dexdump -f classes.dex from your project's bin/ directory, you will be able to determine whether com.google.ads.AdView made it in there. If it did not, then something is strange with your build scripts. If it did, then perhaps there is a dependent JAR that you are missing (though I would expect a VerifyError in that case).
You use 3rd party library, but you seem didn't run DX on it. Make sure that not only your code processed by DX tool (I assume Ant does it), but also all 3rd party libraries you use. You can look in 7Bee script I use to convert web applications to Android davlik format, so it can work for you too. You can find more about the script on Atjeews page.
Solution:
right click on the project in project tree and select Project
properties
select Java Build Path
select TAB Order
and Export
check GoogleAdMobAdsSdk-4.0.4.jar (or your
version SDK)
press OK
clean project by menu Project
-> Clean
rebuild project (Project – Build Automatically)

Categories

Resources