Skip to content

Instantly share code, notes, and snippets.

@Tydus
Last active May 4, 2023 13:43
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Tydus/11109634 to your computer and use it in GitHub Desktop.
Save Tydus/11109634 to your computer and use it in GitHub Desktop.
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
@w1252675615
Copy link

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

@vivek-lakhataria
Copy link

i have issue in ndk-12b
command -: make TARGET=android-x86 build_spatialite build_gdal build_commoncommo build_assimp
make TARGET=android-x86 build_spatialite build_gdal build_commoncommo build_assimp
/home/yash/ndk/build/tools/make-standalone-toolchain.sh
--platform=android-21
--arch=x86
--install-dir=/home/yash/AndroidTacticalAssaultKit-CIV/takthirdparty/builds/android-x86-release/toolchain
make: /home/yash/ndk/build/tools/make-standalone-toolchain.sh: Command not found
make: *** [target-config/android-x86.mk:11: /home/yash/AndroidTacticalAssaultKit-CIV/takthirdparty/builds/android-x86-release/toolchain] Error 127

@Mohamedeijy
Copy link

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

Does someone have an answer concerning this ? Is it possible to specify other architectures ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment