如何解决在ndkBuild任务中声明输出将使其在需要时不执行
我不太熟悉Android本机,我有一个示例项目,其中包含一个包含本机代码的库。我有一个问题,就是该库的ndk构建没有递增执行,或者说它每次都在运行,即使它以前已经完成过(文件已经生成)也是如此。为了对其进行修复,我遵循了本文中的说明:https://blog.gradle.org/introducing-incremental-build-support
因此它实际上可以工作,但是现在的问题是,即使我执行Build-> Clean project,此后,当我运行我的任务时,它也不起作用,它也不会生成本机版本的文件。如果我正常执行此操作(只是允许任务始终运行),文件将显示在src / main / obj / local中。
这是我库的build.gradle中的代码:
apply plugin: 'com.android.library'
String ndkBuildPath= System.env.ANDROID_NDK + "/ndk-build"
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
ndk {
moduleName "hello-jni"
cFlags "-std=c++17"
//cFlags "-std=c++11 -fexceptions"
ldLibs "log"
stl "gnustl_shared"
abiFilters 'armeabi-v7a','x86'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
sourceSets.main.jniLibs.srcDir 'src/main/libs'
// call regular ndk-build(.cmd) script from app directory
/**
* Initial version: uncomment this method and comment the one below
* with the same name,run the app and check Build Output.
* ndkBuild runs every single time.
*/
/*task ndkBuild(type: Exec) {
print "==== running ndkBuild"
workingDir file('src/main')
commandLine ndkBuildPath
}*/
/**
* Improved version: uncomment this method and comment the one above
* with the same name,run the app and check Build Output.
* ndkBuild is up-to-date even if you never ran it...
*/
task ndkBuild(type: Exec) {
// define inputs
def workDir = "src/main"
def codeLine = ndkBuildPath
// define outputs
//def generatedFileDir = file("src/main/objs/local")
//outputs.dir generatedFileDir
outputs.dir file("src/main/objs/local/arm64-v8a/libhello-jni.so")
outputs.dir file("src/main/objs/local/armeabi-v7a/libhello-jni.so")
outputs.dir file("src/main/objs/local/x86/libhello-jni.so")
outputs.dir file("src/main/objs/local/x86_64/libhello-jni.so")
print "==== outputs: " + outputs.files.size()
workingDir workDir
commandLine codeLine
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task cleanNative(type: Exec) {
workingDir file('src/main')
commandLine ndkBuildPath,'clean'
}
clean.dependsOn cleanNative
}
dependencies {
implementation fileTree(dir: "libs",include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
如您所见,我正在生成一个输出,以便我的任务不会每次都执行。 如何使我的任务在适当的时候运行?例如每次在模块中更改代码,或完成Build-> Clean项目?预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。