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

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!

Related

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)

Android SDK Tools R20, javadocs for 3rd party libraries and Ant builds

Since yesterday's release of SDK API 16 and SDK tools R20, I've been unable to make work the combination of the new tools, being able to see the javadoc hovers in Eclipse and being able to do an Ant build from the command line.
When R17 tools was released it became necessary to put all 3rd party jars in the new libs directory. Unfortunately this meant that you couldn't specify the location of the javadocs for a jar in there. The widely used workaround was to put the jars in another folder, specify that in the build properties and export it. This worked for me until R20 came along.
My build.xml in the simplest example is the standard one from a newly created sample project, except for passing the projectname in from a properties file. (I put the jar in a 'lib' directory)
I had an ant.properties file of the form:
projectname=MyProject
jar.libs.dir=lib
build.sysclasspath=last
workspace.dir=/dev/projects/EclipseIndigo/AndroidWorkTwo
outbasebase.dir=/dev/projects/AntBuilds
base.dir=${workspace.dir}/${projectname}
source.dir=${base.dir}/src
outbase.dir=${outbasebase.dir}/${projectname}
ant.project.name=${projectname}
out.dir=${outbase.dir}/bin
layout.dir=${base.dir}/res/layout
key.store=<hidden>
key.alias=<hidden>
key.store.password=<hidden>
key.alias.password=<hidden>
This now fails at the compile target in an Ant command line build (ant release) as it can't find the classes in the jar.
Setting various combinations of project.all.jars.path and tested.project.classpath don't make it work either.
Does anyone have any suggestions for restoring the old functionality?
Despite the remarks in comment 21 of this issue, adding a .properties file doesn't enable the javadocs if the jars are in libs. (Putting the jars in libs still enables my custom build.xmls to work with Ant though, so that's something!) It seems that fixing the complicated integration of library projects and testing regimes is considered important, whilst having broken basic functionality like javadocs support in an IDE can be safely ignored.
The only thing I found which works is the answer here.
I add this to my custom_rules.xml template and it seems to be working fine.
I supposed it's only a few more lines than adding path to the ant.properties file until we get a better solution.
<target name="-pre-compile">
<path id="project.all.jars.path">
<path path="${toString:project.all.jars.path}"/>
<fileset dir="${jar.libs.dir}">
<include name="*.jar"/>
</fileset>
</path>
</target>

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)

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

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.

Categories

Resources