JNI编程基础(二)——Android Studio NDK开发

时间:2022-08-01 08:57:34

由于笔者目前的工作是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:
JNI编程基础(二)——Android Studio NDK开发
如果没有安装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")
}
}