what is the magic of nativeLibsToJar

2019-08-13 15:08发布

问题:

The following code snippet seems to be the answer how to include native libraries with Android Studio:

task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    extension 'jar'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

tasks.withType(Compile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

It seems to simply pack the *.so into *.jar. But I really don't understand it:

  • Why is it necessary to wrap it in a *.jar?
  • When changing something in my native libraries, I can see the changes taking effect in my Application, also the Gradle building process always outputs "...:app:nativeLibsToJar UP-TO-DATE...". So I assume this task is not re-run. But when this task wraps the *.so in *.jar than how is it possible to re-wrap them without rerunning this task??

I am thankful for every explanation :)

回答1:

That's really funny - I found this as solution so many times:

task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    extension 'jar'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

But it can be removed because it does nothing useful (maybe it did in older build-versions).

The real trick is done through sourceSets.main.jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of default dir 'jniLibs