# JNI libs missing in APK, need some help here

**URL:** <https://discuss.gradle.org/t/jni-libs-missing-in-apk-need-some-help-here/9292>\
**Category:** Help/Discuss\
**Created:** [April 23, 2015, 4:54pm UTC](https://discuss.gradle.org/t/jni-libs-missing-in-apk-need-some-help-here/9292 "2015-04-23T16:54:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dealy663](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/dealy663/32/2151_2.png) [@dealy663](https://discuss.gradle.org/u/dealy663)\
**Post date:** [April 23, 2015, 4:54pm UTC](https://discuss.gradle.org/t/jni-libs-missing-in-apk-need-some-help-here/9292/1 "2015-04-23T16:54:53Z")

</div>

I’ve been searching all over the place this morning trying to figure out what’s wrong but haven’t found an answer that works for me.

I have a sub-project which generates an AAR file and a set of JNI libs. When I examine this AAR I can see my .so files.

I’m generating my APK in another project which has a dependency on the AAR project. However when I run my APK program I always get an UnsatisfiedLinkError because it can’t find my .so files at runtime. If I expand the APK file I can see that they are indeed not present. However in my APK’s build/intermediates/exploaded-aar dir I can see the local version of the AAR files and in the classes.jar file I can see my missing .so libs.

I’m pretty new to Gradle so there’s a lot I’m still coming up to speed on. But I just can’t figure out what the problem is here. Hopefully someone has some suggestions.

Here is my AAR build.gradle:

```
apply plugin: 'android-library'
apply plugin: 'android-apt'

sourceCompatibility = 1.6
targetCompatibility = 1.6

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

android {
    compileSdkVersion 18
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"

        ndk {
            moduleName = "usbcamera"
            abiFilters = ['armeabi-v7a']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
         debug {
            debuggable true
        }
    }

    sourceSets.main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']

        jni.srcDirs = [] // disalbe NDK auto build (not sure why this is necessary)
        jniLibs.srcDir 'src/libs'
    }

    task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
        if (androidNdk != null) {
            def ndkDir = androidNdk //android.plugin.ndkFolder
            commandLine "$androidNdk/ndk-build",
                        '-C', file('jni').absolutePath, // Change src/main/jni the relative path to your jni source
                        '-j', Runtime.runtime.availableProcessors(),
                        'all',
                        'NDK_DEBUG=1'
        } else {
            doLast {
                println '##################'
                println 'Skipping NDK build'
                println 'Reason: ANDROID_NDK not set.'
                println '##################'
            }       
        }
    }

    task cleanNative(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = androidNdk 

        commandLine "$ndkDir/ndk-build",
                '-C', file('jni').absolutePath, // Change src/main/jni the relative path to your jni source
                'clean'
    }

    clean.dependsOn 'cleanNative'

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn buildNative
    }

    lintOptions {
        disable 'InvalidPackage', 'LongLogTag'
    }

    dependencies {
        compile project(':HsosSdk')
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // annotation processors for the pre-compile phase
    apt 'com.squareup.dagger:dagger-compiler:1.2.2'

    // annotation processors for the compile phase
    compile 'com.squareup.dagger:dagger:1.2.2'
    compile 'javax.inject:javax.inject:1'
    compile 'com.squareup:javawriter:2.5.1'

}
artifacts.add("default", file('build/outputs/aar/UdaDrivers-release.aar'))

clean.doFirst {
    delete 'bin/udadrivers.jar'
}

```

# ============================================== Here is my APK build.gradle

```
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }

}

apply plugin: 'android'
apply plugin: 'android-apt'

sourceCompatibility = 1.6
targetCompatibility = 1.6

android {
    compileSdkVersion 18
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"
   
         ndk {
            abiFilters = ['armeabi-v7a']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
         debug {
            debuggable true
        }
    }

    sourceSets {
        main.manifest.srcFile 'AndroidManifest.xml'
        main.java.srcDirs = ['src']
    }

    lintOptions {
        disable 'InvalidPackage', 'LongLogTag'
    }

    dependencies {
        compile project(':UdaDrivers')
    }

    dexOptions {
        preDexLibraries = true
    }

    project.tasks.withType(com.android.build.gradle.tasks.Dex) {
// additionalParameters=['--core-library']
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

}

repositories {
    mavenCentral()
}

sourceSets {
    main.output.classesDir = 'bin/classes'
}

dependencies {
    // annotation processors for the pre-compile phase
    apt 'com.squareup.dagger:dagger-compiler:1.2.2'

    // annotation processors for the compile phase
    compile 'com.squareup.dagger:dagger:1.2.2'
    compile 'javax.inject:javax.inject:1'
    compile 'com.squareup:javawriter:2.5.1'

    // remote libraries
    compile 'org.jgroups:jgroups:3.5.1.Final'

    compile project(':Algorithms')
}
```

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [April 24, 2015, 4:17am UTC](https://discuss.gradle.org/t/jni-libs-missing-in-apk-need-some-help-here/9292/2 "2015-04-24T04:17:26Z")

</div>

I believe the JNI libraries should live in the AAR at ‘/jni/\<abi\>/\*.so’ rather than in the classes.jar.

> **[AAR Format - Android Studio Project Site](http://tools.android.com/tech-docs/new-build-system/aar-format)**
>
> Android tools project information site

---

<div class="post-metadata">

**Author:** ![dealy663](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/dealy663/32/2151_2.png) [@dealy663](https://discuss.gradle.org/u/dealy663)\
**Post date:** [April 24, 2015, 2:30pm UTC](https://discuss.gradle.org/t/jni-libs-missing-in-apk-need-some-help-here/9292/3 "2015-04-24T14:30:07Z")

</div>

It started working once I removed the ndk section from the defaultConfig of the library build.gradle file
