JNI編程基礎(二)——Android Studio NDK開發
由於筆者目前的工作是Android開發,所以在JNI開發係列博客中穿插一篇AndroidStudio NDK開發介紹。
隨著Android Studio 2.2的推出,Android Studio的NDK開發支持Cmake和ndk-build兩種方式,簡化了Android Studio上NDK開發流程,提升了開發效率。接下來就介紹下兩種開發方式。
1. CMake
CMake是一個跨平台的編譯(安裝)工具,支持通過用較為簡單的語言來描述編譯(安裝)過程,最後輸出適配不同平台的makefile貨project文件。Android Studio通過引入CMake來簡化JNI函數的編譯。接下來通過一個例子來展示基於CMake的Android Studio NDK開發:
1. 創建一個Android工程,創建時勾選支持c++開發
創建完成後會看到在工程中會有一個c++目錄,與Java目錄同級
2. 在c++目錄下創建兩個文件:jni_lic.cpp/h。
3.在c++目錄下創建CMakeLists.txt文件,文件內容如下:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
AUX_SOURCE_DIRECTORY(./src/main/cpp DIR_SRCS)
add_library( # Sets the name of the library.
jni-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
${DIR_SRCS}
)
include_directories(src/main/cpp/)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
jni-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
4. 在gradle中加入相關配置
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.asia.jni"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/cpp/CMakeLists.txt"
}
}
}
5.配置NDK路徑
file->project structure->SDK Location:
如果沒有安裝NDK,可以通過SDK Manager安裝。
6. JNI函數開發
這部分可以參考上一篇博客:JNI編程基礎(一)
NDK-BUILD
ndk-build方式與cmake方式類似,隻需要將cmake文件改寫為Android.mk和Appliction.mk文件。在CMakeLists.txt加載的位置將CMakeLists.txt替換為Android.mk即可
1.Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
APP_ABI := all
LOCAL_MODULE := jni-lib
LOCAL_CPPFLAGS := -O0 -D_UNICODE -DUNICODE -DUSE_DUMP -Wno-error=format-security
LOCAL_CPP_EXTENSION := .cpp
LOCAL_LDLIBS := -lm -llog -lz
LOCAL_SHORT_COMMANDS := true
INC_DIRS = -I$(LOCAL_PATH)/src/cpp
LOCAL_CPPFLAGS += $(INC_DIRS)
LOCAL_SRC_FILES := \
jni_lib.cpp \ \
LOCAL_SHARED_LIBRARIES += libandroid_runtime
include $(BUILD_SHARED_LIBRARY)
2.Application.mk
APP_ABI := all
NDK_TOOLCHAIN_VERSION := clang
APP_SHORT_COMMANDS := true
APP_STL := stlport_static
APP_CPPFLAGS := -std=gnu++11 -D__STDC_LIMIT_MACROS
3.替換gradle的配置中cmake
// externalNativeBuild{
// cmake{
// path file("src/main//CMakeLists.txt")
// }
// }
externalNativeBuild{
ndkBuild{
path file("src/main/cpp/Android.mk")
}
}
最後更新:2017-04-23 12:00:34