Search code examples
androidandroid-ndkandroid-gradle-pluginapkandroid-build

Android NDK Build: ld: error: failed to write to the output file: Permission denied


I could build my project in Windows 7 until I updated the NDK version to the latest (23.1.7779620).

I put the NDK files beside my Android SDK folder:

local.properties
sdk.dir=F\:\\Programming\\AndroidSdk
ndk.dir=F\:\\Programming\\AndroidNdk\\ndk-r23b

Now when I wanted to build an APK, I got this error:

> Task :app:externalNativeBuildDebug FAILED
Build gdx-audio_armeabi-v7a
[armeabi-v7a] SharedLibrary  : libgdx-audio.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: *** [F:/Programming/AndroidNdk/ndk-r23b/build//../build/core/build-binary.mk:715: F:/Projects/Android/myProject/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libgdx-audio.so] Error 1

ld: error: failed to write to the output file: Permission denied

My app build.gradle file:

    android {
        ...

        ndkVersion "23.1.7779620"
        externalNativeBuild {
            ndkBuild {
                path "jni/Android.mk"
            }
        }


        defaultConfig {
            ...
            externalNativeBuild {
                ndkBuild {
                    arguments "NDK_APPLICATION_MK:=jni/Application.mk", "APP_PLATFORM:=android-16", "-j8"
                    abiFilters "armeabi-v7a", "arm64-v8a"
                }
            }
        }
    }
}

Solution

  • You are correct. ProcessMonitor seems to indicate that in NDK version 23.1.7779620 the ld.exe is using a newer API calls that are not supported on Windows 7.

    Enter image description here

    It looks like ld erroneously interprets the failed API call as a "permission denied" error. The same happens in NDK version 23.0.7599858.

    The last NDK that seems to work on Windows 7 is 22.1.7171670.

    After the system was upgraded to Windows 10 ld started to work normally for the 23.x NDK versions.