如何在另一个Android项目中使用生成的.so库? [英] How to use the generated .so library in another Android Project?

查看:282
本文介绍了如何在另一个Android项目中使用生成的.so库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了 http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ ,效果很好!但是,我想在新的Android应用程序中使用生成的.so库,而我根本不知道该怎么做...我已经努力了好几天,并且是否可以共享任何分步教程那会有所帮助!

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!

这是我用来生成.so文件的Android项目 MyApp :

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

MainActivity:

MainActivity :

Java类:MyNDK

Java Class : MyNDK

头文件:com_demo_ble_myapp_MyNDK.h

header file: com_demo_ble_myapp_MyNDK.h

Cpp文件:MyLibrary

Cpp file: MyLibrary

这是我的新Android项目 useSOLib 的结构,我只需将所有这样的文件从 MyApp \ app \ src \ main \ libs 复制到 useSoLib \ app \ src \ main \ jniLibs

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

这是useSoLib中的MainActivity:

And this is MainActivity in useSoLib:

我可以成功完成Build-> Make Project,但是当我在设备上运行它时,应用程序显示很遗憾,useSoLib已停止."并被击碎.我知道我在这里错过了一些步骤,但是我是Android Studio的新手,所以我不知道应该从哪里开始...在此先感谢您的任何建议! :)

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! :)

推荐答案

您应导入 MyNDK 并使用其实例. JNI库与Java类有关.更改代码:

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());
   }
}

您可以将 MyNDK 类导出为jar包,也可以创建 aar 捆绑包.然后将Java类导入到具有JNI库的新项目中.

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.

帖子如何将* .so库文件构建到AAR捆绑包中Android Studio 可能会对您有所帮助.

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

这篇关于如何在另一个Android项目中使用生成的.so库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆