1

I am having difficulties integrating a native library I have already built using ndk-build into my Android Studio project. The problem is that I am stuck in a transitional period where the Gradle syntax is brand new, but documentation and online help still reference the old syntax.

My library is in this location on my project: app/libs/(armeabi and others)/libsample.so, for a total of 7 variants of the same library for different architectures. I also have the header files for this library in app/src/main/jni/headers, and the C wrapper file which calls functions defined in this library on app/src/main/jni/wrapper.c. The wrapper file has a #include "headers/sample.h".

The main issue is that I don't know how to set up my Gradle build to use the prebuilt shared library. I have a working Android.mk file which can successfully compile my wrapper to use the shared library, but I can't use it in Android Studio, since Gradle generates it's own makefile and uses that instead. Anyone has any idea how to proceed from here?

Ermir
  • 1,391
  • 11
  • 25
  • Are you using the experimental gradle plugin? – Gabriele Mariotti Oct 05 '15 at 06:47
  • Yes, I am using the experimental plugin. Current version is 0.2.0 I believe. I am quite upset that Google is ending support for Eclipse in less than three months, while their planned platform is far from ready. – Ermir Oct 05 '15 at 06:50
  • The last version of gradle experimental plugin is 0.2.1. You can check official examples here: https://github.com/googlesamples/android-ndk – Gabriele Mariotti Oct 05 '15 at 06:54
  • I don't think you can do that, yet. While [this site](http://tools.android.com/tech-docs/new-build-system/gradle-experimental) only mentions to not be able to include **static** libraries, I think they include **shared** object libraries as well (I honestly think, they don't know C/C++ development at all, and made this mistake unknowingly). – Leandros Oct 05 '15 at 07:39
  • I would suggest not using the new gradle system, yet. It's not even close to be ready. And it is in this state for quite a while, unfortunately. I would suggest to keep your old build system (Application.mk / Android.mk in a seperate folder, and you calling ndk-build to generate the lib) and include it via setting the correct jni directory. – Leandros Oct 05 '15 at 07:41
  • I don't know what Google is thinking. Basic functionality regarding their flagship platform is non-existing, their planned upgrade path leaves people in the dust. – Ermir Oct 05 '15 at 07:57
  • @Leandros hi I was also looking for how to link .so library to the project, and I found [this](https://groups.google.com/forum/#!topic/adt-dev/FoyeXl2vl3s), which points to [ndk example](https://github.com/googlesamples/android-ndk/blob/ece572d7219eafcfef928748eadfa10ceb23d2d4/hello-thirdparty/app/build.gradle#L36). So it means Gradle have the ability, and the doc is a bit out-dated... hope it helps – Liyang Chen Oct 08 '15 at 15:56

1 Answers1

0

You should be able to simply drop the pre-built .so files into (project module)/src/main/jniLibs/(target abi), and they'll be included in the final apk that Android Studio generates.

One caveat: I'm not 100% certain, but it looks like Android's installer can choose to throw out some libraries from abi folders that it decides are a poorer fit for the current device than other abi folders in the apk (ex. armeabi and armeabi-v7a may both be compatible, but armeabi-v7a may be a better fit) if said libraries are only present in the poorer-fit folder. This becomes important if you don't have the source code for the native library. You can work around this either by specifying a single abi in your module's build.gradle (see the answer to the linked question) that matches the abi your native library was compiled for or by disabling Android Studio's ndk-build process by setting jni.srcDirs = [] in android.sourceSets.main

Community
  • 1
  • 1
CCJ
  • 1,619
  • 26
  • 41