简体   繁体   English

Android 加载原生库

[英]Android load native library

I'm trying to load a library I built with the standalone NDK toolchain.我正在尝试加载使用独立 NDK 工具链构建的库。

I built libGLmove.so and placed it in libs/armeabi of my Eclipse project我构建了 libGLmove.so 并将其放在我的 Eclipse 项目的 libs/armeabi 中

However, the call to System.loadLibrary("GLmove") throws an UnsatisfiedLinkError但是,对System.loadLibrary("GLmove")的调用会引发UnsatisfiedLinkError

Any ideas as to how to resolve the problem or make Android find my library?关于如何解决问题或让 Android 找到我的图书馆的任何想法? How does ndk-build package the library after it builds it? ndk-build package 库在构建后如何?

Edit: The exact compile flags are:编辑:确切的编译标志是:

/Users/thomas/Documents/android-ndk-r5b/toolchains/arm-eabi-4.4.0/prebuilt/darwin-x86/bin/arm-eabi-g++ --sysroot=/Users/thomas/Documents/android-ndk-r5b/platforms/android-8/arch-arm -march=armv7-a -mfloat-abi=softfp -mfpu=neon -Wl,--fix-cortex-a8 -fno-exceptions -fno-rtti -nostdlib -fpic -shared -o GLmove.so -O3 /Users/thomas/Documents/android-ndk-r5b/toolchains/arm-eabi-4.4.0/prebuilt/darwin-x86/bin/arm-eabi-g++ --sysroot=/Users/thomas/Documents/android-ndk -r5b/platforms/android-8/arch-arm -march=armv7-a -mfloat-abi=softfp -mfpu=neon -Wl,--fix-cortex-a8 -fno-exceptions -fno-rtti -nostdlib -fpic -shared -o GLmove.so -O3

Have you checked in the resulting.APK file (use a zip utility to look at/unarchive it) to see if your library makes it through packaging?您是否检查了结果.APK 文件(使用 zip 实用程序查看/取消归档它)以查看您的库是否通过打包完成? I'm a little suspicious that it might not, because I notice that everything that gets built into the "libs" folder in the project and on my build machine goes into a folder called "lib" (no 's') in the APK.我有点怀疑它可能不会,因为我注意到在项目和我的构建机器上构建到“libs”文件夹中的所有内容都进入了 APK 中名为“lib”(没有“s”)的文件夹.

I wouldn't be too surprised if it turned out that the Eclipse build process doesn't package up any libraries it doesn't know about.如果事实证明 Eclipse 构建过程没有 package 任何它不知道的库,我不会太惊讶。 This is, of course, unlike what happens with resources, which just get packaged by virtue of being in the right place.当然,这与资源发生的情况不同,资源只是由于位于正确的位置而被打包。

If you find your library is not in your APK file, I don't think you can just manually put it in there, since it won't show up in the package manifest and will break any signing as well.如果您发现您的库不在您的 APK 文件中,我认为您不能手动将其放入其中,因为它不会出现在 package 清单中,并且也会破坏任何签名。

You don't mention whether or not your Eclipse project is an NDK project (right click on the project, Android Tools->Add Native Support.) If not, I suspect you'll need to make it into one and then add your library to the Android.mk file as a dependency and not a target.您没有提及您的 Eclipse 项目是否是 NDK 项目(右键单击该项目,Android Tools->Add Native Support。)如果不是,我怀疑您需要将其合二为一,然后添加您的库将 Android.mk 文件作为依赖项而不是目标。

Or: you could try putting your library into /res in the project and use System.load() instead of System.loadLibrary() to load it.或者:您可以尝试将您的库放入项目中的 /res 并使用 System.load() 而不是 System.loadLibrary() 来加载它。 I'll admit that I've never tried that myself, tho.我承认我自己从未尝试过。

I was running into this same problem.我遇到了同样的问题。 The things I had wrong.我做错的事情。

  1. In the make file I had the "LOCAL_SRC_FILES" spelled wrong.在制作文件中,我的“LOCAL_SRC_FILES”拼写错误。
  2. In the c source file I had the library name inside the method name在 c 源文件中,我在方法名称中有库名称

Java_com_my_namespace_libname_activity_methodName(JNIEnv* env, jobject _this) {
//Fancy Native Junk Here
}

Once I fixed those two things, re-ran ndk-build and refreshed the eclipse project with F5 it started to work.一旦我修复了这两个问题,重新运行 ndk-build 并使用 F5 刷新 eclipse 项目,它就开始工作了。

You don't give very many details, but it may be that the .so you've built relies on a library that isn't available on the version of phone you're using.您没有提供太多详细信息,但可能是您构建的.so依赖于您正在使用的手机版本上不可用的库。

I've not found any way to tell the NDK which Android SDK version you're targeting so don't have any very clear idea of how this side of it should work, but it looks like it would be easy to bring in a dependency from a newer SDK version into your .so so it won't load on all phones.我还没有找到任何方法来告诉 NDK 你的目标是哪个 Android SDK 版本,所以对它的这一面应该如何工作没有任何非常清楚的想法,但看起来很容易引入依赖项从较新的 SDK 版本到您的.so它不会加载到所有手机上。

Could you please check the syntax and the location of System.loadLibrary("GLmove")您能否检查一下 System.loadLibrary("GLmove") 的语法和位置

the System.loadLibrary call should be in static block of the Java source file System.loadLibrary 调用应该在 Java 源文件的 static 块中

static {
    System.loadLibrary("nativelib");
}

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html

If your native library needs other libraries you'll need to load them first.如果您的本机库需要其他库,您需要先加载它们。 If that doesn't help, check to see that your project directory does not contain spaces.如果这没有帮助,请检查您的项目目录是否包含空格。

Also, you may find this tutorial helpful:http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/此外,您可能会发现本教程很有帮助:http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/

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

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