Android dx usage - android

I have a jar file created out of an android application, because I marked it as "Is library" in eclipse at creation time. Now the DexClassLoader is not able to load this file because it doesn't have an entry marked classes.dex. This looks like a standard jar. How can I convert such a jar into a dexed jar with dx that DexClassLoader can load? Any help appreciated!

You can use the dx tool from the sdk, from the command line. Something like:
dx --dex --output=dexed.jar hello.jar
Works for me. Integrating such things into your build process is something of a black art, largely involving hacking up the ant buildscripts provided by the SDK. If your library is only occasionally updated it might be viable to do it manually.
In my case, I keep the dexed.jar in my resources/raw folder. At runtime, I copy it from there into the filesystem, then pass the filesystem path to the DexClassLoader. It's... a little bumpy.

Android Libraries are not meant to be started on device. They only can be included as part of Android Projects, which will convert all referenced Android Library .jar files into single shared .dex file and package them to .apk file.
If you want to test your Android Library and it's manifest actually has some entry points for that, then the only way to do so is temporarily change Android Library to Android Project (by checking off that check bar in settings).

Related

aar vs "plain module" advantages

if I have a project with many library projects linked, could I improve build performances by packaging each of them in an AAR and including it in the main project ? Or this will not make any difference since that when the compiler need to assemble the apk it need to package everything together anyway?
Thanks to any one who will give me some clarifcation about performance differences between the 2 approach
I don't think you will save any build time by wrapping an existing .jar file into a .aar file and using that instead of the original .jar file.
As this SO post notes, .aar files are basically just zip files which may contain a jar file and some android resources.
Also, because .aar files are not precompiled into Dalvik byte code, the build process for your apk file must still carry out that step at least once. So, you won't save dexing time just by wrapping the .jar file into a .aar file either.
If you build a typical Android Studio project (with some Android library dependencies specified in the gradle build file) you can see the directory underneath app/build/intermediates/exploded-aar where these files have been unzipped. That work must still be done by your build machine even though you are using a .aar file.
Finally, as you pointed out, the .apk packaging work must still be done at the end of the build.
I believe the Library projects (which you are using) is the best way to go because of two reasons:
The library project gives the direct access to the code base of the libraries which can be compiled and packaged together with the main app code
In case, multiple .aar files are referenced within the project, then during the apk creation the unpacking, merging of resources and Manifest file will increase the build time.

Debugging external libs in 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...

Quick and dirty android module creation without using Eclipse IDE to build jar files

I am currently developing an android application that allows me to dynamically load modules.
Therefore I can have as many modules as I like, and my main application just needs to load them.
I've managed to make this work for the most part - but I want to create an executable to "speed up" the module creation process, or even just find a way to simplify the steps involved.
To create a module I currently have to do the following:
Build the android project containing the files required for my
module [none of which are activity classes - so I don't have to
worry about the androidmanifest file at all]
Use the eclipse IDE jar creation tool to select which src files I want to be compiled into .class to put into my jar, and specify my own custom manifest file for this jar, as well as package all the images I use for the module into the jar as well.
Then using the jar file I run the dex creator command on it to generate a .dex file from the class files contained in the jar, and then use the aapt command to push the dex file back into the jar file.
At this point the jar(Now its a module) is created and I can put it on the server for downloading, download the modules in my app and load all the code I need in my app using reflection.
I have looked into building with ant. It looks fairly complicated for what I wish to achieve and I'm not quite sure where to start with it.
I obviously can't use simple javac to compile my java files contained in my module src because all that code makes references to the android sdk as well as a static library shared between my main application and my respective module.
Currently I use the Eclipse IDE to create the inital jar with all my packaged images, class files, manifest, and then I use two separate batch files that call on the android-sdk to create dex and push the dex into the jar.
Can I simplify this process in one easy step instead? Or is trying to do this - a whole project on its own?
I have looked into building with ant. It looks fairly complicated for what I wish to achieve and I'm not quite sure where to start with it.
The documentation for Ant is online, as is the documentation for building Android projects with Ant. The only difference is that you will want to add a <jar> task to your Ant build.xml file, as I have done in several projects, such as this one:
<target name="jar" depends="debug">
<jar
destfile="bin/CWAC-EndlessAdapter.jar"
basedir="bin/classes"
/>
</target>
You are also welcome to consider Maven. While I do not use Maven personally, it has many fans and community-driven Android support.
You are also welcome to write your own build script in any programming language that suits your fancy: Java, Ruby, Perl, Python, etc.
I obviously can't use simple javac to compile my java files contained in my module src because all that code makes references to the android sdk as well as a static library shared between my main application and my respective module.
Every Android IDE, and Ant, and Maven, and so on, "use simple javac to compile [an Android project's] java files". They simply add the appropriate Android SDK JAR file to the build path.

How to include Java resources from a JAR in my Android APK?

I have an Android project that depends on a non-Android JAR that contains resources (Java resources, not Android resources), which classes within the JAR need to load. When running the application, these resources are not found (i.e., ClassLoader.getResourceAsStream() fails), apparently because the resources are not being included in the APK.
I found some discussion here:
http://code.google.com/p/android/issues/detail?id=10076#c7
But I need to build the APK in Eclipse. Short of doing a command-line build with a deprecated tool (ugh), or duplicating all the resources (ugh), how can I make it work?
Create your jar file with the classes you need and save it your computer. Then in the project explorer right click the project and go to properties -> Java Build Path -> Libraries. Now import the jar file.
You should now have full access to the classes and methods in your code and the jar file will be installed with your APK. My guess is you have utility classes and when you are calling them in your source Eclipse is importing them from another project.
The solution I have given works for sure (I do it myself).

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