如何在Android应用程序中使用现有的.so文件 [英] How to use Existing .so file in android application

查看:138
本文介绍了如何在Android应用程序中使用现有的.so文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可在Android平台中将.doc/.docx转换为.pdf的库.

I was searching for library which should convert .doc/.docx to .pdf in android platform.

我得到了PdFTron android sdk,因为他们提供了libPDFNetC.so文件.

I got PdFTron android sdk,in that they have given libPDFNetC.so file.

对于Conversion,有一个称为Convert的类,里面有一个toPDF()方法, 在该方法中,他们内部调用了本机方法FileToPdf().我尝试了该代码,但无法调用该本机方法,并且出错了

For Conversion, there is class called Convert, inside that there is a method toPDF(), in that method they have internally called native method FileToPdf().I tried that code but unable to call that native method and was getting errors

我想知道是否存在与您一起存在的.so文件,并且如果您想调用.so文件中存在的本机方法,那么是否需要使用JNI?我对JNI不太了解.任何帮助.

I want to know that if there is existing .so file present with you and if you want to call native method which is present in .so file then is there need to use JNI?. i dont know much about JNI. any help.

推荐答案

您必须将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.

这篇关于如何在Android应用程序中使用现有的.so文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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