androidandroid-ndkbuild.gradleandroid.mk

Why build.gradle which uses Android.mk to build native files compiles them on every build?


I am maintaining a legacy Android project which uses Android.mk build file instead of CMake. Previously the native compilation was done from command line batch files. I changed the build system to use build.gradle.

For some strange reason when I build the app the native file compilation is invoked every time I build the app. This happens even if I haven't done any modifications to native files.

I just tested building a project which I had built before and I hadn't done any changes to it. I build with 4 ABIs (armeabi-v7a, arm64-v8a, x86 and x86_64) and the build process spends about 8 minutes per ABI doing the Ndk build tasks:

Task :app:buildNdkBuildRelease[<ABI>]

Which means every time I make a build I have to wait for over 30 minutes! With my other projects that use CMake the native stuff isn't compiled again if there are no changes.

The Android.mk is invoked from build.gradle like this:

externalNativeBuild {
    ndkBuild {
        path '../jni/Android.mk'
    }
}

Is there any way in (Android.mk or build.gradle) to fix this so that native is compiled only if there are actual changes in the native files?

I'm using Windows 11 and Android Studio Electric Eel | 2022.1.1.


Solution

  • The task runs every time because Gradle has no way of knowing if ndk-build needs to do any work (the same happens with the CMake task). If ndk-build is doing work every time, that means there's something in your build that believes something needs to be recompiled. This isn't generally a problem, so it's most likely something in your build scripts (there's always the potential that this is just a bug, be there have been no other reports and ndk-build rarely changes). Those weren't included in the question, so it's impossible to say what that might be.