在 Android Studios C 文件中调用共享库 (.so) 方法 [英] Call shared library (.so) methods within Android Studios C files

查看:19
本文介绍了在 Android Studios C 文件中调用共享库 (.so) 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了好几天.目前我只是用一个简单的 C++ 项目(1 个 .h 和 1 个 .cpp 文件)和一个简约的应用程序(包括 ndk helloJNI 示例代码)对其进行测试(很容易完美运行):

I'm struggling with this for several days now. At the moment i'm just testing it with a simple C++ project (1 .h & 1 .cpp file) and a minimalistic App including the ndk helloJNI sample code (which worked perfect easily):

目标将现有的 C/C++ 文件(项目)导入 Android Studio

Target Import existing C/C++ files (project) to Android Studio

接近在尝试了一些(数十种)不同的可能性之后,我认为/认为以下步骤将是我的目的的最佳解决方案:

Approach After trying out some of the (dozens) of different possibilities, i think/thought the following steps would be the best solution for my purpose:

  1. 从 Visual Studios 2015为 Android 创建共享库"(或其他)创建共享库 (Calculator.so) [成功]
  2. 在 src/main/中创建 jniLibs 文件夹及其子文件夹(在我的例子中是 x86)
  3. 在 src/main/jniLibs 中添加 Android.mk 文件,该文件必须放在那里 (?)
  4. Include 语句:System.loadLibrary("Calculator") 在 MainActivity 中没有lib"和.so"

该库在 Android Studio 的 jniLibs 文件夹中与 Android.mk 一样列出.此外,如果我构建 apk,则库已成功打包(通过解压缩验证)并且我没有收到任何错误.但是:如何调用库中的方法?我尝试了其他线程中提供的不同解决方案,但我认为我错过了 .mk 或上述步骤中的某些内容.

The library is listed in Android Studio in its folder jniLibs as like the Android.mk. Moreover if i build the apk, the library is successfully packed (verified by unzipping) and i dont get any errors. BUT: how can i call the methods in the library? I tried the different solutions offered in other threads, but i think i missed something in my .mk or my steps described above.

试过

  • native-lib.cpp 中的不同 #include <myLib> 语句,如 s
  • 不同的 Android.mk 设置(但我是新来的文件,所以甚至教程都没有帮助我解决我的具体问题::))
  • libCalculator.so 的其他位置,例如子文件夹 x86
  • 还有许多其他人 - 根本不提醒 atm (wasntme)

非常感谢您的帮助!

Android.mk

LOCAL_PATH := $(call my-dir)
APP_ABI := x86        

# library info       
include $(CLEAR_VARS)
LOCAL_MODULE := Calculator
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/Calculator.so
LOCAL_EXPORT_C_INCLUDES := ..../Visual Studio 2015/Projects/SO_Library/SO_Library
include $(BUILD_SHARED_LIBRARY)

推荐答案

有很多事情,你可以在 Android NDK 中做.例如,相机硬件是 Android 操作系统中最重的硬件之一.检测人脸、事物、赋予效果和数千个特征 NDK 是最好的.对您的步骤有一些帮助:

There are lots of things, you can do in Android NDK. For example, Camera hardware is one of the heaviest hardware in Android OS. Detecting faces, things, giving effects and for thousands of features NDK is the best. Some helps for your steps:

  1. 您也可以在 Android Studio 中构建和预构建共享 (.so) 和静态 (.a) 库.不需要 Visual Studio.
  2. 不要在主文件夹中创建 jniLibs 文件夹.当您通过 gradle 构建项目时,它已经创建了这个文件夹并放置了您的目标库.如果您想预构建任何库,请将这些库放在 main/jni/libs 文件夹中,然后使用 Android.mk 预构建.
  3. 不要在 jnilibs 文件夹中添加 Android.mk 文件.在 main/jni 文件夹中创建此文件.还有 Application.mk 文件.
  4. 在您需要的任何活动中,以静态方法调用您的库.像这样:

  1. You can built and prebuilt shared(.so) and static(.a) libraries in Android Studio also. Not need Visual Studio.
  2. Don't create jniLibs folder in main folder. When you build your project via gradle, it already creates this folder and put your target libraries. If you want prebuilt any libraries, put these libraries in main/jni/libs folder and prebuilt then with Android.mk.
  3. Don't add the Android.mk file in jnilibs folder. Create this file in main/jni folder. Also Application.mk file.
  4. Call your libraries, in any activity, where you need, in static method. Like this:

static {  System.loadLibrary("my_library") }

没有lib"和.so"扩展名.

Without "lib" and ".so" extensions.

当你想调用你的本地方法时,只需使用native"关键字.例如:

When you want to call your native methods, just use "native" keyword. For example:

private native int nGetNumberFromNativeSide();

只要在你想要的地方调用这个方法,然后得到结果.但是对于 gradle 方面的 ndk 构建,请查看此 answer.对于在 Android.mk 中构建库,这些示例行可能会对您有所帮助:

Just call this method, where you want, and get result. But for ndk building in gradle side, look at this answer. For building library in Android.mk, these sample lines maybe help you:

include $(CLEAR_VARS)
ifneq (,$(filter $(TARGET_ARCH_ABI), armeabi-v7a x86 arm64-v8a x86_64))

LOCAL_MODULE := my_library
LOCAL_SRC_FILES := $(LOCAL_SRC_LOCATION)/native1.cpp native2.cpp
include $(BUILD_SHARED_LIBRARY)

  • 您可以随意命名,但不要添加 lib 和 .so 扩展名.Ndk 已经在这样做了.
  • 我已经给出了 Android.mk 的例子.
  • 当您构建 Android.mk 文件时,它会找到您的库的相应文件夹.比如 main/libs/x86/libmy_library.so.
  • 我想这个答案会对你有所帮助.如果您有更多问题,请添加评论,我将编辑我的答案并添加答案.

    I guess this answer will help you. If you have more questions, add to comment, i'll edit my answer and add answers.

    这篇关于在 Android Studios C 文件中调用共享库 (.so) 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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