Open Source & Free  

Integrating Android 3rd Party Libraries & JNI

Integrating Android 3rd Party Libraries & JNI

Header Image

While its pretty easy to use native interfaces
to write Android native code some things aren’t necessarily as obvious. E.g. if you want to integrate a 3rd party library, specifically one that includes native C JNI code this process is somewhat undocumented.
If you need to integrate such a library into your native calls you have the following 3 options:

  1. The first option (and the easiest one) is to just place a Jar file in the native/android directory.
    This will link your binary with the jar file. Just place the jar under the native/android and the build server will pick it up and will add it to the classpath.
    Notice that Android release apps are obfuscated by default which might cause issues with such libraries if they reference API’s that are unavailable on Android.
    You can workaround this by adding a build hint to the proguard obfuscation code that blocs the obfuscation of the problematic classes using the build hint:

    android.proguardKeep=-keep class com.mypackage.ProblemClass { *; }
  2. The second option is to add an Android Library Project. Not all 3rd parties can be packaged as a simple jar, some 3rd parties needs to declare
    Activities add permissions, resources, assets, and/or even add native code (.so files). To link a Library project to your CN1 project open the Library
    project in Eclipse or Android Studio and make sure the project builds, after the project was built successfully remove the bin directory from the
    project and zip the whole project.
    Rename the extension of the zip file to .andlib and place the andlib file under native/android directory. The build server will pick it up and will link it to the project.
  3. We recently added a 3rd option :aar files. The aar file is a binary format from Google that represents an Android Library project. One of the problem with
    the Android Library projects was the fact that it required the project sources which made it difficult for 3rd party vendors to publish libraries, so android
    introduced the aar file which is a binary format that represents a Library project. To learn  more about arr you can read
    this.

You can link an aar file by placing it under the native/android and the build server will link it to the project.

To use any of the options above 3rd parties API’s you will need to create a
NativeInterface and access
the libs API’s under the android implementation only section.

6 Comments

Leave a Reply