0

我知道有很多类似的问题,但我就是不明白。

我已经设法将 ndk-build 集成到 build.gradle 中,但似乎我不能在我的 .apk 中包含 .so。这是我的 build.grade:



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

这导致以下目录结构:

|--应用:
|--|--src:
|--|--|--主要
|--|--|--java
|--|--|--库
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so 文件

在我的应用程序运行时,我得到一个java.lang.UnsatisfiedLinkError:调用本机函数时...

我发现了很多东西,比如:

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 档案。还是我完全错过了什么?

提前致谢!

4

1 回答 1

0

我不知道为什么,这样task nativeLibsToJar做的伎俩,但确实如此。也许会有一些关于我的 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'
}
于 2016-05-25T21:24:32.260 回答