Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile simple native code using Android.mk?

Tags:

In particular I am trying to compile chainDD's su binary. I tried to use ndk-build but it seems I need to set NDK_PROJECT_PATH but what this should be set to is not described in the documentation.

like image 901
user492922 Avatar asked May 02 '11 11:05

user492922


People also ask

How does an Android APK execute compiled native code using Java native interface?

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.

What is Android native code?

Android native code is C/C++. Here is a bit of info about SDK(NDK). And here you can find general overview of NDK(native development kit).

How does Android.mk work?

The syntax of the Android.mk allows you to group your sources into modules. A module is either a static library, a shared library, or a standalone executable. You can define one or more modules in each Android.mk file, and you can use the same source file in multiple modules.

What is difference between Android NDK and SDK?

Android provides Native Development Kit (NDK) to support native development in C/C++, besides the Android Software Development Kit (Android SDK) which supports Java. [TODO] more. NDK is a complex and advanced topics.


2 Answers

First, make sure you have the NDK:

http://developer.android.com/tools/sdk/ndk/index.html

Here is the easiest way to compile a C binary for your phone:

http://developer.android.com/tools/sdk/ndk/index.html

http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html

Usually $NDK(may be different) =

Linux:

/home/<user>/android-ndk

Mac OS X:

/Users/<user>/android-ndk

Both: $HOME/android-ndk

In Terminal:

# create tool-chain - one line $NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain  # add to terminal PATH variable export PATH=/tmp/my-android-toolchain/bin:$PATH  # make alias CC be the new gcc binary export CC=arm-linux-androideabi-gcc  # compile your C code(I tried hello world) $CC -o foo.o -c foo.c  # push binary to phone adb push foo.o /data/local/tmp  # execute binary adb /data/local/tmp/foo.o 

Please let me know if I can help!

Regards,

like image 61
Jared Burrows Avatar answered Oct 01 '22 11:10

Jared Burrows


You need establish your project folder like this:

project_root

|__ jni/ (include Android.mk and your C/C++ code)

|__ other_directory

The jni directory can't change name. and run ndk-build in project_root directory.

like image 42
onlyxool Avatar answered Oct 01 '22 13:10

onlyxool