天天看点

NDK r19 to build standalone app/HelloWorld on Android

1. Standalone toolchain from NDK r19/2019

Google/https://developer.android.com/ndk/guides/other_build_systems

Using the NDK with other build systems

Note: The content described in this document requires at least NDK r19. If you're using an older NDK, consider upgrading. If you're unable to upgrade, see the Standalone Toolchain guide.

https://developer.android.com/ndk/guides/standalone_toolchain

Standalone Toolchains

Warning: If using r19 or newer, follow the Other Build Systems document for instructions on using the NDK toolchains with arbitrary build systems. As of r19, the NDK's default toolchains are standalone toolchains, which renders this process unnecessary.

2. Clang VS GCC toolchain from NDKn11/2016

Android NDK r11 recommends clang instead of GCC/deprecated/4.9

3. ############## Linux/AOSP7.1.2/NDK prebuilts/arm-linux-androideabi-4.9-GCC 

   ### this is platform build with AOSP 

[email protected]:~/hello$ which  arm-linux-androideabi-gcc

/localrepo/lake/fmsng/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc

### Note  please source   build/envsetup.sh    and launch  xxxxxx

Build 

arm-linux-androideabi-gcc  hello.c   --sysroot=/localrepo/lake/fmsng/prebuilts/ndk/current/platforms/android-24/arch-arm     -o  hellondk  

[email protected]:~/hello$ file hellondk 

hellondk: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/, not stripped

[email protected]:~/hello$ 

3.1. Android version before Android LL 5.0 

[email protected]:~/hello$ adb    push    hellondk  /data/local/tmp/

hellondk: 1 file pushed. 0.8 MB/s (6744 bytes in 0.008s)

[email protected]:~/hello$ adb shell   chmod  a+x   /data/local/tmp/hellondk

[email protected]:~/hello$ adb shell   /data/local/tmp/hellondk

Hello World

3.2. For Android version after Android LL5.0, addd CFLAG+= -fPIE -pie  

[email protected]:~/hello$ adb shell   /data/local/tmp/hellondk

"/data/local/tmp/hellondk": error: only position independent executables (PIE) are supported.

[email protected]:~/hello$ arm-linux-androideabi-gcc  hello.c   --sysroot=/localrepo/lake/fmsng/prebuilts/ndk/current/platforms/android-24/arch-arm     -o  hellondk   -fPIE -pie  

[email protected]:~/hello$ adb push hellondk   /data/local/tmp/[100%] /data/local/tmp/hellondk

[email protected]:~/hello$ adb shell   chmod  a+x   /data/local/tmp/hellondk

[email protected]:~/hello$ adb shell   /data/local/tmp/hellondk

Hello World

[email protected]:~/hello$ 

4. Window NDK build n19/2019 arm-linux-androideabi-4.9-clang (llvm + clang)

C:\work\Hello-Git\Hello\src>C:\Android\AndroidStudio\android-sdk-windows\ndk-bundle\toolchains\llvm\prebuilt\windows-x86

_64\bin\armv7a-linux-androideabi24-clang.cmd     Hello.c   -o hellondk

C:\work\Hello-Git\Hello\src>C:\Android\AndroidStudio\android-sdk-windows\ndk-bundle\toolchains\arm-linux-androideabi-4.9

\prebuilt\windows-x86_64\bin\arm-linux-androideabi-readelf.exe  -s   hellondk

Symbol table '.dynsym' contains 8 entries:

   Num:    Value  Size Type    Bind   Vis      Ndx Name

     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND

     1: 00000000     0 FUNC    GLOBAL DEFAULT  UND [email protected] (2)

     2: 00000000     0 FUNC    GLOBAL DEFAULT  UND [email protected] (2)

     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND [email protected] (2)

     4: 00000000     0 FUNC    GLOBAL DEFAULT  UND [email protected] (2)

     5: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata

     6: 00002030     0 NOTYPE  GLOBAL DEFAULT  ABS _end

     7: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start

Symbol table '.symtab' contains 37 entries:

   Num:    Value  Size Type    Bind   Vis      Ndx Name

     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND

     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtbegin.c

     2: 000003cc     0 NOTYPE  LOCAL  DEFAULT   11 $a.0

     3: 00000428     0 NOTYPE  LOCAL  DEFAULT   11 $a.2

     4: 00000454     0 NOTYPE  LOCAL  DEFAULT   11 $a.4

     5: 00000418     0 NOTYPE  LOCAL  DEFAULT   11 $d.1

     6: 0000044c     0 NOTYPE  LOCAL  DEFAULT   11 $d.3

     7: 00000460     0 NOTYPE  LOCAL  DEFAULT   11 $d.5

     8: 000003d4    84 FUNC    LOCAL  DEFAULT   11 _start_main

     9: 000001c0     0 NOTYPE  LOCAL  DEFAULT    2 ndk_build_number

    10: 00000180     0 NOTYPE  LOCAL  DEFAULT    2 ndk_version

    11: 00000168   152 OBJECT  LOCAL  DEFAULT    2 note_android_ident

    12: 0000017c     0 NOTYPE  LOCAL  DEFAULT    2 note_data

    13: 00000200     0 NOTYPE  LOCAL  DEFAULT    2 note_end

    14: 00000174     0 NOTYPE  LOCAL  DEFAULT    2 note_name

    15: 00000000     0 FILE    LOCAL  DEFAULT  ABS Hello.c

    16: 00000464     0 NOTYPE  LOCAL  DEFAULT   11 $a.0

    17: 00000498     0 NOTYPE  LOCAL  DEFAULT   11 $d.1

    18: 00000428    12 FUNC    LOCAL  HIDDEN    11 __atexit_handler_wrapper

    19: 0000202c     4 OBJECT  LOCAL  HIDDEN    19 __dso_handle

    20: 00000434    32 FUNC    LOCAL  HIDDEN    11 atexit

    21: 00000454    16 FUNC    LOCAL  HIDDEN    11 pthread_atfork

    22: 00001f08   248 OBJECT  LOCAL  HIDDEN    17 _DYNAMIC

    23: 00002010     0 OBJECT  LOCAL  HIDDEN    18 _GLOBAL_OFFSET_TABLE_

    24: 00000388     0 NOTYPE  LOCAL  DEFAULT   10 $a

    25: 00001f00     4 OBJECT  GLOBAL DEFAULT   16 __FINI_ARRAY__

    26: 00001ef8     4 OBJECT  GLOBAL DEFAULT   15 __INIT_ARRAY__

    27: 00001ef0     4 OBJECT  GLOBAL DEFAULT   14 __PREINIT_ARRAY__

    28: 00000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit

    29: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_init

    30: 00000000     0 FUNC    GLOBAL DEFAULT  UND __register_atfork

    31: 000003cc     8 FUNC    GLOBAL DEFAULT   11 _start

    32: 00000464    56 FUNC    GLOBAL DEFAULT   11 main

    33: 00000000     0 FUNC    GLOBAL DEFAULT  UND printf

    34: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata

    35: 00002030     0 NOTYPE  GLOBAL DEFAULT  ABS _end

    36: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start

C:\work\Hello-Git\Hello\src>

C:\work\Hello-Git\Hello\src>C:\Android\AndroidStudio\android-sdk-windows\ndk-bundle\toolchains\arm-linux-androideabi-4.9

\prebuilt\windows-x86_64\bin\arm-linux-androideabi-nm.exe     hellondk

00000428 t __atexit_handler_wrapper

0000202c A __bss_start

         U __cxa_atexit

0000202c b __dso_handle

00001f00 T __FINI_ARRAY__

00001ef8 T __INIT_ARRAY__

         U __libc_init

00001ef0 D __PREINIT_ARRAY__

         U __register_atfork

00001f08 d _DYNAMIC

0000202c A _edata

00002030 A _end

00002010 d _GLOBAL_OFFSET_TABLE_

000003cc T _start

000003d4 t _start_main

00000434 t atexit

00000464 T main

000001c0 r ndk_build_number

00000180 r ndk_version

00000168 r note_android_ident

0000017c r note_data

00000200 r note_end

00000174 r note_name

         U printf

00000454 t pthread_atfork

C:\work\Hello-Git\Hello\src> 

C:\work\Hello-Git\Hello\src>c:\Android\adb\adb.exe   push    hellondk  /data/local/tmp/

hellondk: 1 file pushed. 0.8 MB/s (6744 bytes in 0.008s)

C:\work\Hello-Git\Hello\src>adb shell   chmod  a+x   /data/local/tmp/hellondk

C:\work\Hello-Git\Hello\src>adb shell   chmod  a+x   /data/local/tmp/hellondk

C:\work\Hello-Git\Hello\src>adb shell     /data/local/tmp/hellondk

Hello world!

C:\work\Hello-Git\Hello\src>