4

对不起我的英语不好...

我有最后一个 android 工作室(2013 年 6 月 14 日)。创建新的安卓项目。将 .so 文件添加到 /libs/armeabi

编辑 build.gradle 到

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar','libs/jcPKCS11.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 16
    }
}

task copyNativeLibs(type: Copy) {
    from(new File(project(':JaCertTest').getProjectDir(), 'libs/armeabi'))  { include '**/*.so' }
    into new File(buildDir, 'native-libs')
}

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

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniDir new File('build/native-libs')
}

我收到一个错误:FAILURE: Build failed with an exception。

  • 出了什么问题:发现任务':JaCertTest:packageDebug'的配置有问题。

    为属性“jniDir”指定的目录“build\native-libs”不存在。

编写构建脚本如何正确?

4

3 回答 3

2

This will happen if your copyNativeLibs task fails to find any files, and therefore doesn't create the "build\native-libs" directory. Are you sure that there are .so files in your "libs/armeabi" directory?

Also, keep in mind that your script won't actually compile the native code. You still need to do that yourself by running ndk-build to generate the .so libraries.

Here is an example of how to get your script to compile your native code. Note, this requires that ndk-build is in your PATH.

// Task to run ndk-build
task ndkBuild(type: Exec) {
    commandLine 'ndk-build', '-j', Runtime.runtime.availableProcessors()
}

task copyNativeLibs(type: Copy) {
    from(new File(project(':JaCertTest').getProjectDir(), 'libs/armeabi'))  { include '**/*.so' }
    into new File(buildDir, 'native-libs')
}

// Make copyNativeLibs depend on ndkBuild since we must build the libraries
// before we can copy them.
copyNativeLibs.dependsOn 'ndkBuild'
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniDir new File('build/native-libs')
}
于 2013-06-14T19:53:59.303 回答
0

你可以试试这个:

// Include the native-libs folder into the final APK
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { 
  pkgTask ->
  pkgTask.jniFolders = new HashSet<File>()
  pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
}
于 2014-03-07T17:45:55.733 回答
0

在你的 build.gradle 中试试这个:

dependencies {
    // whatever you'd normally have here...
    compile fileTree(dir: 'libs', include: '*.jar')
}

task packageNativeLibs(type: Jar) {
    baseName 'libtest'  // adjust to what you want your lib to be called

    // libs is where normally a jni project puts objects so let's look there....
    from(file('libs/armeabi/')) { 
        include '**/*.so'
    }

    into('libs/armeabi') // pay attention if using a different ABI like x86

    destinationDir(file('libs/'))
}

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

clean.dependsOn 'cleanPackageNativeLibs'

那你应该很好

于 2013-11-06T03:36:22.877 回答