Tuesday, 22 January 2019

Exclude JNI libs based on flavor in a smart way

I have an Android project that uses some native libs that are quite big in size. I have several flavors in this project and some of them don't make use of these libs so I would like to exclude them from the APK.

I know several ways to achieve this but I would like to use some nice code in the build.gradle file to reduce possible errors and learn groovy.

I have to mention that there is a boolean buildConfigField (called DO_IT in this example). If DO_IT is false then JNI libs are to be excluded.

This is the way I do it now:

defaultConfig {
     buildConfigField "boolean", "DO_IT", "true"
}

productFlavors {
    flavor1 {
        // for this flavor JNI libs will be included
    }
    flavor2 {
        // for this flavor JNI libs will NOT be included
        buildConfigField "boolean", "DO_IT", "false"
        ndk {
            abiFilters ''
        }
    }
}

Remarks:

1 - Consider that I have many flavors with tons of properties and I don't want to replicate the block

ndk {
   abiFilters ''
}

but I cannot manage to put this block it inside a method.

2 - The pefect solution would just exclude the libs based on the DO_IT buildConfigField in a routine outside the flavors' blocks EG in the defaultConfig.



from Exclude JNI libs based on flavor in a smart way

No comments:

Post a Comment