源码链接
添加个cpp文件后要Refresh Linked C++ Projects
用gradle-ndk-build:
import org.apache.tools.ant.taskdefs.condition.Os
android {
compileSdkVersion versionCompiler
buildToolsVersion versionBuildTool
compileOptions {
sourceCompatibility javaSourceCompatibility
targetCompatibility javaTargetCompatibility
}
defaultConfig {
minSdkVersion 14
targetSdkVersion versionTarget
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets {
main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = []
}
}
lintOptions {
abortOnError false
}
// externalNativeBuild {
// ndkBuild {
// path 'src/main/jni/Android.mk'
// }
// }
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
String getNdkBuildPath() {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkBuildingDir = properties.getProperty("ndk.dir")
def ndkBuildPath = ndkBuildingDir
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
ndkBuildPath = ndkBuildingDir + '/ndk-build.cmd'
} else {
ndkBuildPath = ndkBuildingDir + '/ndk-build'
}
return ndkBuildPath
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
println('executing ndkBuild')
def ndkBuildPath = getNdkBuildPath();
commandLine ndkBuildPath, '-j8', '-C', file('src/main').absolutePath
}
task ndkClean(type: Exec, description: 'clean JNI libraries') {
println('executing ndkBuild clean')
def ndkBuildPath = getNdkBuildPath();
commandLine ndkBuildPath, 'clean', '-C', file('src/main').absolutePath
}
clean.dependsOn 'ndkClean'
dependencies {
implementation fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
implementation "com.android.support:support-v4:${supportLibVersion}"
implementation "com.android.support:support-annotations:${supportLibVersion}"
implementation("com.serenegiant:common:${commonLibVersion}") {
exclude module: 'support-v4'
}
}
通过Gradle搭建NDK环境,坏处不能debug,不能f3到源码处(如上上面gradle配置);
通过NDKBuild搭建NDK环境(重点介绍,如下gradle配置)-》
apply plugin: 'com.android.library'
/*
* UVCCamera
* library and sample to access to UVC web camera on non-rooted Android device
*
* Copyright (c) 2014-2017 saki [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* All files in the folder are under this Apache License, Version 2.0.
* Files in the libjpeg-turbo, libusb, libuvc, rapidjson folder
* may have a different license, see the respective files.
*/
//import org.apache.tools.ant.taskdefs.condition.Os
android {
compileSdkVersion versionCompiler
buildToolsVersion versionBuildTool
compileOptions {
sourceCompatibility javaSourceCompatibility
targetCompatibility javaTargetCompatibility
}
defaultConfig {
minSdkVersion 14
targetSdkVersion versionTarget
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
// sourceSets {
// main {
// jniLibs.srcDir 'src/main/libs'
// jni.srcDirs = []
// }
// }
//
// lintOptions {
// abortOnError false
// }
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
}
//tasks.withType(JavaCompile) {
// compileTask -> compileTask.dependsOn ndkBuild
//}
//
//String getNdkBuildPath() {
// Properties properties = new Properties()
// properties.load(project.rootProject.file('local.properties').newDataInputStream())
// def ndkBuildingDir = properties.getProperty("ndk.dir")
// def ndkBuildPath = ndkBuildingDir
// if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// ndkBuildPath = ndkBuildingDir + '/ndk-build.cmd'
// } else {
// ndkBuildPath = ndkBuildingDir + '/ndk-build'
// }
// return ndkBuildPath
//}
//
//task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
// println('executing ndkBuild')
// def ndkBuildPath = getNdkBuildPath();
// commandLine ndkBuildPath, '-j8', '-C', file('src/main').absolutePath
//}
//
//task ndkClean(type: Exec, description: 'clean JNI libraries') {
// println('executing ndkBuild clean')
// def ndkBuildPath = getNdkBuildPath();
// commandLine ndkBuildPath, 'clean', '-C', file('src/main').absolutePath
//}
//
//clean.dependsOn 'ndkClean'
dependencies {
implementation fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
implementation "com.android.support:support-v4:${supportLibVersion}"
implementation "com.android.support:support-annotations:${supportLibVersion}"
implementation("com.serenegiant:common:${commonLibVersion}") {
exclude module: 'support-v4'
}
}
通过NDKBuild搭建NDK环境》(好处可以实现f3源码和debug调试)。
注意针对多文件夹下的多android.mk文件,需要一个总的android.mk,下面为子android.mk,
通过Gradle搭建NDK环境时,总的android.mk为:
#include $(call all-subdir-makefiles) PROJ_PATH := $(call my-dir) include $(CLEAR_VARS) include $(PROJ_PATH)/UVCCamera/Android.mk include $(PROJ_PATH)/libjpeg-turbo-1.5.0/Android.mk include $(PROJ_PATH)/libusb/android/jni/Android.mk include $(PROJ_PATH)/libuvc/android/jni/Android.mk
通过NDKBuild搭建NDK环境时,总的android.mk为:
PROJ_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(PROJ_PATH)/UVCCamera/Android.mk
include $(PROJ_PATH)/libjpeg-turbo-1.5.0/Android.mk include $(PROJ_PATH)/libusb/android/jni/Android.mk
include $(PROJ_PATH)/libuvc/android/jni/Android.mk
(即少了#include $(call all-subdir-makefiles))。