在 Android Studio 中包含 .so 和 Build:Gradle:2.1.0
include .so in Android Studio with Build:Gradle:2.1.0
我知道有很多类似的问题,但我就是不明白。
我已经设法在 build.gradle 中集成了 ndk-build,但我似乎无法在我的 .apk 中包含 .so。这是我的 build.gralde:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "masterproject.student_at_university_kiel.knauf.torsten"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// for preventing (cheating) gradle-build to overwrite Android.mk with auto-generated Android.mk (NdkCompile task)
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task ndkBuild(type: Exec) {
def ndkDir = "/home/expert/Android/Sdk/ndk-bundle"
workingDir "src/main/jni"
commandLine "$ndkDir/ndk-build"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}
这导致以下目录结构:
|--app:
|--|--src:
|--|--|--main
|--|--|--java
|--|--|--libs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
在我的应用程序运行时,我在调用本机函数时得到 java.lang.UnsatisfiedLinkError:
...
我发现了很多类似的东西:
</p>
<pre><code>task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}
但我不明白,我觉得没有必要为本机代码创建 .jar 存档。还是我完全错过了什么?
提前致谢!
我不确定为什么,task nativeLibsToJar
可以解决问题,但确实如此。也许我的新问题会有一些答案
这是我的 build.gradle 文件,工作正常。也许有人会发现它很有用:)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "masterproject.student_at_university_kiel.knauf.torsten"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// for preventing (cheating) gradle-build to overwrite Android.mk with auto-generated Android.mk (default NdkCompile task)
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
task ndkBuild(type: Exec, description: 'compile native code') {
def ndkDir = "/home/expert/Android/Sdk/ndk-bundle"
workingDir "src/main/jni"
commandLine "$ndkDir/ndk-build"
}
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/'
}
nativeLibsToJar.dependsOn {
ndkBuild // comment that, when you don't want to rerun ndk-build script
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}
我知道有很多类似的问题,但我就是不明白。
我已经设法在 build.gradle 中集成了 ndk-build,但我似乎无法在我的 .apk 中包含 .so。这是我的 build.gralde:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "masterproject.student_at_university_kiel.knauf.torsten"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// for preventing (cheating) gradle-build to overwrite Android.mk with auto-generated Android.mk (NdkCompile task)
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task ndkBuild(type: Exec) {
def ndkDir = "/home/expert/Android/Sdk/ndk-bundle"
workingDir "src/main/jni"
commandLine "$ndkDir/ndk-build"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}
这导致以下目录结构:
|--app: |--|--src: |--|--|--main |--|--|--java |--|--|--libs |--|--|--|--|--armeabi |--|--|--|--|--|--.so Files
在我的应用程序运行时,我在调用本机函数时得到 java.lang.UnsatisfiedLinkError:
...
我发现了很多类似的东西:
</p>
<pre><code>task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}
但我不明白,我觉得没有必要为本机代码创建 .jar 存档。还是我完全错过了什么?
提前致谢!
我不确定为什么,task nativeLibsToJar
可以解决问题,但确实如此。也许我的新问题会有一些答案
这是我的 build.gradle 文件,工作正常。也许有人会发现它很有用:)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "masterproject.student_at_university_kiel.knauf.torsten"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// for preventing (cheating) gradle-build to overwrite Android.mk with auto-generated Android.mk (default NdkCompile task)
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
task ndkBuild(type: Exec, description: 'compile native code') {
def ndkDir = "/home/expert/Android/Sdk/ndk-bundle"
workingDir "src/main/jni"
commandLine "$ndkDir/ndk-build"
}
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/'
}
nativeLibsToJar.dependsOn {
ndkBuild // comment that, when you don't want to rerun ndk-build script
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}