Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a shared library without JNI in Android?

I develop an Android application, which needs to be a shared library. I have already made the same lib in Linux (using gcc), so I want to compile the same shared lib in Android (ARM processor). But NDK only suports JNI. Now I want to build a separate shared library without direct JNI interaction. I have a lot of C files and headers, so i cannot write JNI for everything. I want build my library for ARM processors. How can i do this?

my stuture is
  ----->JNI
     ---->myfile.c(jni c code)
      ----->android.mk(here i call my two shared lib)

       folder1
             --->include
             ----src
             ---->lib(here i will get my shared lib)
       folder 2
            ----->include
            ----->src
            ----->lib(here i will get my 2nd shared lib)

Here I want to build my shared lib separately and then I will call it. Is it possible to build a shred lib without JNI in Android?

like image 640
Mr.Cool Avatar asked Sep 25 '12 08:09

Mr.Cool


2 Answers

Android NDK makes no requirements for JNI compliance of the binaries built with it. But usually Android applications are written in Java, and when they use native components, the natural path is to use JNI.

Android NDK provides a rich and easy to use ndk-build command, which requires you to prepare at least one Android.mk file that is used by their make system. In many cases it is worth the effort to prepare such file based on your makefile(s). But if the project that you port from another platform uses advanced configure or Cmake features, you can skip the Android.mk completely, and use the standalone toolchain which is part of the NDK, as described in NDK official documentation. Note that Android does not support full Linux glibc, but rather a subset called bionic. The STL support is also not obvious, you have to choose one of the 7 options!

You can stop right here, get the shared library or an executable file and use them in traditional Linux manner. You can build your Android application all in C++.

But the easiest approach is to build a Java Android application using Android SDK, and call some native methods via JDK. You will need a wrapper .so for that, a library that exports JNI functions to Java and calls the non-JNI libraries in turn. In Android.mk you can refer to these libraries with LOCAL_SHARED_LIBRARIES. You may find the following question useful for this part: ndk build library outside main project source tree.

You will need to load the native libraries "manually" in your Java code. The system loader only searches for unresolved references in /system/lib folder.

The conventional place is the static constructor of the Java class that uses the native methods. The order of loading is important. If libone.so depends on libtwo.so, and libjni.so depends on libone and on libtwo, you will have something like

{
  System.Load("two");
  System.Load("one");
  System.Load("jni");
}

You should also make sure that the Android SDK builder finds the files in libs/armeabi or libs-armeabi-v7a directory when it creates the APK file.

like image 59
Alex Cohn Avatar answered Sep 30 '22 04:09

Alex Cohn


It is possible to build native library without writing any JNI specific C-code. You need to create Android.mk and Application.mk files for your library. You have a valid makefile ready, so you can use it to make an Android.mk.

Application.mk can be as follows:

APP_OPTIM := release
APP_PLATFORM := android-7
APP_STL := gnustl_static
APP_CPPFLAGS += -frtti 
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -DANDROID
APP_ABI := armeabi-v7a
APP_MODULES := <put-the-name-of-your-library-here>

Use ndk-build to compile the library.

like image 34
Sergey K. Avatar answered Sep 30 '22 04:09

Sergey K.