Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to install Standalone toolchain for Android

HOWTO Cross compiling on Android

5W1H

What is NDK

NDK (Native Develop Toolkit) is a toolchain from Android official, originally for users who writes native C/C++ code as JNI library. It's not designed for compiling standalone programs (./a.out) and not compatible with automake/cmake etc.

What is Standalone Toolchain

"Standalone" refers to two meanings:

  1. The program is standalone (has nothing connect to NDK, and don't need helper scripts to run it)
  2. The toolchain is made for building standalone programs and libs, and which can used by automake etc.

(Optional) Why NDK is hard to use

By default, NDK uses android flavor directory structure when it's finding headers and libs, which is different from GNU flavor, so the compiler cannot find them. For Example:

/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: error: ld returned 1 exit status

Although we can manuall specify the path (someone wrote a program called "agcc" to handle this automatically, but still not good), it's really annoying.

Howto

  1. Download Android NDK
    from https://developer.android.com/tools/sdk/ndk/index.html
  2. Extract the NDK
    tar xf android-ndk-r9d-*.tar.bz2 && cd android-ndk-r9d
  3. Make GNU Android Toolchain from NDK
    build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.8 --platform=android-19 --install-dir=../toolchain
  4. Delete the NDK (Yes, we don't need it any more)
    cd .. && rm -rf android-ndk-r9d
  5. Test the native toolchain
    cd toolchain/bin
    echo "main(){}" | ./arm-linux-androideabi-gcc -x c -
    file a.out # a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
  6. (Optional) Now you can use it as a standard GNU toolchain
    For example: ./configure --prefix=/opt/android --host=arm-linux-androideabi && make && make install
@nikhilkilivayil

This comment has been minimized.

Copy link

nikhilkilivayil commented Feb 5, 2016

I am getting this issue :
/home/umn/android/android-ndk-r7c/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lssl
collect2: ld returned 1 exit status

What will do?

@MichaelSX

This comment has been minimized.

Copy link

MichaelSX commented Apr 13, 2016

Check if you have libssl-dev installed when using ubuntu reffering to this thread (even though it isn't about android, but still about your issue).

@usman313

This comment has been minimized.

Copy link

usman313 commented May 1, 2016

I am getting this error each time I try to do any work on Android Studio 2.1 and everything in each activity become red. How can I resolve this issue??
untitled

@ranshalit

This comment has been minimized.

Copy link

ranshalit commented Feb 14, 2017

That's amazing. Thank you very much. I don't have to build now with "-static". Right ?

@hannesa2

This comment has been minimized.

Copy link

hannesa2 commented Dec 28, 2017

I downloaded android-ndk-r16b and after make-standalone-toolchain.sh the command
in step .5 echo "main(){}" | ./arm-linux-androideabi-gcc -x c - shows nothing.

Is this right, or did I something wrong ?

@AxelNennker

This comment has been minimized.

Copy link

AxelNennker commented Apr 20, 2018

@hannesa2
echo "main(){}" | ./arm-linux-androideabi-gcc -x c - && file a.out

@KrishnaCAmjuri

This comment has been minimized.

Copy link

KrishnaCAmjuri commented Jun 2, 2018

Is this procedure valid for mac(unix) also?

@unalkalkan

This comment has been minimized.

Copy link

unalkalkan commented Jun 3, 2018

Tried in Debian 9, works like a charm!

@KrishnaCAmjuri

This comment has been minimized.

Copy link

KrishnaCAmjuri commented Jun 4, 2018

This will only build the toolchain for one architecture that is "armv7-a". What about the other architectures? Can you please clarify?

@bybilly

This comment has been minimized.

Copy link

bybilly commented Aug 14, 2018

I am having this issue:
"
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can't open name: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_abstract_Specify: not found
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can't open name: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_abstract_Specify: not found
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can't open path: No such file or directory
./make-standalone-toolchain.sh: can't open path: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_default_.=: not found
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can't open name: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_abstract_Specify: not found
./make-standalone-toolchain.sh: OPTIONS_default_linux-armv7l=: not found
[and there's more]
"

At the top it says "WARNING: The shell running this script isn't bash. Although we try to avoid bashism in scripts, things can happen."
At the bottom it says that "--toolchain" is an unknown option. Even if --help is the first parameter it says it is an unknown option.

I'd appreciate any help. What can I do?

@tonyw5

This comment has been minimized.

Copy link

tonyw5 commented Aug 14, 2018

Tonys-MacBook-Pro:~ tonywilliams$ /Users/tonywilliams/android-ndk-r17b/build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.8 --platform=android-19 --install-dir=../toolchain
HOST_OS=darwin
HOST_EXE=
HOST_ARCH=x86_64
HOST_TAG=darwin-x86_64
HOST_NUM_CPUS=4
BUILD_NUM_CPUS=8
Auto-config: --arch=arm
ERROR: Failed to create toolchain.

When trying to build the toolchain arm I am getting this error...Why is that?

@jainabhinav249

This comment has been minimized.

Copy link

jainabhinav249 commented Mar 5, 2019

i am using android-ndk 14 and i am getting the clang:error: unknown argument: '-mandroid' can any one help me

@jainabhinav249

This comment has been minimized.

Copy link

jainabhinav249 commented Mar 5, 2019

i an trying to build android-27 version

@w1252675615

This comment has been minimized.

Copy link

w1252675615 commented Sep 29, 2019

hello, i got "ignoring DT_PREINIT_ARRAY in shared library" when run my app on my phone, can you offer me some help or suggestion? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.