平时都是在源码环境下开发,很久没有用到AS NDK了,今天通过AS 终端执行ndk-build命令时报错:ld: error: failed to write to the output file: Permission denied
[armeabi-v7a] SharedLibrary : libxl.so
ld: error: failed to write to the output file: Permission denied
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [C:/Users/Administrator/AppData/Local/Android/Sdk/ndk/23.0.7599858/build//../build/core/build-binary.mk:715: C:/Users/Administrator/AndroidSt
udioProjects/MyKotlin/app/src/main/obj/local/armeabi-v7a/libxl.so] Error 1
1.尝试修改权限:
看log是写入权限问题,开始看了win7下工程目录文件夹的确都是只读状态,就一顿操作取消只读属性,可重新打开文件夹,发现仍是显示只读状态,实际是可以写入的,这是win7的问题。但是还是报同样的权限错误,头大。
然后想着会不会是c盘的某些只读属性有影响,刚好c盘空间也不太够了,就把项目工程文件夹和sdk、ndk目录都移到了D盘,想着这下总该没有权限问题了吧,结果执行ndk-build,还是一样的结果...
2.尝试不用ndk-build方式编译,改用官方推荐的cMake方式。一通折腾,结果一样:
Determining if the C compiler works failed with the following output:
Change Dir: C:/Users/Administrator/AndroidStudioProjects/MyKotlin/app/.cxx/cmake/debug/armeabi-v7a/CMakeFiles/CMakeTmp
Run Build Command:"C:\Users\Administrator\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe" "cmTC_b2791"
[1/2] Building C object CMakeFiles/cmTC_b2791.dir/testCCompiler.c.o
[2/2] Linking C executable cmTC_b2791
FAILED: cmTC_b2791
cmd.exe /C "cd . && C:\Users\Administrator\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe --target=armv7-none-linux-androideabi22 --gcc-toolchain=C:/Users/Administrator/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/Administrator/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--gc-sections CMakeFiles/cmTC_b2791.dir/testCCompiler.c.o -o cmTC_b2791 -latomic -lm && cd ."
ld: error: failed to write to the output file: Permission denied
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
3.最后没办法,重新分析,发现不管是ndk-build还是cmake,编译过程的obj文件都是正常的输出了,只是最后链接成so文件夹时报了这个错误,我想着可能是NDK版本兼容问题(用的最新的ndk-23),所以就去网上下载了旧版本ndk-21,重新设置ndk路径后 ,ndk-build一下,终于正常
[armeabi-v7a] StaticLibrary : libstdc++.a
[armeabi-v7a] SharedLibrary : libnative-lib.so
[armeabi-v7a] Install : libnative-lib.so => libs/armeabi-v7a/libnative-lib.so
总是尝鲜,不管是studio还是nkd都是用最新的还是有坑,可能有各种意想不到的兼容问题,慎用。NDK历史版本下载链接:
https://developer.android.google.cn/ndk/downloads/revision_history