Cmake的配置
Android Studio 2.2以上支持了Cmake的配置JNI的相關參數,簡化了通過Android.mk配置。並很好的繼承了C++的編輯方式。以下是對應的引入第三方so和第三方.cpp文件的路徑腳本編寫。對應於:CMakeLists.txt
#定義變量ProjectRoot爲工程根目錄,用相對路徑沒通過編譯,可能是路徑寫錯,以後再試
#本次使用絕對路徑作爲參數
set(Project D:/GitHub/EchoCancellation)
#動態方式加載 speex是libxxxx.so的xxxx部分
add_library(speex SHARED IMPORTED)
#設置要連接的so的相對路徑,${ANDROID_ABI}表示so文件的ABI類型的路徑,這一步引入了動態加入編譯的so
set_target_properties(speex PROPERTIES IMPORTED_LOCATION
${Project}/app/src/main/jni/${ANDROID_ABI}/libspeex.so)
#配置加載native依賴
#include_directories( ${Project}/app/src/main/jni/include )
#將native-lib加入到編譯源中
add_library( # Sets the name of the library.
native-lib
SHARED
src/main/cpp/native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
log )
#這裏多出了引用的speex
target_link_libraries( # Specifies the target library.
native-lib
speex
# Links the target library to the log library
# included in the NDK.
${log-lib} )
build.gradle的配置
此處配置主要是我們寫的JNI層的代碼,可以通過編譯爲SO文件,被我們的項目引用。生成的so,在你對應項目的build中進行查找。
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
// moduleName "netContent"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['D:\\progress\\FaqRobotSdk_v2\\sdk_v2\\src\\main\\jniLibs']
}
}
}
signingConfigs {
release {
storeFile file('debug.jks')
storePassword "houshuai"
keyAlias "houshuai"
keyPassword "houshuai"
}
debug {
storeFile file('debug.jks')
storePassword "houshuai"
keyAlias "houshuai"
keyPassword "houshuai"
}
}
buildTypes {
release {
// 不顯示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
//混淆
minifyEnabled false
//Zipalign優化
zipAlignEnabled false
// 移除無用的resource文件
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release // 簽名配置
}
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}