问题描述
我是NDK的新手。
我想在我的标准Java Android Studio项目中添加一些C ++代码。
我做了什么:
-
在主文件夹下添加cpp文件夹
-
在my cpp文件夹中添加myccplib.cpp和myccplib.h
-
在Gradle(模块)中添加以下内容:
android { externalNativeBuild { cmake { version "3.10.2" } } }
myccplib.cpp中的 -
代码:
#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()); }
-
MainActivity.java中的代码:
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("myccplib"); // "myjni.dll" in Windows,"libmyjni.so" in Unixes
}
public native String stringFromJNI();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String s = stringFromJNI();
Log.d("stringFromJNI " + s);
}
}
我的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)
我使用的是Android 4.0。
有人能告诉我我想念什么吗?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)