Including Native Libraries in Your APK

| Comments

I have recently been working on a top secret project that includes the wonderful native libraries of the libgdx project in order to have a decent Box2d physics simulation. I am using Android Studio 0.4.0 as of this week, and the recently released 0.7.3 of the Gradle Android build tools includes a new way to bundle shared libraries with an APK. Here’s how to do it.

Edit your build.gradle file and add something like the following in the android section:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        renderscriptTargetApi 18
        renderscriptSupportMode true
    }

    sourceSets {
        main {
            jniLibs.srcDir file('jni/')
        }
    }
}

The magic is on line 14, where you can see the new jniLibs.srcDir property being set. The trailing slash isn’t required. I just did it to be explicit that it is a directory.

Then make a jni directory in the project directory, and inside there you can place the shared libraries you want to bundle into your APK under specific abi directories. For example:

jni
jni/armeabi
jni/armeabi/libgdx.so
jni/armeabi-v7a
jni/armeabi-v7a/libgdx.so
jni/x86
jni/x86/libgdx.so

So far, this only works in application modules, not library modules.

Copyright © 2015 - Neal Sanche. Powered by Octopress