Android Studio C ++ NDK库示例 [英] Android Studio C++ NDK library example
问题描述
我想下载一些使用C或C ++的Android Studio NDK项目的工作示例,该示例将编译为.so库(或我可以从其中提取.so的APK). 我已经尝试过将ndkbuild与Android.mk一起使用,也尝试将CMake与CMakeLists.txt一起使用,以及Windows上的官方和非官方教程...
I would like to download some working example of Android Studio NDK project with C or C++ which will be compiled to .so library (or APK from which I can extract .so). I have tried ndkbuild with Android.mk build and also CMake with CMakeLists.txt, official and unofficial tutorials on Windows...
如果我在同一个应用程序(java)上的android上尝试使用它们,则可以正常运行,但是我想在Android上的Unity3D中使用NDK,并且我一直在获取DllNotFoundException.
If I try them on android from same app (java), it is working but I want to use NDK in Unity3D on android and I keep getting DllNotFoundException.
我上传了 Android 和
I uploaded my Android and Unity projects to github.
推荐答案
首先,此处是您要查找的示例Android Project插件.
First of all, here is a sample Android Project plugin you are looking for.
知道自己如何做到这一点非常重要.
It's very important to know how to do this yourself.
这以前很难做到,但是最新的 Android Studio 使现在更容易使用C/C ++或轻松生成 .so 库.将 Android Studio 更新到最新版本,然后按照以下步骤创建C ++插件.
This used to be hard to do before but the latest Android Studio made it easier to now use C/C++ or generate .so library easily. Update Android Studio to the latest version then follow steps below to create a C++ plugin.
1 .在Android Studio中创建一个新项目
1.Create a new Project in Android Studio
2 .选中包含C ++支持以启用C ++.
2.Check the Include C++ suport to enable C++.
3 .在下拉菜单中,检查 C ++ Standard 下拉菜单中的 C ++ 11 .您将需要C ++ 11才能真正使用最有用的C ++功能.另外,如果需要,请启用exception或frtti.
3.On the Dropdown Menu, check C++ 11 on the C++ Standard drop-down menu. You will need C++ 11 to actually use most useful C++ features. Also, enable exception or frtti if you need them.
就是这样.
构建插件时:
调试库应该位于:
\app\build\intermediates\cmake\debug\obj\
发布库应位于:
\app\build\intermediates\cmake\debug\obj\
如果您仅看到调试而不是插件的发行版,请在此处查看如何使其显示
If you only see the debug but not the release version of the plugin, check here for how to make it appear.
在Android上获取 DllNotFoundException 的可能原因:
Possible reasons you are getting DllNotFoundException on Android:
1 .您没有将C/C ++函数包装在extern "C"
周围.您必须对 .cpp 文件中的每个功能执行此操作,或者对 .h 文件中的功能执行此操作.
1.You did not wrap the C/C++ function around extern "C"
. You must do this for each function in the .cpp file or you do it in the function in the .h file.
2 .您将插件放在错误的Unity文件夹中.
2.You put the plugin in the wrong Unity folder.
在<ProjectDirectory>\app\build\intermediates\cmake\release\obj
处生成的armeabi-v7a和x86插件应放置在Unity项目的Assets\Plugins\Android\libs\armeabi-v7a\
和Assets\Plugins\Android\libs\x86\
处.
The armeabi-v7a and x86 plugins generated at <ProjectDirectory>\app\build\intermediates\cmake\release\obj
should be placed at Assets\Plugins\Android\libs\armeabi-v7a\
and Assets\Plugins\Android\libs\x86\
in the Unity project.
请确保正确拼写这些内容.有关更多信息,请参见此.
Make sure to spell these correctly. See this for more information about this.
3 .您从C#端加载错误.
3.You are loading it incorrectly from the C# side.
假设插件的名称是libScreenshot.a,不包括lib前缀,也不在加载.a
时添加.a
./p>
Let's say that the name of the plugin is libScreenshot.a, do not include the lib prefix, also do not add the .a
when loading it.
[DllImport("Screenshot", CallingConvention = CallingConvention.Cdecl)]
public static extern void takeScreenshot();
这篇关于Android Studio C ++ NDK库示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!