Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Android NDK build for some build variant

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.

like image 297
andrfog Avatar asked Oct 18 '22 20:10

andrfog


1 Answers

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"
}
like image 79
andrfog Avatar answered Oct 21 '22 04:10

andrfog