Gradle / android: one ndk build for multiple flavors?

I have a build.gradle setup with the following (I obviously excluded parts that don'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 its own code for each + buildType attribute. so not only debug and release, but also flavor1Debug, flavor2Release, etc., which takes forever

how can I say gradle only for externalNativeBuild for two types of build and use them for all flavors?

+5
source share
1 answer

This is true, if you look at the .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 : 

etc. for each buildVariant. What can you do to reduce assembly time?

  • Extract the time-consuming part to a static library (or a set of static libraries) and leave only the final link for the built-in ndkBuild . Use these static libraries as $(PREBUILT_STATIC_LIBRARY) .

  • Disable the fully integrated ndkBuild and install

     jniLibs.srcDir 'src/main/libs' 

    The easiest way to disable integrated ndkBuild is to specify

     jni.srcDirs = [] 

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

     tasks.all { task -> if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) { task.enabled = false } } 
0
source

Source: https://habr.com/ru/post/1266909/


All Articles