Gradle/android: single ndk build for multiple flavors?
Asked Answered
U

1

8

i've got a build.gradle file setup with the following (i've obviously excluded parts that shouldn't matter for brevity):

android { defaultConfig { ndk { abiFilters 'armeabi', 'armeabi-v7a', 'x86' } }
productFlavors {
    flavor1 { ... }
    flavor2 { ... }
    flavor3 { ... }
    flavor4 { ... }
    flavor5 { ... }
}
buildTypes {
    debug {
        externalNativeBuild { ndkBuild { cFlags '-DDEBUG' } }
        ...
    }
    release {
        externalNativeBuild { ndkBuild { cFlags '-DRELEASE' } }
        ...
    }
}
externalNativeBuild {
    ndkBuild {
       path 'jni/Android.mk'
    }
}

it works, but it compiles the native code for each flavor+buildType. so not only debug and release, but also flavor1Debug, flavor2Release, etc., which takes forever

how do i tell gradle to only do the externalNativeBuild for the two build types, and to use those for all the flavors?

Unset answered 19/4, 2017 at 11:59 Comment(1)
I have a similar issue with a cmake build. Gradle configuration is taking almost one hour after adding cmake build. We do have quite a few flavors and our C++ library and cmake configuration is quite large too.Patella
S
0

This is true, if you look into the file .externalNativeBuild/ndkBuild/flavor1Debug/armeabi/ndkBuild_build_command.txt, you will see something similar to mine:

Executable : ~/Library/Android/sdk/ndk-bundle/ndk-build
arguments : 
NDK_PROJECT_PATH=null
APP_BUILD_SCRIPT=~/proj/jni/Android.mk
APP_ABI=armeabi
NDK_ALL_ABIS=armeabi
NDK_DEBUG=1
APP_PLATFORM=android-21
NDK_OUT=~/app/build/intermediates/ndkBuild/flavor1/debug/obj
NDK_LIBS_OUT=~/app/build/intermediates/ndkBuild/flavor1/debug/lib
APP_SHORT_COMMANDS=false
LOCAL_SHORT_COMMANDS=false
-B
-n
jvmArgs : 

and so on for each buildVariant. What you can do to reduce build time?

  1. Extract the time-consuming part into a static library (or a set of static libraries), and leave only final link for the integrated ndkBuild. Use these static libraries as $(PREBUILT_STATIC_LIBRARY).

  2. Disable the integrated ndkBuild altogether, and set

    jniLibs.srcDir 'src/main/libs'
    

    The easiest way to disable integrated ndkBuild is by pointing

    jni.srcDirs = []
    

    But you can also keep Android Studio indexing of your cpp files, but disable the gradle task:

    tasks.all { task ->
        if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) {
            task.enabled = false
        }
    }
    
Seamaid answered 25/5, 2017 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.