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:
- 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 { *; }
- 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. - 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
Hi Chen,
Under option 1, can you elaborate what you mean by “API’s that are unavailable on Android” when talking about obfuscation?
I would be great to have a code example to, for example, using Android PackageManager to retrieve a list of installed applications.
There are quite a few samples in the cn1libs section where pretty much all the libraries are open source.
Using this query on github I was able to find several results: [https://github.com/search?q…](https://github.com/search?q=codename1+PackageManager&type=Code&utf8=%E2%9C%93)
Hi,
sorry for the late reply. Not sure why Chen didn’t answer back then…
A JAR might import javax.swing and use it for some cases but might handle that case correctly by catching the class not found exception. However, this might collide with obfuscation that doesn’t like those sort of tricks…
the query does not work anymore, could you please give me a example? Thank you 🙂
I see 11 results in the link above