I want to do something conceptually simple but for which I cannot find any documentation. I have a certain libraries for which I compiled SO files for all the platforms I am interested.
Now I would like just to generate an AAR file containing those so files.
I know the AAR file has to have a structure like: /jni//mylib.so
What I do now know is what kind of Manifest/other metadata I should produce.
Ideally it would be great to generate that using Gradle but I just need this thing done so it makes sense also writing a simple script generating a zip and then rename it.
AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods. AAR files can contain C/C++ libraries for use by the app module's C/C++ code.
Publishing an AAR containing native libraries is pretty straight-forward. Advantages I've found include:
Trade off is that there are a few extra steps to update the c-code, but if that changes less frequently than the app, then it's a good trade off.
It's worth noting that I get a warning: Current NDK support is deprecated. Alternative will be provided in the future.
on every build. AFAIK, the alternative isn't yet available - my definition of deprecated would be that it's not deprecated until that happens.
Here are the key points of how I have it working:
app/build.gradle (a lot of this also applies to using NDK in an app project):
apply plugin: 'maven'
at top under apply plugin: 'com.android.library'
Add c/cpp header containing directories under android block:
sourceSets.main {
jni.srcDirs 'src/main/jni/Thirdparty/lib1/headers', ...
}
under android/defaultConfig (whatever options your native code may need) :
ndk {
moduleName "module_name"
cFlags "-std=gnu++11 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -D__STDC_LIMIT_MACROS -fexceptions"
ldLibs "log"
stl "gnustl_static"
}
Outside of the android block - to tell gradle how to publish the library to a maven repo. This just pushes to the local Android sdk repo directory, but it's also possible to pushed to shared repositories (I'm currently pushing to an internal nexus repo also - gradle code for that not included here)
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost" + System.getenv("ANDROID_HOME") + "/extras/android/m2repository/")
pom.version = '1.0-SNAPSHOT'
}
pom.groupId = 'com.example.groupid'
pom.artifactId = 'library_name'
}
./gradlew uploadArchives
then publishes a valid aar to my local repo and I can depend on it from an application project using with compile 'com.example.groupid:library_name:1.0-SNAPSHOT'
in app/build.gradle under android/dependencies blockIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With