Android NDK release flag

Viewed 2663

I'm using the Android NDK (integrated with CMake and gradle). Currently the C++ Release-Flag is automatically set dependent on the Android Build-Type (Release/Debug). I'd like to compile/link the native code always with the release flag. How can this be done?

Thank you

2 Answers

Inside the module build.gradle (for example, app/build.gradle), inside android {} block, add:

android {
   ...
    externalNativeBuild {
        cmake {
            arguments "-DCMAKE_BUILD_TYPE=Release"
        }
    }
}

This would overwrite the default debug settings as arguments are appended to default settings. Check the generated build files at .externalNativeBuild/cmake/debug/$arch :

  • cmake_build_command.txt
  • android_gradle_build.json

Gerry's answer does not work for me.

If anyone has the same problem, you can try use the config shown in the official docs

For example

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        externalNativeBuild {
            cmake {
                arguments "-DCMAKE_BUILD_TYPE=Release"
            }
        }
    }

    externalNativeBuild {
        cmake {
            path file('../app/src/main/cpp/your-proj/CMakeLists.txt')
        }
    }
}
Related