Archive for the ‘ndk’ Category

android: ndk: use cmake to build a native module

April 26, 2017

This post demonstrates building a native module for android devices with cmake

host configuration
OS X El Captain Version 10.11.4

target configuration
[ro.build.version.release]: [5.1]
[ro.product.cpu.abi]: [armeabi-v7a].

how to build native code with ndk-build

  • Download NDK(Native Development Kit)
  • NDK of different releases could be founded from https://developer.android.com/ndk/downloads/index.html

    $ wget https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
    $ unzip https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
    
  • Create project directory
  • $ mkdir project
    $ cd project/
    
  • Edit hello.c
  • #include <stdio.h>
    
    int main() {
        printf("hello world\n");
        return 0;
    }
    
  • Edit CMakeLists.txt
  • PROJECT (hello)
    ADD_EXECUTABLE (hello hello.c)
    
  • Build hello module
  • $ mkdir build
    $ cd build
    $ cmake -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=../../android-ndk-r13b -DCMAKE_TOOLCHAIN_FILE=../../android-ndk-r13b/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 ..
    cmake -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=../../android-ndk-r13b -DCMAKE_TOOLCHAIN_FILE=../../android-ndk-r13b/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 ..
    -- Check for working C compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
    -- Check for working C compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
    -- Check for working CXX compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /Users/chengyihe/workspace/cmake-ndk/project/build
    $ make
    Scanning dependencies of target hello
    [ 50%] Building C object CMakeFiles/hello.dir/hello.c.o
    [100%] Linking C executable hello
    [100%] Built target hello
    
  • Run the hello executable in the target device
  • $ adb push hello /data/local/tmp/hello
    [100%] /data/local/tmp/hello
    $ adb shell /data/hello
    WARNING: linker: /data/hello: unused DT entry: type 0x6ffffffe arg 0x378
    WARNING: linker: /data/hello: unused DT entry: type 0x6fffffff arg 0x1
    hello world
    

conclusion
This post show how to build a native executable for android devices with cmake and ndk.

android: ndk: use ndk-build to build a native module

April 26, 2017

This post demonstrates building a native module with ndk-build.

host configuration
OS X El Captain Version 10.11.4

target configuration
[ro.build.version.release]: [5.1]
[ro.product.cpu.abi]: [armeabi-v7a]

what is ndk
Native Development Kit to build native code for android

what is ndk-build
A convenient script in NDK to build native code

how to build native code with ndk-build

  1. Download NDK(Native Development Kit)
  2. NDK of different releases could be founded from https://developer.android.com/ndk/downloads/index.html

    $ wget https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
    $ unzip https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
    
  3. Edit hello.c
  4. #include <stdio.h>
    
    int main() {
        printf("hello world\n");
        return 0;
    }
    
  5. Edit Android.mk
  6. LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_CFLAGS += -fPIE
    LOCAL_LDFLAGS += -fPIE -pie
    LOCAL_MODULE := hello
    LOCAL_SRC_FILES := hello.c 
    
    include $(BUILD_EXECUTABLE)
    
  7. Edit Application.mk
  8. APP_ABI := armeabi-v7a
    APP_PLATFORM := 21
    APP_BUILD_SCRIPT := Android.mk
    
  9. Build native module
  10. $ ../android-ndk-r13b/ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=./Application.mk 
    [armeabi-v7a] Compile thumb  : hello <= hello.c
    [armeabi-v7a] Executable     : hello
    [armeabi-v7a] Install        : hello => libs/armeabi-v7a/hello
    
  11. Run the module in target device
  12. $ adb push libs/armeabi-v7a/hello /data/
    [100%] /data/hello
    $ adb shell /data/hello
    WARNING: linker: /data/hello: unused DT entry: type 0x6ffffffe arg 0x38c
    WARNING: linker: /data/hello: unused DT entry: type 0x6fffffff arg 0x1
    hello world
    

conclusion
This post show how to build a native executable with ndk-build.


%d bloggers like this: