How to use the generated .so library in another Android Project?

拈花ヽ惹草 提交于 2019-12-12 20:15:13

问题


I’ve followed http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ and it works well! However, I want to use the generated .so library in a new Android application, and I simply don’t know how to do this... I’ve been struggling for days and if any step-by-step tutorial can be shared that would be helpful!

This is the Android Project MyApp which I used to generate the .so files:

MainActivity :

Java Class : MyNDK

header file: com_demo_ble_myapp_MyNDK.h

Cpp file: MyLibrary

And this is the structure of my new Android project useSOLib, I simply copy all the so files from MyApp\app\src\main\libs to useSoLib\app\src\main\jniLibs

And this is MainActivity in useSoLib:

I can Build-> Make Project successfully, but when I run it on the device, the app shows "Unfortunately, useSoLib has stopped." and crushed. I know I miss some steps here, but I'm new to Android Studio so I have no clue where I should start with... thank you in advance for any suggestions! :)


回答1:


You should import MyNDK and use its instance. JNI library is related to Java class. Change the code:

public class MainActivity extends AppCompatActivity{
   @Override
   protected void onCreate(Bundle savedInstanceState){

       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Log.i("MyTag", new MyNDK().getMyString());
   }
}

You can either export the MyNDK class as a jar package or create an aar bundle. Then import the Java class into your new project with JNI library.

The post How to Build *.so Library Files into AAR Bundle in Android Studio may help you.



来源:https://stackoverflow.com/questions/37383368/how-to-use-the-generated-so-library-in-another-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!