[英]Android Studio Gradle with native libs error
Sorry for my english... 对不起我的英语不好...
I have last android studio (14 june 2013). 我有上一个Android Studio(2013年6月14日)。 Create new Android project. 创建新的Android项目。 Add .so files to /libs/armeabi 将.so文件添加到/ libs / armeabi
Edit build.gradle to 将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')
}
I received an error: FAILURE: Build failed with an exception. 我收到一个错误:失败:生成失败,出现异常。
Directory 'build\\native-libs' specified for property 'jniDir' does not exist. 为属性“ jniDir”指定的目录“ build \\ native-libs”不存在。
This will happen if your copyNativeLibs task fails to find any files, and therefore doesn't create the "build\\native-libs" directory. 如果您的copyNativeLibs任务找不到任何文件,因此不会创建“ build \\ native-libs”目录,则会发生这种情况。 Are you sure that there are .so files in your "libs/armeabi" directory? 您确定“ libs / armeabi”目录中有.so文件吗?
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. 您仍然需要自己运行ndk-build来生成.so库。
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. 注意,这要求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')
}
Try this in your build.gradle: 在您的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'
Then you should be good 那你应该很好
You can try this: 您可以尝试以下方法:
// 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'))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.