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

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

问题描述

我已经为此苦了几天.目前,我只是用一个简单的C ++项目(1个.h& 1个.cpp文件)和一个包含ndk helloJNI示例代码的简单应用程序(很容易实现)进行测试:

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

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

  1. 从Visual Studios 2015为Android创建共享库"(或其他方法)创建共享库(Calculator.so)[成功]
  2. 在src/main/中创建jniLibs文件夹及其子文件夹(在我的情况下为x86相关文件夹)
  3. 将android.mk文件添加到src/main/jniLibs中,该文件必须放在其中(?)
  4. include语句:MainActivity中没有"lib"和".so"的System.loadLibrary("Calculator")

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

尝试

    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 OS中最重的硬件之一.检测面部,事物,效果和成千上万的功能,NDK是最好的. 对您的步骤有帮助:

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

    static {  System.loadLibrary("my_library") }
    

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

当您要调用本机方法时,只需使用"native"关键字.例如:

private native int nGetNumberFromNativeSide();

只需在需要的地方调用此方法即可获得结果.但是要在gradle端构建ndk,请查看此 answer .为了在Android.mk中构建库,这些示例行可能会帮助您:

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

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. Create the shared library (Calculator.so) from Visual Studios 2015 "Create shared library for Android" (or something) [successful]
  2. Create jniLibs folder in src/main/ with its subfolders (x86 the relevant one in my case)
  3. Add the Android.mk file in src/main/jniLibs which has to be placed there (?)
  4. Include statement: System.loadLibrary("Calculator") without "lib" and ".so" in MainActivity

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.

Tried

  • Different #include <myLib> statements in native-lib.cpp, like s
  • Different Android.mk settings (but i'm new to make files so not even tutorials helped me much with my specific problem ::) )
  • Other locations for the libCalculator.so like in the subfolder x86
  • and many others - simply not reminding atm (wasntme)

Your help is highly appreciated!

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)

解决方案

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. 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") }
    

    Without "lib" and ".so" extensions.

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

private native int nGetNumberFromNativeSide();

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)

  • You can put name anything you want, but dont add lib and .so extensions. Ndk is already doing it.
  • I have already gave Android.mk example.
  • When you build Android.mk file, it locates your libraries appropriate folder. Like 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天全站免登陆