【问题标题】:include .so in Android Studio with Build:Gradle:2.1.0使用 Build:Gradle:2.1.0 在 Android Studio 中包含 .so
【发布时间】:2016-05-19 18:41:58
【问题描述】:

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

我已设法将 ndk-build 集成到 build.gradle 中,但似乎我无法将 .so 包含在我的 .apk 中。这是我的 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 档案。还是我完全错过了什么?

提前致谢!

【问题讨论】:

    标签: android-studio shared-libraries build.gradle ndk-build


    【解决方案1】:

    我不知道为什么,task nativeLibsToJar 可以解决问题,但确实可以。也许我的新问题magic of 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-09-04
      • 2016-07-02
      • 2023-03-17
      • 2016-08-02
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      相关资源
      最近更新 更多