How to use existing .so file in android application

I was looking for a library that should convert .doc / .docx to .pdf on the Android platform.

I got pdFTron android sdk since they provided the libPDFNetC.so file.

For conversion, there is the Convert class, inside which the toPDF () method exists, in this method they are internally called the native FileToPdf () method. I tried this code but could not call this own method and got errors

I want to know that if an existing .so file is present with you, and if you want to call your own method, which is present in the .so file, then you need to use JNI ?. I do not know much about JNI. any help.

+6
source share
3 answers

You need to make sure that the .so file that appears in the native interface is available on your system and can be found by Java.

Your Java code should have a call like System.loadLibrary("<libraryname>") or System.load("/path/to/libs/lib<libraryname>.so") . This will instruct the JVM to search for the library with the given name and load it.

  • System.load("/path/to/libs/lib<libraryname>.so") will simply search for the file specified as an argument and load it.
  • System.loadLibrary("<libraryname.") Will look in the configured library path for the library named lib<libraryname>.so . The library path is taken from the java.library.path system variable.

Also, make sure that the version of the library you are downloading is compatible with Java JNI mapping!

+2
source

You need to link your final shared library created by Android NDK with ndk-build in the shared PDF library that you said that you already compiled for the ARM architecture . ( Make sure this is the case, otherwise the library will not work on Android! )

For this, if, for example, you have the following directory structure:

 jni └── libs └────── my_shared_lib.so └── Android.mk └── Application.mk 

In the Android.mk file, you must have the following content:

 LOCAL_PATH := $(call my-dir) # define our prebuilt shared library as a module to the build system include $(CLEAR_VARS) LOCAL_MODULE := mysharedlib LOCAL_SRC_FILES := libs/my_shared_lib.so include $(PREBUILT_SHARED_LIBRARY) # The final shared library that will be bundled inside the .apk include $(CLEAR_VARS) LOCAL_MODULE := mynativelib LOCAL_LDLIBS := -landroid -llog LOCAL_CPPFLAGS := -O0 -g3 -std=c++11 -Wall -Wextra LOCAL_SHARED_LIBRARIES := mysharedlib LOCAL_C_INCLUDES := myheader1.h myheader2.h LOCAL_SRC_FILES := src_file1.cpp src_file2.cpp include $(BUILD_SHARED_LIBRARY) 

and the contents of the Application.mk file (to use the standard C ++ library and create the final shared library for two different versions of the ARM architecture):

 APP_OPTIM := debug APP_PLATFORM := android-14 APP_STL := gnustl_static APP_ABI := armeabi armeabi-v7a 

Then, after compiling the code from Eclipse or from the command line using ndk-build script, it will compile your final shared library and link it to your pre-created shared library (i.e. in the shared PDF library that you said you are trying to use) .

For shared libraries, the apk generated and hosted on the device / emulator contains the final shared library, as well as any ready-made shared libraries that you link to , as opposed to links to static libraries that are not linked inside the apk.

In your use case, you should have two shared libraries in the lib directory of your Android application after unpacking the apk on the device. You can verify this by running the following command from the terminal:

 adb shell ls -l /data/data/com.company.myapp/lib 

Replace com.company.myapp with your application package name.

Also, be sure to include inside the static context of the Java class:

 class MyClass { static { try { System.loadLibrary("mynativelib"); } catch (UnsatisfiedLinkError ule) { Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage()); } } // other code here... } 

Note the use of the same name inside the System.loadLibrary method call as the final name of the shared library.

+2
source

To work with ndk, there is a .mk file (make file) that runs under JNI to get a .so file.

File

.mk is written to native code using the c / C ++ snippet.

Now for compiling using JNI, there are two general ways:

1. When working with older versions of ADT + SDK: To configure windows, you will need to create a linux environment to run JNI commands in the shell. This environment can be achieved with various software available on the network, such as installing cygwin.

2. If updated versions of ADT + SDK are updated: Then there is an NDK pluggin available in ADT itself. So it’s easier to work with ndk and your own library. Learn more about NDK here.

0
source

All Articles