AndroidStudio搭建好开发环境后,开始开发NDK,需配置NDK
1**.NDK build.gradle配置**
android {
compileSdkVersion 23
buildToolsVersion ‘23.0.3’
defaultConfig {
minSdkVersion 18
targetSdkVersion 23
//NDK编译方法1 NDK配置``
//ndk {
// moduleName "libcamera"
// abiFilters "armeabi", "armeabi-v7a"
//}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
//SRC配置
sourceSets {
main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = []
}
}
//NDK编译方法1
//externalNativeBuild {
// ndkBuild {
// path file("src/main/jni/Android.mk")
// }
//}
//NDK编译方法2
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
println('executing ndkBuild')
def ndkBuildPath = android.ndkDirectory;
commandLine "$ndkBuildPath/ndk-build.cmd", '-j2', '-C', file('src/main').absolutePath
}
task ndkClean(type: Exec, description: 'clean JNI libraries') {
println('executing ndkBuild clean')
def ndkBuildPath = android.ndkDirectory;
commandLine "$ndkBuildPath/ndk-build.cmd", 'clean', '-C', file('src/main').absolutePath
}
clean.dependsOn 'ndkClean'
/*
单线程:
//NDK编译方法
task buildNative(type: Exec, description: ‘Compile JNI source via NDK’) {
def ndkDir = android.ndkDirectory
commandLine “$ndkDir/ndk-build.cmd”,
‘-C’, file(‘src/main/jni’).absolutePath//jni 路径
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni').absolutePath//jni路径
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
/
}
dependencies {
compile fileTree(dir: new File(buildDir, ‘libs’), include: '.jar’)
}重点内容
2. src/main/jni Android.mk以及Application.mk配置 针对编译方法2
2.1
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/.. \
$(LOCAL_PATH)/include
LOCAL_CFLAGS := $(LOCAL_C_INCLUDES:%=-I%)
LOCAL_EXPORT_LDLIBS := -llog
LOCAL_SRC_FILES := \
empty.c
LOCAL_MODULE := libempty
include $(BUILD_SHARED_LIBRARY)
2.2 Application.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
APP_PLATFORM := android-19
APP_ABI := armeabi-v7a arm64-v8a
#APP_OPTIM := debug
#APP_OPTIM := release
include $(BUILD_EXECUTABLE)