本次只是简单记录下怎么使用Android Studio3.0 编译C++成so,以备以后查看。
1.打开AS新建一个项目,这里见NativeBuild,勾选Include C++ support:
2.在app/src/main下新建一个jniLibs,将opencv android-sdk的库文件(这里使用so)拷贝到该目录下
3.将自己的c++代码放在app/src/main/cpp下,这里将整个c++代码目录src拷贝到这里
4.修改app/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.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
# 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(src/main/cpp
src/main/cpp/include
src/main/cpp/bc_api
src/main/cpp/bc_fillbg
src/main/cpp/bc_intelligent_fill_background
src/main/cpp/bc_utility)
file(GLOB IMGPROC_SRCS
src/main/cpp/bc_imgproc_jniapi.cpp
src/main/cpp/src/bc_api/*.cpp
src/main/cpp/src/bc_intelligent_fill_background/*.cpp
src/main/cpp/src/bc_utility/*.cpp)
add_library(opencv_java3
SHARED
IMPORTED)
set_target_properties(# Specifies the target library.
opencv_java3
# Specifies the parameter you want to define
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
add_library(imgproc SHARED ${IMGPROC_SRCS})
message(STATUS "******************************${IMGPROC_SRCS}")
target_link_libraries(imgproc log android -ljnigraphics m opencv_java3)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.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 )
# 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.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
5.配置build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "ndkbuild.nativetest"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
packagingOptions{
doNotStrip '*/mips/*.so'
doNotStrip '*/mips64/*.so'
}
sourceSets {
main {
jniLibs.srcDirs 'src/main/jniLibs'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
6.运行Make Project则生成对应的so