且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android应用程序中使用现有的.so文件

更新时间:2023-10-17 23:23:04

您必须将Android NDK使用ndk-build生成的最终共享库链接到您所说的 的PDF共享库.您已经为ARM体系结构进行了编译 . (请确保是这种情况,否则该库将无法在Android上运行!)

You have to link your final shared library that is generated by the Android NDK using the ndk-build to the PDF shared library you said you already have compiled for the ARM architecture. (Ensure that this is the case, otherwise the library won't work on Android!)

为此,例如,如果您具有以下目录结构:

For that, if for example you have the following directory structure:

jni
└── libs
└────── my_shared_lib.so
└── Android.mk
└── Application.mk

您需要在 Android.mk 文件中包含以下内容:

You need to have the following content inside the Android.mk file:

LOCAL_PATH := $(call my-dir)

# define our prebuilt shared library as a module to the build system
include $(CLEAR_VARS)
LOCAL_MODULE := mysharedlib
LOCAL_SRC_FILES := libs/my_shared_lib.so
include $(PREBUILT_SHARED_LIBRARY)

# The final shared library that will be bundled inside the .apk
include $(CLEAR_VARS)
LOCAL_MODULE := mynativelib
LOCAL_LDLIBS := -landroid -llog
LOCAL_CPPFLAGS := -O0 -g3 -std=c++11 -Wall -Wextra 
LOCAL_SHARED_LIBRARIES := mysharedlib
LOCAL_C_INCLUDES := myheader1.h myheader2.h
LOCAL_SRC_FILES := src_file1.cpp src_file2.cpp
include $(BUILD_SHARED_LIBRARY)

Application.mk 文件的内容(用于使用C ++标准库,并为两个不同版本的ARM体系结构构建最终的共享库):

and the contents of the Application.mk file (for using the C++ Standard Library, and build the the final shared library for two different versions of the ARM architecture):

APP_OPTIM := debug
APP_PLATFORM := android-14
APP_STL := gnustl_static
APP_ABI := armeabi armeabi-v7a

然后,使用 ndk-build 脚本从Eclipse或从命令行编译代码之后,它将编译您最终的共享库,并将其链接到预先构建的共享库(即PDF)您说过要使用的共享库).

Then after you compile your code from within Eclipse or from the command line using the ndk-build script it'll compile you final shared library and link it against your prebuilt shared library (i.e. the PDF shared library you said you are trying to use).

对于共享库,生成并部署到设备/仿真器的apk包含最终的共享库以及与之链接的所有预构建共享库,而与静态库的链接相反未捆绑在APK中.

For shared libraries the apk that is generated and deployed to the device/emulator contains the final shared library as well as all the prebuilt shared libraries you linked against, in contrast with linking against static libraries which are not bundled inside the apk.

对于您的用例,在设备上解压缩apk之后,您应该在Android应用程序的lib目录中具有两个共享库. 您可以通过在终端上运行以下命令来进行检查:

For your use case you should have two shared libraries in the lib directory of your Android application after the apk is unpacked on the device. You can check this by running the following command from a terminal:

adb shell ls -l /data/data/com.company.myapp/lib

用应用程序的程序包名称替换com.company.myapp.

Replace com.company.myapp with your application's package name.

此外,不要忘记将以下内容放在Java类的静态上下文中:

Also, don't forget to put the following inside a static context of a Java class:

class MyClass
{
      static
      {
            try
            {
                System.loadLibrary("mynativelib");
            }
            catch (UnsatisfiedLinkError ule)
            {
               Log.e(TAG, "WARNING: Could not load native library: " 
                      + ule.getMessage());
            }
      }

       // other code here...
}

请注意,在System.loadLibrary方法调用中将相同名称用作最终的共享库名称.

Notice the use of the same name inside the System.loadLibrary method call as the final shared library name.