Use ant (build.xml) to compile jni directory - android

I'm using ant to build my android app (ant compile, ant install and so on) and I'm adding some NDK sources (inside jni/ directory) and I want to have a rule to recompile the jni/ directory with ant and THEN install my app into my device/emulator, like this:
$ ant jni-and-install
Instead of:
$ make -f jni/Makefile APP=myapp
(jni compilation & installation)
...
$ ant install
I was trying to find the place to put my rules in build.xml but I don't know where.
What should I edit for ant to call make on jni/ directory before installing into device?

You could use the exec ant task :
<target name="install">
<!-- ... -->
</target>
<target name="jni">
<exec executable="make">
<arg line="-f jni/Makefile APP=myapp"/>
</exec>
</target>
<target name="jni-and-install" depends="jni, install"/>

You can use an exec ant task for this. I think putting the following in your -pre-build target in build.xml will do the trick:
<exec executable="make" failonerror="true">
<arg line="-f jni/Makefile APP=myapp"/>
</exec>
Edit: JB's solution allows for things like ant jni to build the jni code without building the rest of your project. You can get the best of both worlds by wrapping the above in a target named "jni" and then adding depends="jni" to your -pre-build target.

Related

run ant with the NDK without setting the ndk.dir in the local.properties

I've been trying to compile a NDK project using ant (ant release), but to make this possible I need to set the local.properties with the following line:
ndk.dir=/{my-path}/android/android-ndk-r9
I would like to add this in my .bash_profile, but I tried with all this variables:
export ANDROID_NDK=/{my-path}/android/android-ndk-r9
export NDK=/{my-path}/android/android-ndk-r9
export NDK_ROOT=/{my-path}/android/android-ndk-r9
export NDK_PATH=/{my-path}/android/android-ndk-r9
export NDK_HOME=/{my-path}/android/android-ndk-r9
Any body knows how to fix that?
you can try to use
<exec executable="/{my-path}/android/android-ndk-r9/ndk-build.cmd" failonerror="true"/>
Another way, you can tell ant to use your variables by adding
<property environment="env" /> befor the exec line.
for example set variable NDK_DIR and use it in the ant.properties file using "env.VAR" like
ndk.dir=${env.NDK_DIR}
or in my case:
<exec executable="${ndk.dir}/ndk-build.cmd" failonerror="true">
<env key="ABI" value="armeabi-v7a"/>
</exec>

Ant Build Failed

I am trying to build an Android Project using Apache Ant through Command Line but it is giving me Build Failed with the following error statements:
Java.io.IOException: Cannot run Program. "..\android_sdk\platform-tools\aapt.exe"
D:\MediaSync\build.xml:429: Execute failed: java.io.IOException: Cannot run program "..\android_sdk\platform-tools\aapt.exe"
I am new to this thing so i am not getting any idea how to get over this error. Searched alot about it but still scratching my head..
Anybody any idea about it then please tell me.
EDIT: Here are parts of my Buil.xml file
<!-- General SDK location -->
<property name="sdk-folder" value="D:/sdk" />
<!-- Preset tools location -->
<property name="android-platform-tools" value="${sdk-folder}/platform-tools"/>
<!-- Tools -->
<condition property="aapt" value="${android-platform-tools}/aapt.exe" else="${android-platform-tools}/aapt" >
<os family="windows"/>
</condition>
<!-- Generate the R.java file for this project's resources. -->
<target name="resource-src" depends="init, localization, replace-template-values">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg value="${outdir-r}" />
<arg value="-M" />
<arg value="${outdir.manifest}/AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
Make sure aapt.exe is installed at the correct path relative to your build.xml file (../android_sdk/platform-tools). That path is likely relative to your project, but not the build.xml.
I encountered the same problem on a 64-bit fedora platform and finally worked it out following this: http://blog.mx17.net/2012/10/25/android-on-ubuntu-ioexception-on-aapt/
It had failed because of trying to run a 32-bit program on an 64-bit architecture.
Maybe this would help.
Your error message says:
D:\MediaSync\build.xml:429: Execute failed: java.io.IOException: Cannot run program "..\android_sdk\platform-tools\aapt.exe"
while your aapt.exe is located in:
C:\android-sdk-windows\sdk\platform-tools\aapt.exe\
Obviously the build.xml tries to search D:\ for android_sdk\platform-tools\aapt.exe, which is the wrong path.
There should be a property for you to specify the path of aapt.exe, or it reads from system's PATH varibles.
To get a solution, post the lines around line 429 in build.xml. In line 429, there should be a <exec> task, be sure to include the whole content of that task (copy from <exec ...> to </exec>).
There is a bug in the intellij android studio and perhaps other IDEs that points to platform-tools for some executables/libs that now live in build-toos. A workaround for now is to make sim-links fof the necessary files.
So on linux/mac
platform-tools$ ln -s ../build-tools/android-version-blah/bin/appt appt
and
platform-tools$ ln -s ../build-tools/android-version-blah/lib lib
for Windows, install LinkExtensions and replace ln with mklink and read the help from typing mklink with no args. In Windows, the file would be appt.exe
A quick fix you can do is copy the aapt.exe and lib folder from the build-tools to the platform-tools folder.

how to generate apk using ant

i want to know how can we generate apk file using ant script. i came to know that we can use "ant debug" command to do that.but when i am running this command it is returning error that " Target "debug" does not exist in the project". i am using ant1.7.0.is it the issue?i have tried given below code to generate apk.
<target depends="build-subprojects,build-project" name="build">
<jar destfile="bin/test.apk" basedir="bin/classes" >
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
but the generated apk is different from auto generated apk while build the app using eclipse.Please guide me.
but when i am running this command it is returning error that " Target "debug" does not exist in the project"
Then you did not create your Android project properly, or you modified your build.xml file, or something else is broken.
As a test, I just ran the following command from my /tmp directory on Linux:
android create project --target android-17 --path Foo --package com.foo --activity Foo
I then changed into the Foo directory and ran ant debug. The app compiled, and in my bin/ directory is Foo-debug.apk.
If you are not getting this sort of result, delete your build.xml file and run android update project --path ..., where ... is the path to your project, to create a fresh build.xml file.

Hudson Android multi-configuration test project can't find test results - "No test report files were found. Configuration error?"

I have android test project that i've integrated into the Hudson. I have created a free-style software project with next settings :
1. Source Code Management - custom workspace
2. Run an Android emulator during build :
* Android OS Version: 4.0.3
* Screen density: 240
* Screen resolution: WVGA
* Device locale: en_US
* SD card size: 16M
3. Invoke Ant:
Target: all clean emma debug install test
Build File: workspace/Project/build.xml
4. Post-build Actions
* Archive the artifacts: **/*test-TEST.xml
* Publish JUnit test result report: **/*test-TEST.xml
* Record Emma coverage report: **/coverage.xml
* Record fingerprints of files to track usage: **/*test-TEST.xml
Here's build.xml file part that covers my ant target:
<!-- version-tag: custom -->
<target name="test" depends="-test-project-check"
description="Runs tests from the package defined in test.package property">
<property name="tested.project.absolute.dir" location="${tested.project.dir}" />
<property name="test.runner" value="android.test.InstrumentationTestRunner" />
<!-- Application package of the tested project extracted from its manifest file -->
<xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
expression="/manifest/#package" output="tested.manifest.package" />
<xpath input="AndroidManifest.xml"
expression="/manifest/#package" output="manifest.package" />
<property name="emma.dump.file"
value="/data/data/${tested.manifest.package}/coverage.ec" />
<if condition="${emma.enabled}">
<then>
<echo>WARNING: Code Coverage is currently only supported on the emulator and rooted devices.</echo>
<run-tests-helper emma.enabled="true">
<extra-instrument-args>
<arg value="-e" />
<arg value="coverageFile" />
<arg value="${emma.dump.file}" />
</extra-instrument-args>
</run-tests-helper>
<echo>Downloading coverage file into project directory...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="pull" />
<arg value="${emma.dump.file}" />
<arg value="coverage.ec" />
</exec>
<echo>Extracting coverage report...</echo>
<emma>
<report sourcepath="${tested.project.absolute.dir}/${source.dir}"
verbosity="${verbosity}">
<!-- TODO: report.dir or something like should be introduced if necessary -->
<infileset dir=".">
<include name="coverage.ec" />
<include name="coverage.em" />
</infileset>
<!-- TODO: reports in other, indicated by user formats -->
<html outfile="${basedir}/coverage/coverage.html" />
<xml outfile="${basedir}/coverage/coverage.xml" />
</report>
</emma>
<echo>Cleaning up temporary files...</echo>
<delete file="coverage.ec" />
<delete file="coverage.em" />
<echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
</then>
<else>
<run-tests-helper />
</else>
</if>
<mkdir dir="${basedir}/junit-results" />
<echo>Base dir is ${basedir}</echo>
<exec executable="${adb}" failonerror="true" dir="${basedir}/junit-results">
<arg line="${adb.device.arg}" />
<arg value="pull" />
<arg value="/data/data/${tested.manifest.package}/files/" />
</exec>
</target>
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
Now, this project works fine and i get both emma coverage and test results, but when i configured multi-configuration project with the same options, except for android emulator i set different variables with os, density and screen resolution, i got stuck with one error -
test:
[echo] WARNING: Code Coverage is currently only supported on the emulator and rooted devices.
[echo] Running tests ...
[exec]
[exec] com.mobclix.android.externaltestharness.test.UnitActivityTest:.
[exec] Test results for PolideaInstrumentationTestRunner=.
[exec] Time: 25.122
[exec]
[exec] OK (1 test)
[exec]
[exec]
[exec] Generated code coverage data to /data/data/com.mobclix.android.externaltestharness/coverage.ec
[echo] Downloading coverage file into project directory...
[exec] 355 KB/s (2043 bytes in 0.005s)
[echo] Extracting coverage report...
[echo] Cleaning up temporary files...
[delete] Deleting: /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage.ec
[delete] Deleting: /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage.em
[echo] Saving the report file in /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.html
[echo] Base dir is /Users/biercoff/Work/Workspace/HarnessExternalTest
[exec] pull: building file list...
[exec] pull: /data/data/com.mobclix.android.externaltestharness/files/com.mobclix.android.externaltestharness.test-TEST.xml -> ./com.mobclix.android.externaltestharness.test-TEST.xml
[exec] 1 file pulled. 0 files skipped.
[exec] 40 KB/s (499 bytes in 0.012s)
BUILD SUCCESSFUL
Total time: 1 minute 20 seconds
$ /Users/biercoff/.hudson/tools/android-sdk/platform-tools/adb disconnect localhost:53830
[android] Stopping Android emulator
[android] Archiving emulator log
$ /Users/biercoff/.hudson/tools/android-sdk/platform-tools/adb kill-server
Recording fingerprints
Recording test results
No test report files were found. Configuration error?
Archiving artifacts
Description set:
Emma: looking for coverage reports in the provided path: /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.xml
Emma: found 1 report files:
/Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.xml
Emma: stored 1 report files in the build folder: /Users/biercoff/.hudson/jobs/MulitTestHarnessProject/configurations/axis-density/240/axis-os/4.0.3/axis-resolution/WVGA/builds/2012-05-18_17-26-05/emma
Emma: Coverage: Classes 21/39 (54%). Methods 40/128 (31%). Blocks 833/2928 (28%). Lines 154.5/528 (29%).
[DEBUG] Skipping watched dependency update for build: MulitTestHarnessProject/density=240,os=4.0.3,resolution
For emma coverage i had to change path in Hudson into /Users/biercoff/Work/Workspace/HarnessExternalTest/coverage/coverage.xml to make it work,
but when i tried to do the same thing for Junit test results, Hudson refused to "see" it. File is in the project folder under junit-results folder, but everytime i run new build, i get the same No test report files were found. Configuration error? error message. Tried many variations, but no luck. I'm missing probably something really tiny. Could somebody help me, please?
Hudson searches for jUnit test results relative to its workspace. And you can't specify an absolute path (I've been using Hudson/Jenkins for more than a year and still not sure why not). So in your case it is looking only at things that are deeper in the directory level than the workspace. Now, for a multi-congiguration project the workspace does not start at the top level, but deeper - it includes axes names (e.g.: workspace_root/AXIS1_NAME/AXIS1_VAL...). But your tests are located up the directory tree from that workspace. You have to copy them in a build step into ${WORKSPACE} and then Hudson will be able to publish them.

Error with NetBeans Android plugin

I am currently developing an Android app and i am using Netbeans 6.9.1 and nbandroid 1.0 beta. I have installed Android SDK and configured everything but when i create a simple project and try to build it gives an error
Creating output directories if needed...
Created dir: C:\Documents and Settings\KeSoLK\My Documents\NetBeansProjects\AndroidApplication2\bin
Created dir: C:\Documents and Settings\KeSoLK\My Documents\NetBeansProjects\AndroidApplication2\gen
Created dir: C:\Documents and Settings\KeSoLK\My Documents\NetBeansProjects\AndroidApplication2\bin\classes
Compiling aidl files into Java classes...
Compiling RenderScript files into Java classes and RenderScript bytecode...
Generating R.java / Manifest.java from the resources...
compile:
C:\Documents and Settings\KeSoLK\My Documents\Android\android-sdk-windows\tools\ant\main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
Compiling 2 source files to C:\Documents and Settings\KeSoLK\My Documents\NetBeansProjects\AndroidApplication2\bin\classes
Converting compiled files and external libraries into C:\Documents and Settings\KeSoLK\My Documents\NetBeansProjects\AndroidApplication2\bin\classes.dex...
=C:\Documents was unexpected at this time.
C:\Documents and Settings\KeSoLK\My Documents\Android\android-sdk-windows\tools\ant\main_rules.xml:472: The following error occurred while executing this line:
C:\Documents and Settings\KeSoLK\My Documents\Android\android-sdk-windows\tools\ant\main_rules.xml:203: apply returned: 255
BUILD FAILED (total time: 1 second)
what can be the problem? and how to solve it.
Thanks !!
Just encountered the same issue. It seems to be related to spaces in path at the time ant tries to start dex.bat with parameters:
<exec executable="${dx}" failonerror="true">
<arg value="--dex"/>
<arg value="--output=${basedir}/${intermediate.dex}"/>
<arg value="--positions=lines"/>
<arg path="${build.classes.dir}"/>
</exec>
The project won't run if ${basedir}, ${intermediate.dex} or ${build.classes.dir} contain spaces.
Parameters for batch files have to be encapsulated with "...". But how to do this when ant starts the batch file?
My quick workaround was to move the folder of my netbeans android project (not all projects) to a path that doesn't contain spaces.
Indeed it is a 'space in path' problem of Android SDK.
Other than that I'd strongly recommend you to update to newer version of both NetBeans and NBAndroid plugin.

Categories

Resources