Android NDK项目:java.lang.UnsatisfiedLinkError:dalvik.system.PathClassLoader
我是NDK的新手 我想在我的标准java Android工作室项目中添加一些C++代码。 我所做的:Android NDK项目:java.lang.UnsatisfiedLinkError:dalvik.system.PathClassLoader,java,android,cmake,android-ndk,java-native-interface,Java,Android,Cmake,Android Ndk,Java Native Interface,我是NDK的新手 我想在我的标准java Android工作室项目中添加一些C++代码。 我所做的: 在主文件夹下添加cpp文件夹 在我的cpp文件夹中添加myccplib.cpp和myccplib.h 在Gradle(模块)中添加以下内容: myccplib.cpp中的代码: #include "myccplib.h" #include <jni.h> #include <string> extern "C" J
#include "myccplib.h"
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_tof_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) {
std::string hello = "Hello, World!";
return env->NewStringUTF(hello.c_str());
}
我的CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
问题:我遇到了车祸:
2020-10-14 19:02:45.465 25918-25918/com.example.tof E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tof, PID: 25918
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.tof-t8Csh416BSdO6UBfZv0mOw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.tof-t8Csh416BSdO6UBfZv0mOw==/lib/arm64, /system/lib64, /product/lib64]]] couldn't find "libmyccplib.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
at java.lang.System.loadLibrary(System.java:1667)
at com.example.tof.MainActivity.<clinit>(MainActivity.java:37)
2020-10-1419:02:45.465 25918-25918/com.example.tof E/AndroidRuntime:致命异常:main
进程:com.example.tof,PID:25918
java.lang.UnsatifiedLinkError:dalvik.system.PathClassLoader[DexPathList[[zip文件”/data/app/com.example.tof-t8Csh416BSdO6UBfZv0mOw===/base.apk”],NativeLibraryDirectory=[/data/app/com.example.tof-t8Csh416BSdO6UBfZv0mOw==/lib/arm64,/system/lib64,/product/lib64]]找不到“libmyccplib.so”
位于java.lang.Runtime.loadLibrary0(Runtime.java:1067)
位于java.lang.Runtime.loadLibrary0(Runtime.java:1007)
位于java.lang.System.loadLibrary(System.java:1667)
位于com.example.tof.MainActivity。(MainActivity.java:37)
我使用安卓4.0
谁能告诉我我错过了什么
谢谢 你给我们看的代码似乎与stacktrace不匹配。stacktrace说找不到
libmyccplib.so
,这表明您在loadLibrary
调用中错误地包含了“lib”
前缀(即您实际上正在执行System.loadLibrary(“libmyccplib”);
),您能共享CMakeLists.txt的内容吗?确保System.loadLibrary中使用的库名称与CMakeLists.txt中target_link_库中指定的名称匹配。如果是您的第一个项目,您可以使用Android Studio的“Native C++”向导项目模板,而不是手动添加所有内容,因此您可以将其用作学习基础知识的参考。@PerracoLabs谢谢!我没有任何CMakeLists.txt:)我知道我应该把它放在cpp文件夹中。但是我应该在这个文件中写什么呢?顺便说一句,迈克尔我更正了坠机日志;我没有粘贴正确的版本。谢谢@Regis_AG如果您没有任何本机构建脚本文件,那么无论是CMakeLists.txt还是旧的Android.mk,都不会编译本机二进制文件。CMakeLists.txt指示编译和生成的内容。我建议使用“本机C++向导模板”,因此文件->新项目->本机C++抱歉,实际上,我有一个,但它是空的。现在,我复制并粘贴了我项目中模板C++项目的CMAKLISTS.TXT文件的内容。我在CMakeLists.txt中重命名了文件名。但我仍然有这个问题。请注意,我的CMakeLists.txt文件的内容似乎不像模板项目中那样由Android Studio着色(所有字符均为灰色);我不知道这是否会有影响。
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
2020-10-14 19:02:45.465 25918-25918/com.example.tof E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tof, PID: 25918
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.tof-t8Csh416BSdO6UBfZv0mOw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.tof-t8Csh416BSdO6UBfZv0mOw==/lib/arm64, /system/lib64, /product/lib64]]] couldn't find "libmyccplib.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
at java.lang.System.loadLibrary(System.java:1667)
at com.example.tof.MainActivity.<clinit>(MainActivity.java:37)