【问题标题】:Android Studio Gradle with native libs error带有本机库错误的 Android Studio Gradle
【发布时间】:2013-06-11 13:12:35
【问题描述】:

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

我有最后一个 android studio(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:构建失败并出现异常。

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

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

编写构建脚本如何正确?

【问题讨论】:

标签: android android-ndk gradle android-studio


【解决方案1】:

如果您的 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')
}

【讨论】:

  • .so 文件在 libs/armeabi 中,在没有 gradle 项目构建和运行的纯 Intellij Idea 中,但我想把它放在带有 gradle 的 android-studio 中
  • 你能提供更多关于你的项目布局的信息吗?我相当确定您的错误是由于某处的路径不正确造成的。
【解决方案2】:

在你的 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'

那你应该好好的

【讨论】:

    【解决方案3】:

    你可以试试这个:

    // 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'))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多