Android Studio Gradle与本机库错误

时间:2013-06-14 19:39:22

标签: android android-ndk gradle android-studio

抱歉我的英文......

我有最后一个android工作室(2013年6月14日)。 创建新的Android项目。 将.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:构建因异常而失败。

  • 出了什么问题: 在任务':JaCertTest:packageDebug'的配置中发现了一个问题。

      

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

  •   
  如何编写构建脚本是正确的?

3 个答案:

答案 0 :(得分:2)

如果您的copyNativeLibs任务无法找到任何文件,则会发生这种情况,因此不会创建“build \ native-libs”目录。您确定“libs / armeabi”目录中有.so文件吗?

另外,请记住,您的脚本实际上不会编译本机代码。您仍然需要通过运行ndk-build来生成.so库来自己完成。

以下是如何让脚本编译本机代码的示例。注意,这要求ndk-build在你的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')
}

答案 1 :(得分: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'

然后你应该很好

答案 2 :(得分: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'))
}
相关问题