How to build an android app with external libraries using ant? - android

I have an existing project that builds fine using my IDE. I'd like to use the "android update" command to generate an ant buildfile for this project.
The buildfile is generated fine, but the build fails because it's not building with some jarfiles I have in my libs directory.
I'd like to figure out the proper way to tell ant to build with some external jar files in my libs directory. How should I do this? Is it a property in build.properties? Do I need to modify build.xml somehow? Or is there a different solution entirely?

but the build fails because it's not
building with some jarfiles I have in
my libs directory.
And your error message is...what? I suspect you may be misinterpreting the error message.
I'd like to figure out the proper way
to tell ant to build with some
external jar files in my libs
directory. How should I do this?
Just put them in libs/, as Ant will add everything in there to your build path. See this project, and this project, and this project for examples.

I spent some time trying to get the Facebook API to work with ant. The trick for me was to add this to my default.properties files.
android.library.reference.1=../Facebook
Where ../Facebook contains AndroidManifest.xml, etc. The real key being the relative path. Don't use an absolute path because Ant seems to treat your project directory as the root.
This should hold true for other library projects that you are including from source code.

I was dealing with similar issue. I'm building Android project on Jenkins using standard Ant build.xml (generated by Android SDK). I also have reference to another Java project with some shared domain classes. In Eclipse there is no problem with it. The domain project is a project reference. However on Jenkins this domain.jar is built by Maven and it was not accessible by Android project.
I have finally solved it by adding this at the end of build.xml:
<target name="-pre-build">
<copy todir="${jar.libs.dir}">
<fileset
dir="../path-to-another-project/target"
includes="*.jar" />
</copy>
</target>
This copies all jars from the target directory of another project into "libs" directory of my Android project. The -pre-build Ant target is automatically called before Android compilation starts.

I agree with Mark, however, if you're planning to modify your build script further - than you need to make it custom. Bring tasks from android/platforms/android-PLATFORMVERSION/templates/android_rules.xml to your build.xml and modify whatever you want to modify. Including location for external libs.

Related

Android gradle build includes jni libs twice

I've been trying to solve a problem about Android build, but couldn't figure out how to solve it.
Basically, I am trying to build an Android project using Gradle. It works perfect, but the size of the final apk is 7MB more than when I builded with Eclipse.
When I unzip the apk, I see that the JNI Libs are included twice. Which is different from the Eclipse build that includes it only one time. Here is the paths I can find them in the APK:
lib/armeabi
lib/armeabi-v7a
main/jniLibs/armeabi
main/jniLibs/armeabi-v7a
In my project, those two files are in:
android/app/src/main/jniLibs/armeabi
android/app/src/main/jniLibs/armeabi-v7a
I have two different build.graddle files in:
android/
android/app/
None of them contains anything related to the JNI Libs.
To build a release, I use the command:
./gradlew assembleRelease
And everything works fine. I use Gradle 1.10.
I was wondering if someone ever encountered the problem and find a solution to avoid to the JNI Libs to be included twice in the APK.
Thanks :)
Put .so files in...
/src/main/jniLibs/armeabi-v7a
/src/main/jniLibs/x86
directories and gradle will correctly package the .so files into the correct app and it won't include the duplicates
This is a good reference..
http://www.shaneenishry.com/blog/2014/08/17/ndk-with-android-studio/

android build: add jar to endstate via command-line builds

With respect to this question, simply putting a jar into the /libs does not auto-magically include that jar into the .dex when invoking, say, ant debug. Not at least with Android SDK ver 15.
I've verified this by comparing the same project created two different ways. First (call it 'Alpha'), by Eclipse. Second, via android create project ..., blah blah blah (the Ant way, call it 'Bravo').
Both Alpha and Bravo I built using ant debug. Comparing their /bin dirs, the jar under <project_root>/libs is missing from Bravo's *.d; Alpha's aren't.
What magic is Eclipse embedding during project creation?
Better still, how can I ensure a jar is passed to ant debug|release when building a project, that a jar is included in the endstate?
You may just have to adjust the build.xml to include something like the following in the -pre-build node if you want to hardwire it in. You can also add this in a custom target in the build.xml file that would able you do include from the command line when you want. Something like the following could work.
<target name="add-jar">
<copy file="${src.folder}/YourJarHere.jar" tofile="libs/YourJarHere.jar" overwrite="true"/>
</target>
Where ${src.folder} is defined in your ant.properties file.
Something like src.folder=PATHOFYOURJAR
Then you should be able to run
ant debug add-jar
including the file.
I have had to manually create the libs folder in the past but I don't see why you can't do this through the build.xml file. Hope this helps!

Ant build Android project with dependency lib

I have two Android projects, one shared library and the app. Now I want to compile the app with dependency to the library. In Eclipse, it works very well. After that, I upload it via git to my repository and trigger Jenkins to build both projects.
My problem is, that the error occurs: "sdk/android-sdk-linux/tools/ant/build.xml:440: ../shared-lib resolve to a path with no project.properties file for project". That's clear, because in Jenkins the jobs are stored different than under Eclipse.
Another problem is, that Eclipse compiled the shared to ".jar" and Ant compiled it to "classes.jar" (is named in sdk/android-sdk-linux/tools/ant/build.xml).
Ant scripts should allow you to include whatever files you need. In your case I will suggest you move the reference to the shared-lib to local.properties file (this file should also be read by the ant script generated by update-project. Keep the adequate path for jenkins in the repository and modify the file locally for the local built. In the file in the repository you will need to have something like:
android.library.reference.1=../classes.jar
EDIT By the way the suggestion of the second properties file is just because this file is really meant to store location-specific properties.
I fixed it with copy files. The first project builds my shared-lib.jar. The other projects (phone and tablet) copy this file (shared-lib.jar) to there libs-folder and build correctly. But now I have different projects.propertieson the server and my dev-client. This one is not checked in into git.

Adding referenced jars to android build.xml

I have an android eclipse project and have generated the corresponding build.xml ant file by doing:
android update test-project -p . -m ..\main_project
This compiles fine. However, in eclipse I have then added three referenced external jars (easymock, objenesis and cglib). In eclipse everything builds fine but then when I run the above command again it doesn't add these jars to the classpath.
So my question is how to tell the build.xml to include these jars?
I have done some research first and some people suggested changing the rules files in the sdk itself but this is something I want to avoid as then everyone in my team would need to do this and that's not practical.
Thanks
Stephen
add jar.libs.dir=your_path_here/lib in ant.properties for SDK >=8
For build.xml you can set property "external.libs.dir" in one of your properties file( like build.properties), by default it points to libs/ folder of your project - simply put there all your jars
external.libs.dir=<your_custom_path>/lib
You should ideally use libs/ not lib, to remain consistent with Eclipse.

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