1、ndk ffmpeg不行,那么先自己做一个简单的myso
2、先直接用gcc编译果然链接不行,
gcc -shared -fPIC -o libmyso.so myso.c
用ndk里按照网上说的,还是有问题,报错,但是错误里明显把android studio的调用命令给出来了,那么用这个命令
Build command failed.
Error while executing process /Users/wangt/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/wangt/AndroidStudioProjects/NativeTestSo/app/.cxx/Debug/6e5m5423/arm64-v8a nativetestso}
ninja: Entering directory `/Users/wangt/AndroidStudioProjects/NativeTestSo/app/.cxx/Debug/6e5m5423/arm64-v8a'
[1/1] Linking CXX shared library /Users/wangt/AndroidStudioProjects/NativeTestSo/app/build/intermediates/cxx/Debug/6e5m5423/obj/arm64-v8a/libnativetestso.so
FAILED: /Users/wangt/AndroidStudioProjects/NativeTestSo/app/build/intermediates/cxx/Debug/6e5m5423/obj/arm64-v8a/libnativetestso.so
: && /Users/wangt/Library/Android/sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/wangt/Library/Android/sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/wangt/Library/Android/sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnativetestso.so -o /Users/wangt/AndroidStudioProjects/NativeTestSo/app/build/intermediates/cxx/Debug/6e5m5423/obj/arm64-v8a/libnativetestso.so CMakeFiles/nativetestso.dir/native-lib.cpp.o ../../../../libs/libmyso.so -llog -latomic -lm && :
CMakeFiles/nativetestso.dir/native-lib.cpp.o: In function `Java_com_example_nativetestso_MainActivity_stringFromJNI':
/Users/wangt/AndroidStudioProjects/NativeTestSo/app/src/main/cpp/native-lib.cpp:9: undefined reference to `getDataFromSo()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
/Users/wangt/Library/Android/sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/wangt/Library/Android/sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/wangt/Library/Android/sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -shared -fPIC -o libmyso.so myso.c
4 生成 apk 出错
abiFilters "arm64-v8a" 加上这个,不去生成其他的了
别忘了
sourceSets{
main{
jniLibs.srcDirs = ['libs']
}
}
现在运行出错
--------- beginning of crash
2021-08-27 05:25:44.159 17208-17208/com.example.nativetestso E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nativetestso, PID: 17208
java.lang.UnsatisfiedLinkError: dlopen failed: library "../../../../libs/libmyso.so" not found: needed by /data/app/~~iNqDlLH21o2dB9AsKojg5Q==/com.example.nativetestso-CMkPfzF7qn_2Ir-QclsDUQ==/lib/arm64/libnativetestso.so in namespace classloader-namespace
5
set(DIR ../../../../libs)
add_library(myso
SHARED
IMPORTED)
set_target_properties(myso
PROPERTIES IMPORTED_LOCATION
${DIR}/libmyso.so)
可以直接用这一句替换
set(FF ${CMAKE_SOURCE_DIR}/../../../libs/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${FF}")
5 java.lang.UnsatisfiedLinkError: dlopen failed: library "libmyso.so" not found: needed by
原来报这个错
拷贝到这个位置后,报java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found: needed by /data/app/~~p8tRq5oyoGWLks5,看来有用
---还是不行啊,刚才怎么搞出来另外一个问题的??
看来第5条没有用
6、把生成的apk 重命名为 zip,然后解压,发现lib里面没有,看来找到问题原因了,后面像早一样记录在笔记本上,to thi.
最后还是要建立 架构的子目录来解决(终于解决)
最后的cmake
cmake_minimum_required(VERSION 3.10.2) # Declares and names the project. project("nativetestso") #set(DIR ../../../../libs) set(FF ${CMAKE_SOURCE_DIR}/../../../libs/arm64-v8a/) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${FF}") #set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../libs/) #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../../../libs/) add_library( # Sets the name of the library. nativetestso SHARED native-lib.cpp ) find_library( # Sets the name of the path variable. log-lib log) target_link_libraries( # Specifies the target library. nativetestso myso ${log-lib})
最后的build.gradle(module app)
plugins { id 'com.android.application' } android { compileSdk 31 defaultConfig { applicationId "com.example.nativetestso" minSdk 21 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags '' abiFilters "arm64-v8a" arguments "-DANDROID_STL=c++_shared" } } } sourceSets{ main{ jniLibs.srcDirs = ['libs'] } }