How to disable Android NDK build for some build variant

4.1k views Asked by At

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'
            }
        }
    }
}
  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.

2

There are 2 answers

0
andrfog On BEST ANSWER

Here is how I solved it.

This way Gradle build works for developers with and without NDK installed (and on the build server), which was the goal.

/*
 * 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"
}
1
yakobom On

Regarding #1, if I understand correctly you want to do (or skip) certain things according to the buildType. If so, you can look at one of the many discussions on this subject, such as this one: get current buildType.

Regarding #2, it is not very clear what you want. If you are seeking an alternative to setting the NDK path in local.properties, you can set it via ANDROID_NDK_HOME environment variable. If you want to prevent gradle from failing, check if the ndk.dir exists in the relevant places of your build.gradle script (using properties.getProperty('ndk.dir')).