25

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.

frogatto
  • 28,539
  • 11
  • 83
  • 129
user492922
  • 925
  • 2
  • 12
  • 23
  • Generally you run ndk-build from within your project - does it work if you do this? – Scott C Wilson May 02 '11 at 16:13
  • I ran ndk-build and it complained about NDK_PROJECT_PATH – user492922 May 02 '11 at 19:29
  • What version of the NDK are you using? – Scott C Wilson May 02 '11 at 19:57
  • Can you set NDK_PROJECT_PATH to the actual path of your project (say $NDK_DIR/samples/your-project, where $NDK_DIR is the path to the NDK)? – Scott C Wilson May 03 '11 at 12:22
  • 1
    I do not have much Android development experience so I am not sure what are the minimal constituents of an Android project. Is an Android.mk along with my source files sufficient? Currently I am setting my NDK_PROJECT_PATH to the directory of my source files and Android.mk. Why should it be a subdirectory of the ndk? – user492922 May 03 '11 at 16:21
  • Well I changed the directory structure putting everying in $NDK/samples/su-binary/jni/ and adb runs but the compile fails for other reasons (I will post a follow up question). – user492922 May 03 '11 at 23:35

2 Answers2

9

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,

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • `$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain` may throw a error `Unable to auto-config arch from toolchain`,accoding – Glowin Feb 12 '16 at 13:20
  • `$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain` may throw a error `Unable to auto-config arch from toolchain`,accoding to this answer http://stackoverflow.com/questions/29444079/unable-to-auto-config-arch-from-toolchain ,should add `--toolchain=arm-linux-androideabi-4.8` https://gist.github.com/Tydus/11109634 – Glowin Feb 12 '16 at 14:16
4

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.

onlyxool
  • 41
  • 3
  • Did you mean to add some formatting to this? Your folder structure isn't very clear. – Dutts Mar 12 '13 at 12:26
  • Thank you for your answer. That simple directory structure is enough to make ndk-build work. In my case I was trying to compile a static lib to be used in another project, and after running ndk-build I found the .a file under project_root/obj/local/armeabi. – damix911 Dec 30 '14 at 15:36