I'm new to automating builds using Ant. I have a bunch of Android project in Eclipse and I've gotten as far as setting up Ant builds I can run from the command line for each project, where a signed APK is generated in the 'bin' directory for whatever project I am running the build on.
If possible, I'd like to setup a single script to build each of my projects and output the signed APK's to one directory on my computer. Not exactly sure how to do that, whether I need to write a batch script or something else.
Any insight would be appreciated.
A batch script could be one option. Another, now that you're working with Ant, would be to create an Ant script that runs all the other Ant scripts and copies the results.
You can use the ant task to run one Ant script from another. You can then use the copy task to copy the resulting APK file to where you want it.
Here is an example that runs the default target of the build.xml file found in the directory path_to_other_project and then copies any APK files found in path_to_other_project/bin to destination_dir.
<ant dir="path_to_other_project"
antfile="build.xml"
inheritAll="false"
inheritRefs="false" />
<copy todir="destination_dir">
<fileset dir="path_to_other_project/bin" includes="*.apk" />
</copy>
If you have several projects, you can replace several similar calls to ant with one call to subant.
Related
I'm trying to build my android project from command line.
ant debug install && adb shell 'am start -n com.test.example/.MainActivity'
I find, ant doesn't recognise modified files sometimes(or most of times) unless I do ant clean
Is there a workaround to make ant recognise file changes while developing?
This is a known issue:
ant clean
ant debug <-- This result is OK.
change some source code
ant debug
RESULT:
-dex:
[dex] No new compiled code. No need to convert bytecode to dalvik format.
...
-package:
[apkbuilder] No changes. No need to create apk.
you can try a few things things:
Workaround for [dex]: include a <delete> just before the call to <dex /> within the macro "dex-helper":
<delete file="${intermediate.dex.file}"/>
Workaround for [apkbuilder]: include a <delete> into the macro "package-helper":
<macrodef name="package-helper">
<element name="extra-jars" optional="yes" />
<sequential>
<delete failonerror="false" file="${out.packaged.file}"/>
<apkbuilder outfolder="${out.abs.dir}" resourcefile="${res.pkg.file.name}"
...
See HERE for these Workarounds and a long, related, discussion.
these may be useful too:
try ant clean debug: call clean, because otherwise the build system will not detect changes in the configuration, and may not recompile the proper classes. , OR
android update project -p <project-path>: will generate any files and folders that are either missing or need to be updated, as needed for the Android project See HERE
I have all of my tests in my test project located in a tests/ directory and everything in this directory is run when I call ant test. The directory has a few subdirectories, such as unit and functional. How can I tell ant test to only run tests in one of these subdirectories and ignore the others?
In my setup i defined a separate Ant target for each type of test, and then made each target conditional on some property.
<target name="performance" if="test.performance">
</target>
Then at build time specify the required properties on the ant command line (-Dkey=value). If you have allot of different packages to be tested independently consider defining properties in a properties file which you load in your build file.
let me know if you have any questions
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!
I have a number of projects that I work on simultaneously.
Every time I build and run one of them, the apk is located as usual in the bin folder.
If I want to copy this apk to some other folder outside the project, I have to do it manually.
I created a single batch file that copies all my projects' apk files to the desired location.
Is there a way to change the output location of the bin folder to somewhere outside the project or more preferably run the batch file after each build?
NOTICE
I am using eclipse with ADT. I have tried to add a builder that executes the batch file. However, when the batch file is run, the apk file is not yet generated. I tried all combinations of options in the builder and all the possible of sequences of builders.
If you are happy just to use Eclipse whilst you are perfecting the build, then switching to the command line for the final build, then with Ant it's really easy to get what you want with very little effort or configuration.
Assumptions
1) Your sources are in an Android workspace and you will end up with two sets of binaries - one made by Eclipse, the other made by Ant will end up outside the workspace as set by a PROPERTIES file
2) You are using SDK14 or 15 (Ant changed in 14)
3) You have Ant installed and in your path - you'll need to have Ant 1.8.2 - this is not the internal one that Eclipse uses, you may have to get it from the Apache site, it's easy to install
Steps
1) Make a sample project from the command line as described in http://developer.android.com/guide/developing/projects/projects-cmdline.html
For example I used:
android create project --target 8 --name Sample15App --path c:\dev
\projects\samples\Sample15 --activity Sample15Activity --package com.me.samplefifteen
This will make a directory and some files which you will use later as a template in your projects
2) Make a sample project in your workspace from Eclipse, I made EclipseSample in one of my workspaces
3) Copy the following files from Sample15App to the root of your EclipseSample project:
build.xml
ant.properties
local.properties
4) Edit ant.properties (which is initially empty) to be like this example:
projectname=EclipseSample
workspace.dir=/dev/projects/EclipseIndigo/AndroidWorkTwo
base.dir=${workspace.dir}/${projectname}
outbasebase.dir=/dev/projects/AntBuilds
outbase.dir=${outbasebase.dir}/${projectname}
ant.project.name=${projectname}
out.dir=${outbase.dir}/bin
layout.dir=${base.dir}/res/layout
source.dir=${base.dir}/src
From this you can see that my workspace is /dev/projects/EclipseIndigo/AndroidWorkTwo
The eclipse project under this is in directory EclipseSample
I want my apks to end up in /dev/projects/AntBuilds/EclipseSample (i.e outbasebase concatenated with projectname -so for other projects you can use a very similar ant.properties file just change projectname)
5) IMPORTANT - EDIT THE build.xml
Comment out or remove the line :
<project name="Sample15App" default="help">
replace it with just
<project>
This just means it will pick up the project name from ant.properties rather than the build.xml and you can use the same build.xml in all your projects, only ant.properties need change
6) try it with "ant debug" should build the debug apks under /dev/projects/AntBuilds/EclipseSample
7) finally if you want to automate the release build (signing and password entering automatically) add lines like
key.store.password=YourPassword
key.alias.password=YourPassword
key.store=c:/users/you/yourrelease-key.keystore
key.alias=release_alias
to the ant.properties and then you just type "ant release"
If you don't add them it will tell you to sign manually and as no password entries were found in 'build.properties' (- that was what ant.properties used to be called pre SDK 14, they should have corrected this!)
You can add new Builders to your project using Project Properties->Builders->New....
One way to do this is to create an ant build (which copies the file), or just a plain old script or batch file.
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.