gradle.properties里面可以定义一些变量,以便在build.gradle中使用,举个例子:
systemProp.myDir=/home/myDir
在build.gradle中就可以使用这个变量,例如:
externalNativeBuild {
cmake {
version "3.10.2"
path file(System.properties['myDir']+'Src/CMakeLists.txt')
}
}
sourceSets {
main {
jniLibs.srcDirs System.properties['myDir']+'thirdLibs/ffmpeg'
}
}
显然System.properies['myDir']就是我们在gradle.properties里面定义的。
下面给定一个比较完善的build.gradle配置:
apply plugin: 'com.android.library'
android {
compileSdkVersion versions.compileSdkVersion
buildToolsVersion versions.buildToolsVersion
android.ndkVersion versions.ndkVersion
defaultConfig {
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
externalNativeBuild {
cmake {
abiFilters "arm64-v8a"
cppFlags "-std=c++11 -frtti -fexceptions"
arguments "-DANDROID_STL=c++_shared", "-DANDROID_PLATFORM=android-21"
}
}
ndk {
abiFilters "arm64-v8a"
}
}
externalNativeBuild {
cmake {
version "3.10.2"
//这里可以指定自己定义的CMakefile
path file(System.properties['myDir']+'Src/CMakeLists.txt')
}
}
sourceSets {
main {
//这里可以指定依赖库的路径
jniLibs.srcDirs System.properties['myDir']+'thirdlibs/ffmpeg'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
api project(':libCommon')
implementation fileTree(dir: 'libs', include: ['*.jar'])
api "com.github.bumptech.glide:glide:4.11.0"
annotationProcessor "com.github.bumptech.glide:compiler:4.11.0"
implementation ('com.github.bumptech.glide:okhttp3-integration:4.4.0'){
exclude group: 'glide-parent'
}
}
下面给出cmakelist实现,myDir/Src/CmakeList.txt实现:
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(NLEPlatformPro CXX)
add_definitions(-DTARGET_IPHONEOS)
add_definitions(-DANDROID_NDK)
#这些是库路径,这些路径下面有各自的CMakeList
add_subdirectory(Externlib/libOne)
add_subdirectory(Externlib/libTwo)
add_subdirectory(project/libCore/src/main/cpp)
libOne中的Cmakelist.txt举例
# 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.0)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(oneLib CXX)
add_definitions(-DMARKUP_STDCONV)
#匹配cpp
file(GLOB ComSupportSources
"Android/ComSupport/*.cpp" "Android/Common/Audio/*.cpp" "Android/Common/ComSupport/*.cpp" "Android/Common/Image/*.cpp")
file(GLOB AndroidSources "Android/*.cpp" "Util/*.cpp")
file(GLOB AndroidJNISources "Android/Jni/*.cpp"
# 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.
add_library( # Sets the name of the library.
AndrCodec
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${ComSupportSources}
${AndroidSources}
${AndroidJNISources}
../Base/COM/mediaInfo.cpp
)
# 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)
find_library(
jnigraphics-lib
jnigraphics
)
# 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_include_directories(oneLib PRIVATE
Android
Android/Jni
Android/Common/Audio
../../../SDK/ffmpeg/include
)
add_library(lib_opencv SHARED IMPORTED)
if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
set(OPENCV_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../SDK/opencv/armeabi-v7a/libopencv_java4.so)
else()
set(OPENCV_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../SDK/opencv/arm64-v8a/libopencv_java4.so)
endif()
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OPENCV_PATH})
add_library(ffmpeg SHARED IMPORTED)
add_dependencies(${PROJECT_NAME}
NLELogger
)
set_target_properties(ffmpeg
PROPERTIES IMPORTED_LOCATION
${FFMPEG_PATH})
target_link_libraries( # Specifies the target library.
${PROJECT_NAME} PRIVATE
ffmpeg
lib_opencv
${jnigraphics-lib}
${log-lib}
)