Android JNI Learning Note (iii)-Compiling files android.mk, application.mk, and Camke

Source: Internet
Author: User

1. Preface

In android2.2, joined the CMake compilation, and used to be android.mk, APPLICATION.MK, today to record their configuration options.

2. Android.mk

ANDROID.MK is used in the JNI directory to describe the source files of the build system and
Shared libraries. The file format is as follows:

    • Start with a Local_path variableLOCAL_PATH := $(call my-dir)
    • followed by the Clear_vars variableinclude $(CLEAR_VARS)
    • Next Local_module variable, define the name of the so file to be output, by default, so is the Lib+local_module variable value +.so, if the variable value is preceded by LIB, it will not be added, or the value of the variable after the. XXX will also be removed.
    • Next is the Local_src_files variable, which declares our original file path, such asLOCAL_SRC_FILES := hello-jni.c
    • The last line is to help build the system to link together.include $(BUILD_SHARED_LIBRARY)
LOCAL_PATH:$(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE:= hello-jniLOCAL_SRC_FILES:= hello.cppinclude$(BUILD_SHARED_LIBRARY)

Of course, above is just one of the simplest, let's introduce some other variables and macros.

The build system provides many variables and macros, and of course allows us to customize it with the following three types of built-in:

    • Start with Local_, such as Local_module
    • To Private_, Ndk_, or APP
    • Lowercase letters, such as My-di

If you want to customize it, it is recommended that my_ begin.

2.1 NDK Default variables
    • Clear_vars is used to introduce this script before describing the new model, which clears the previous valueinclude $(CLEAR_VARS)
    • Build_shared_library, tell the build system to collect the value of the declared Local_ variable and output it asinclude $(BUILD_SHARED_LIBRARY)
    • Build_static_library, similar to build_shared_library, but not copied to project/packages, but can be supplied to shared libraries and output as. Ainclude $(BUILD_STATIC_LIBRARY)
    • Prebuilt_shared_library is used to specify pre-compiled shared libraries, but Local_src_files is the so fileinclude $(PREBUILT_SHARED_LIBRARY)
    • Prebuilt_static_library and Prebuilt_shared_library are similar
    • Target_arch a little, focus on Target_arch_abi
    • Target_platform specifying the currently compiled Android API versionTARGET_PLATFORM := android-22
    • TARGET_ARCH_ABI Specifies the CPU architecture,TARGET_ARCH_ABI := arm64-v8a
    • Target_abi, specify the Android API version of the Fish ABI architecture,TARGET_ABI := android-22-arm64-v8a
2.2 Module-description Variables describes the variables of the model
    • LOCAL_PATH specifies the path of the current file, must be specified at the beginning of the file LOCAL_PATH := $(call my-dir) , note clear_vars, and will not clear this value
    • Local_module
    • Local_module_filename can specify the name of the resulting so file
    • Local_src_files specify the original file corresponding to this model
    • Local_cpp_extension Config C + + file suffix (extension), C + +, CC, or other
    • LOCAL_CPP_FEATURES Specifies specific C + + features such as support for RTTI (RunTime Type information),LOCAL_CPP_FEATURES := rtti
    • LOCAL_C_INCLUDES Specifies the path list, relative to the NDK's path
    • Local_cflags, Local_cppflags can specify additional macro definitions and compilation options
    • Local_static_libraries, local_shared_libraries Specify other STATIC LIBRARIES, SHARED LIBRARIES
    • Local_whole_static_libraries the whole.
    • LOCAL_LDLIBS Specifies the system-L designation system library, such as/system/lib/libz.soLOCAL_LDLIBS := -lz
    • Local_ldflags, I didn't see it.
    • Local_allow_undefined_symbols by default, when the build system encounters an undefined reference in an attempt to establish a share, it throws an undefined symbol error. This error can help Debug.
    • The rest of the many are not used, temporarily here, after the opportunity to use, check the document it.
3. Application.mk

The native model used to describe the app's needs.

3.1 Variables
    • App_project_path This variable stores the absolute path of the application's project root directory.
    • App_optim Configuring Release and Debug
    • App_cflags This variable stores a set of build system C compiler flags passed to the compiler to compile any module of any C or C + + source code, you can modify the application needs of the build module without modifying the Android.mk file
    • App_cppflags and App_cflags are similar
    • App_ldflags A set of linker flags that the build system passes when linking the application, only for shared libraries and Executab Les effective
    • App_build_script Specifying ANDROID.MK files
    • App_abi Specifying ABI
    • App_platform specifying the Android API version
    • App_stl links to other C + + support
    • Ndk_toolchain_version GCC compiled version
    • App_pie
    • App_thin_archive
4. Using in Android Studio

Requires Android Studio 2.2 or more.

In the Gradle,

android {  defaultConfig {      externalNativeBuild {      cmake {        "-DVAR_NAME=VALUE"        "-DANDROID_ARM_NEON=TRUE""-DANDROID_TOOLCHAIN=clang"      }    }    // 设置 abi    ndk {            "armeabi","x86","armeabi-v7a"        }  }  buildTypes {...}  externalNativeBuild {    cmake {        // CMakeLists.txt 文件路径        ‘src/main/jni/CMakeLists.txt‘     }  }}

What we need to write is the three places with comments above.

    • CMake parameter format is-D + Variable name = Arguments situation
      • Android_toolchain cmake compilation chain, GCC and clang (default) Two
      • Android_platform Target Android PLATFORM
      • Android_stl CMake compile with which STL, there are the following kinds of helper runtimes
      • ANDROID_PIE Specifies whether to use the location-independent executable (PIE). Android's dynamic Linker supports dispatch on Android 4.1 (API level 16) and higher.
      • ANDROID_CPP_FEATURES specifies a specific C + + attribute CMake compile with local libraries, such as C + + RTTI (runtime type information) and exceptions, rtti,exceptions
      • ANDROID_ALLOW_UNDEFINED_SYMBOLS Specifies whether to throw an undefined symbol error if cmake encounters an undefined reference and builds your local library. Disable these types of errors and set this variable to true.
      • Android_arm_mode set the generated binaries ARM or thumb mode, thumb mode, each instruction is in 16bits,arm mode for 32 bits, default is thumb
      • Ndroid_arm_neon Build native Lib is none supported
      • Android_disable_no_execute whether to allow NE bit, or to perform, or to secure a special training
      • Whether the ANDROID_DISABLE_RELRO is read-only
      • ANDROID_DISABLE_FORMAT_STRING_CHECKS Specifies whether to compile the protection of source code with the format string. When enabled, the compiler throws an error if the Printf-style function is used in a non-constant format string.
    • NDK abifilters
    • CMake Path

About CMake parameters, official documents

5. Written by CMakeLists.txt
    • cmake_minimum_required(VERSION 3.4.1)Minimum version of CMake
    • add_library(native lib name,SHARED(SHARED还是STATIC),c++或c文件路径)
    • include_directories(src/main/cpp/include/)Specifying the header file path
Add native API
ofvariable that stores the              oflibrary.              log-lib              oflibrary that              to locate.              log )

According to my own understanding,

    • The first one is the Lib library alias, which we want to use elsewhere in this file.
    • The second parameter is the name of the corresponding native Lib library, and the second parameter can be found in the Ndk-bundle/platforms/android version/below. According to the above-mentioned generation of so file rules, the Lib name can be clearly presented

Then use Target_link_libraries (native-lib,${log-lib}) to link our local libraries with the local libraries in the NDK,

You can also add the source code in,

add_library( app-glue             STATIC             ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
    • Lib Name
    • Type
    • File path
Add other pre-built libraries

Because these already exist, need to use imported to tell Cmkae, only need to import this lib into our project

add_library( imported-lib             SHARED             IMPORTED )

Then you need to use Set_target_properties to specify the path.

set# Specifies the target library.                       imported-lib                       # Specifies the parameter you want to define.                       PROPERTIES IMPORTED_LOCATION                       # Provides the path to the library you want to import.                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so )
    • Lib Name
    • Specifying parameters
    • Specify the path to so

At this time need include_directories to specify so corresponding header file path, also mentioned above.

6. Summary

There is no understanding of the wrong, we point out that common learning common progress.

Resources:

    • ANDROID.MK, Documentation
    • APPLICATION.MK Documentation
    • Android Document CMake Documentation
    • Introduced in Android Studio

Android JNI Learning Note (iii)-Compiling files android.mk, application.mk, and Camke

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.