How to disable Android NDK build for some build variant

and*_*fog 5 c++ android-ndk build.gradle android-gradle-plugin gradle-plugin

I am using Android Studio 2.2 and have setup Gradle to build c/c++ sources with NDK via CMake.

Now I would like to disable NDK build for buildType "debug". For buildType "release" I would like to keep it.

The goal is to make NDK sources compile on the build server (using "release") but disable it for developers (using "debug").

This is the build.gradle file currently in use:

android {
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    defaultConfig {
        externalNativeBuild {                
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang"
                cppFlags "-std=c++14"
            }
        }

        ndk {
            abiFilters 'armeabi-v7a', 'x86'
        }
    }

    buildTypes {        
        release {            
            externalNativeBuild {                
                cmake {
                    arguments "-DANDROID_TOOLCHAIN=clang"
                    cppFlags "-std=c++14"
                }
            }

            ndk {
                abiFilters 'armeabi-v7a'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. How can I disable NDK build (externalNativeBuild) for defaultConfig or buildType "debug"?

  2. Other developers won't have NDK installed (local.properties without ndk.dir=PATH_TO_NDK). Is this possible to configure?

Thanks in advance


Edit:

This externalNativeBuild must be configured with a 'com.android.library'-module, not a 'com.android.application'-module.

and*_*fog 3

这是我解决问题的方法。

通过这种方式,Gradle 构建适用于安装或未安装 NDK(以及构建服务器)的开发人员,这就是我们的目标。

/*
 * As soon as Gradle is linked to the externalNativeBuild (cmake / ndkBuild) with a path to
 * CMakeLists.txt / Android.mk, the ndk.dir from local.properties file or the ANDROID_NDK_HOME
 * environment variable needs to be set, otherwise gradle fails.
 * E.g.:
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}
*/

// Only enable externalNativeBuild on machines with NDK installed -> valid ndkDir
def ndkDir = project.android.ndkDirectory;
if (ndkDir != null && !ndkDir.toString().isEmpty()) {

    externalNativeBuild.cmake.path = "CMakeLists.txt"
}
Run Code Online (Sandbox Code Playgroud)