Search code examples
android-studioshared-librariesbuild.gradlendk-build

include .so in Android Studio with Build:Gradle:2.1.0


I know there are many similar questions but I simply don't understand it.

I have managed to integrate ndk-build within build.gradle but it seems I cant include the .so in my .apk. Here is my 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'
    }

and this leads to the following directory structure:

|--app:
|--|--src:
|--|--|--main
|--|--|--java
|--|--|--libs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files 

At runtime of my App I get an java.lang.UnsatisfiedLinkError: when calling a native function...

I have found many things like:

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

But I don't understand that and it feels unnecessary to me to create .jar archives for the native code. Or have I missed something completely?

Thanks in advance!


Solution

  • I am not sure why, the task nativeLibsToJar does the trick, but it does. Maybe there will be some answer on my new asked question magic of nativeLibsToJar

    Here's my build.gradle file, which works fine. Maybe someone will find it usefull :)

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