Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

packaging options based on product flavor in gradle

I m using packaging options to exclude some libs. Is it possible to have packaging options based on product flavor. For example -

android {
    productFlavors {
        flavorDimensions 'models'
            S2 {
                flavorDimension 'models'
                minSdkVersion 22
                ....
            }
            S6 {
                flavorDimension 'models'
                minsdkversion 22
                ....
            }
        }


    packagingOptions {
        exclude 'lib/armeabi/libs2.so'
        exclude 'lib/arm64-v8a/libs6.so
    }
}

Now in above code, I want to exclude only 'lib/armeabi/libs2.so' in apk generated for s6 flavor and want to exclude only 'lib/arm64-v8a/libs6.so' in apk generated for s2 flavor

How can we achieve this.

like image 220
Timm Carnot Avatar asked Feb 22 '16 07:02

Timm Carnot


People also ask

What is Gradle product flavors?

This means you can generate different versions or variants of your app using a single codebase. Product flavors are a powerful feature of the Gradle plugin from Android Studio to create customised versions of products. They form part of what we call Build Variants.

What are buildTypes and product Flavours in Gradle and what can you use them for?

Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.

When would you use a product Flavour in your build setup?

You use same core ingredients to make the base but will use different toppings for each one to have a different taste. Similarly, android apps can have the same base functionalities with changes to some of the features like styles, logo etc. This can be achieved using product flavours.

What is BuildConfig flavor?

BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig. FLAVOR.


1 Answers

Based on @Dodolong solution:

  packagingOptions {
      ...
        gradle.startParameter.getTaskNames().each { task ->
            if (task.contains('assemble')) {
                if (task.contains('s2')) {
                    exclude 'lib/**/my_cool_lib1.so'
                } else if (task.contains('s6')) {
                    exclude 'lib/**/my_cool_lib2.so'
                }
            }
        }

  }
like image 104
b0b Avatar answered Sep 28 '22 23:09

b0b