Import existing C ++ library (.a or .so file) ndk android

I just went through my own development in Android. I managed to get my AndroidStudio 2.2.2 ready for my own debelopment

I also create a hello-jni project

What I'm trying to achieve

I am trying to use an existing library created in C ++ (I will be provided with a static library .a or .so ).

Few confusions regarding one's own development

1) Should I use .cpp and .h files of an existing C ++ library instead of a .a or .so file?

2) I need to do CMakeLists.text . As I understand it, my .a files are not generated using ndk-build >, so I need to do this.

If I use .cpp and .h files , should I make Android.mk and Application.mk

Is CMakeLists.text compiling my newly developed android project as a library or an existing .a file?

3) Where can I put the .a file in my project. Is it in jni folder ?

4) If my java class files must define methods with the native keyword the same as implemented in the C ++ file (Example: in the C ++ file, the getData () method name, should the java class contain public getData ())

+5
source share
3 answers

So you have a bunch of questions. Some of these questions are related to the type of personal preference, but I will provide them as my personal choice.

1

This is your choice. I would personally use a compiled .so file. That way, I never have to worry about NDK, CMake, and .mk files. If you have a file, all you have to do is add the file to the libs (not lib ) folder and make minor changes to your build.gradle file. What is it.

Change to build.gradle:

 sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } 

2 and 3

This does not apply to this option.

4

You would need to do something like this, regardless of whether you use files or compiled libraries:

 @SuppressWarnings("JniMissingFunction") public class MyNativeMethods { static { System.loadLibrary("my_native_lib"); } public native int native_method_1(int fd); public native int native_method_2(int fd); public native void native_method_3(int fd, int arr[]); public native int[] native_method_4(int fd); } 

And then you can call these methods from your Activity / Fragment .

Hope this is clear enough.

EDIT (based on comment below) :

1) .so or .a files are your native libraries.

2) .cpp , .c files, etc. - these are just your source files. If you intend to use these files in a project, you will have to use a build system (e.g. CMake) to use them. CMake will take the source code files and create the .so library, which will again become the native library. That's why I suggested using .so files, because why do I need to work with CMake in your project when you don't need it?

If you want to try CMake or find out in the future, check this answer: C / C ++ with Android Studio version 2.2

3) System.loadLibrary("my_native_lib"); : Here you specify the Java runtime to add this library. This way you create a connection between Java and the C ++ code that is inside the library. The methods below this line should have the same name as in C ++ / C code. Thus, the Java runtime will find and open the library and search for this method in the loadable library. More here

+7
source

From here

Opening shared libraries directly from the APK

In API 23 and above, you can open a .so file directly from your APK. Just use System.loadLibrary ("foo") exactly the same as usual, but set android: extractNativeLibs = "false" in your AndroidManifest.xml. In older versions, .so files were extracted from the APK file during installation. This meant that they took up space in your APK and again in your installation directory (and this was counted against you and was reported to the user as the space occupied by your application). Any .so file that you want to download directly from your APK must be page aligned (at the border of 4096 bytes) in a zip file and saved uncompressed. Current versions of the zipalign tool will take care of alignment.

Please note that in API 23 and above, dlopen (3) will open the library from any zip file, not just your APK. Just give dlopen (3) the path of the form "my_zip_file.zip! /Libs/libstuff.so". As with the APK, the library must be page aligned and kept uncompressed for this to work.

+1
source
0
source

Source: https://habr.com/ru/post/1259258/


All Articles