1.创建Android Native C++工程
2.新建Native方法,并按照android studio提示,创建C/C++代码,如下所示:
c/c++代码会在native-lib.cpp中定义。
3.引入第三方so库
由于虚拟机环境下linux编译的so库不能运用在android平台,因此需要手动在android studio编译出一个so库,为了简单起见,实现一个int add(int, int)函数,返回两数之和。
方法:
第一步,在cpp目录下新建add.h,add.c文件,并实现相关逻辑,如下所示:
第二步,修改CMakeList.txt,增加对add编译,如下所示:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("androidnativecdemo")
add_library(add STATIC IMPORTED) #添加预编译静态库,只需要告诉CMAKE导入项目即可
add_library(XXX_beautyshot STATIC IMPORTED) #添加预编译静态库,只需要告诉CMAKE导入项目即可
add_library(mpbase STATIC IMPORTED) #添加预编译静态库,只需要告诉CMAKE导入项目即可
set_target_properties( # Specifies the target library.
add
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
${CMAKE_SOURCE_DIR}/../jniLibs/armeabi-v7a/libadd.so )
set_target_properties( # Specifies the target library.
XXX_beautyshot
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
${CMAKE_SOURCE_DIR}/../jniLibs/arm64-v8a/libXXX_beautyshot.so )
set_target_properties( # Specifies the target library.
mpbase
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
${CMAKE_SOURCE_DIR}/../jniLibs/arm64-v8a/libmpbase.so )
# 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 them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories("D:/AndroidNativeC++Demo/app/src/main/cpp")
add_library( # Sets the name of the library.
androidnativecdemo
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
#add_library( # Sets the name of the library.
# add
# Sets the library as a shared library.
# SHARED
# Provides a relative path to your source file(s).
# add.c)
#动态方式加载
#add_library( libadd1 SHARED IMPORTED )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries 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 this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
androidnativecdemo
add
XXX_beautyshot
mpbase
# Links the target library to the log library
# included in the NDK.`
${log-lib})
并在target_link_libraries增加add。
第三步,执行android make project命令,即可编译出libadd.so。
第四步,拷贝libadd.so,然后删除android工程下所有libadd.so。
第五步,在app目录下新建子目录,并将libadd.so拷贝其中,如下所示:
第六步,修改CMakeList.txt,增加对libadd.so导入,如下所示:
第七步,修改app目录下build.gradle,如下所示:
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.androidnativecdemo"
minSdk 16
targetSdk 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
}
//DRS 20160822h - Added sourceSets
sourceSets {
main {
jniLibs.srcDirs = ['D:\\AndroidNativeC++Demo\\app\\src\\main\\jniLibs']
}
}
packagingOptions {
pickFirst 'lib/arm64-v8a/libXXX_beautyshot.so'
pickFirst 'lib/arm64-v8a/libmpbase.so'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
第八步,修改JAVA代码,增加loadLibrary.(“add”),并修改native-lib.cpp,增加相关业务代码。