Android NDK build with ANT script
Asked Answered
H

2

24

Can we use the ANT script for Android NDK builds ? If Yes how? And if no, then why Not ?

I don't have any idea about it

Hylophagous answered 15/9, 2011 at 14:24 Comment(0)
B
43

Call ndk-build from your -pre-build target, like this:

<target name="-pre-build">
    <exec executable="${ndk.dir}/ndk-build" failonerror="true"/>
</target>

<target name="clean" depends="android_rules.clean">
    <exec executable="${ndk.dir}/ndk-build" failonerror="true">
        <arg value="clean"/>
    </exec>
</target>

Then you can set ndk.dir to point at the NDK directory from your local.properties file, or set it on the command line. I do this:

ant -Dsdk.dir=/home/dg/apps/android-sdk-linux_x86-r11/ -Dndk.dir=/home/dg/apps/android-ndk-r6b release

Now running ant will build your native code automatically. Plus, running 'ant clean' will clean your native code.

Updated: Added failonerror="true" to the <exec> tasks --- this causes ant to abort if the make fails. Without it it'll just go right ahead and generate an APK with an invalid binary in it. Not good!

Bally answered 30/9, 2011 at 13:36 Comment(8)
Thanks Actually done with it, but forget to edit here what I did is this <target name="n"><echo message="Starting Android NDK build step..." /> <!-- Build JNI code and create shared object loaded by app --> <exec executable="sh" failonerror="yes" > <arg value="${env.ANDROID_NDK_ROOT}/ndk-build" /> <arg value="V=1" /> </exec> <echo message="...finished Android NDK build step." /></target>Hylophagous
On windows, you have to use sh as the executable, and the ndk-build script as the first argument.Vagabond
In what file do those <target> directives go? isn't the build.xml file generated by the android tool?, so changes would be destroyed if you have to update the project.Wenn
I believe that the automatic build.xml generator will attempt to merge around your own customisations when updating the project, but I don't use it so I can't tell you for sure.Bally
You should put this custom build step in custom_rules.xml as it is included in to the autogenerated build.xml but not modified.Alan
On windows use: ndk-build.cmd instead of ndk-buildAmmunition
What's the release suffix for?Gavrielle
It tells ant to build the target called release, which (IIRC) is part of the standard Android rules to generate a release-mode apk file.Bally
D
9

here is what to add to your build.xml as others stated:

<target name="-pre-build">
    <exec executable="${ndk.dir}/ndk-build" failonerror="true"/>
</target>

<target name="clean" depends="android_rules.clean">
    <exec executable="${ndk.dir}/ndk-build" failonerror="true">
        <arg value="clean"/>
    </exec>
</target>

define the ndk.dir in local.properties file: ndk.dir=C:\EclipseWorkspace\android-ndk-r8d

The situation i wanted to mention after doing this you get an error "%1 is not a valid Win32 application" while running ANT against this target override. For me i had to upgrade to NDK R8d and also update the following line so that it fetches ndk-build.cmd (this version of ndk can run on windows and via cygwin:

exec executable="${ndk.dir}/ndk-build.cmd" failonerror="true"

Dragonroot answered 25/2, 2013 at 0:4 Comment(5)
thanks j2emanue for helping out. But answer is given and accepted already :)Hylophagous
Hopefully this can help others was the intention, thanks though.Dragonroot
the '.cmd' part is important on windowsIneslta
for windows, [...executable="${ndk.dir}/ndk-build"...] should be, [...executable="${ndk.dir}/ndk-build.cmd"...] as per the comment by @Hoang TranPernell
Trying to use the Visual Studio installation of ant to build APK from C++ source, running ccmd:Ur

© 2022 - 2024 — McMap. All rights reserved.