简体   繁体   English

本机活动和“无法加载本机库”

[英]Native activity and “unable to load native library”

I am attempting to build an Android-10 NDK native activity based on the "native-activity" sample project from the Android NDK folder. 我正在尝试基于Android NDK文件夹中的“native-activity”示例项目构建Android-10 NDK本机活动。 However, my native activity crashes with the following runtime exception when I open Eclipse and select "Run As -> Android Application" for my project: 但是,当我打开Eclipse并为我的项目选择“Run As - > Android Application”时,我的本机活动崩溃并出现以下运行时异常:

02-09 03:02:12.599: E/AndroidRuntime(881): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.native_activity/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app-lib/com.example.native_activity-1/[library name].so 02-09 03:02:12.599:E / AndroidRuntime(881):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.native_activity / android.app.NativeActivity}:java.lang.IllegalArgumentException:无法加载本地库:/data/app-lib/com.example.native_activity-1/ [图书馆名称] .so

However, I have confirmed that the file "lib[library name].so" already exists in the "libs/armeabi" (etc.) paths. 但是,我已经确认文件“lib [library name] .so”已经存在于“libs / armeabi”(等)路径中。 My native activity eventually needs to load three ".so" files, but this error persists whether I am attempting to load one ".so" file or all three ".so" files. 我的本机活动最终需要加载三个“.so”文件,但是如果我尝试加载一个“.so”文件或所有三个“.so”文件,则此错误仍然存​​在。 My "AndroidManifest.xml" file is as follows: 我的“AndroidManifest.xml”文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.native_activity"
    android:versionCode="1"
    android:versionName="1.0">

<!-- This is the platform API where NativeActivity was introduced. -->
<uses-sdk android:minSdkVersion="9" />

<!-- This .apk has no Java code itself, so set hasCode to false. -->
<application android:label="@string/app_name" android:hasCode="false">

    <!-- Our activity is the built-in NativeActivity framework class.
         This will take care of integrating with our NDK code. -->
    <activity android:name="android.app.NativeActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden">
        <!-- Tell NativeActivity the name of or .so -->
        <meta-data android:name="android.app.lib_name"
            android:value="[library name]" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest> 
<!-- END_INCLUDE(manifest) -->

What needs to be done to fix this runtime exception? 需要做些什么来修复此运行时异常?

A useful way to find more information about the load failure is to make a basic Android application and use System.loadLibrary to load the native libraries in the onCreate method, bypassing the NativeActivity altogether. 查找有关加载失败的更多信息的有用方法是创建一个基本的Android应用程序,并使用System.loadLibraryonCreate方法中加载本机库,完全绕过NativeActivity。

eg 例如

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    System.loadLibrary("foo");
    System.loadLibrary("bar");
    System.loadLibrary("depends-on-foo-and-bar");
}

Then, instead of getting the not so useful "Unable to load native library" message NativeActivity provides, you will get a more useful message such as 然后,您将获得一个更有用的消息,如,不是获得不那么有用的“无法加载本机库”消息NativeActivity提供

java.lang.UnsatisfiedLinkError: dlopen failed: could not load library "lib42.so" needed by "libdepends-on-foo-and-bar.so"; java.lang.UnsatisfiedLinkError:dlopen失败:无法加载“libdepends-on-foo-and-bar.so”所需的库“lib42.so”; caused by library "lib42.so" not found 由库“lib42.so”引起的未找到

In your JNI folder what is the "" set to ? 在你的JNI文件夹中,“”设置为什么? "[library name]" name should be set as the name the package you are building. “[库名称]”名称应设置为您正在构建的包的名称。

The problem seems to be that the system is looking for a library with a -1 appended to its name 问题似乎是系统正在寻找一个附加了-1名称的库

(from your error message) /data/app-lib/com.example.native_activity-1 (来自您的错误消息)/data/app-lib/com.example.native_activity-1

Ive seen that andoird does this when a library previosuly exists then adds -1, -2 etc to the name. 我已经看到,当一个库普遍存在然后在名称中添加-1,-2等时,它会执行此操作。 Not sure why but pretty sure its a versioning thing that you might solve with an uninstall or fresh install. 不确定为什么,但很确定它是一个版本控制的东西,你可以通过卸载或全新安装解决。

This happens due to missing dependencies (or even implementations). 这是由于缺少依赖性(甚至实现)而发生的。 You may be getting the "unable to load library: libFoo.so" but the real dependency missing may be libBar.so that is depended by libFoo.so. 您可能会收到“无法加载库:libFoo.so”,但缺少真正的依赖项可能是libBar.so,它依赖于libFoo.so。 Or you may simply have declared functions and called them but have no implementation for them. 或者您可能只是声明了函数并调用它们但没有实现它们。

The problem is due to the way building an so file works. 问题是由于构建so文件的方式有效。 When you build a shared object using gcc, the linking step doesn't truly happen. 使用gcc构建共享对象时,链接步骤并不会真正发生。 For example within libFoo.so, you may declare a myFunc() prototype and call it. 例如,在libFoo.so中,您可以声明一个myFunc()原型并调用它。 And you can skip the implementation of myFunc(){}. 您可以跳过myFunc(){}的实现。 The gcc will happily build you a .so file. gcc很乐意为你建立一个.so文件。 And it will happily end up in the apk. 它会愉快地结束在apk中。 However when it's time to load library, this will break with the "unable to load library" message. 但是,当加载库时,这将打破“无法加载库”消息。

There are two ways to deal with this: You either do what AndrewJC tells you in his answer. 有两种方法可以解决这个问题:你要么做AndrewJC在答案中告诉你的事情。 Or you start commenting code out until you are able to load again. 或者您开始​​注释代码,直到您能够再次加载。 Thus locating the function implementation missing. 因此找不到函数实现。

IMPORTANT: the -1 -2 suffixes that are added to the so files ARE NOT the problem. 重要提示:添加到so文件的-1 -2后缀不是问题。 That comment misled me to spend a few days only to find that those numeric suffixes are actually the expected behavior by the android package manager. 该评论误导我花了几天时间才发现那些数字后缀实际上是android包管理器的预期行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM