pshashi04
pshashi04

Reputation: 242

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.

Upvotes: 13

Views: 2955

Answers (4)

b0b
b0b

Reputation: 464

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'
                }
            }
        }

  }

Upvotes: 4

Dodolong
Dodolong

Reputation: 23

I encountered the same problem, I added the code phrases in dependencies closure. The concept is to detect flavor type when task being executed. And find instance of productFlavors to exclude libraries.

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

dependencies {
    gradle.startParameter.getTaskNames().each { task ->
        println 'task name is :' + task
        if (task.contains('assemble')) {
            def android = project.extensions.findByName("android")

            if(android != null) {
                com.android.build.gradle.internal.dsl.PackagingOptions packagingOptions = android["packagingOptions"]

                if (packagingOptions != null) {
                    if(task.contains("S2")){
                        packagingOptions.exclude('lib/lib_for_s2.so')
                    } else if(task.contains("S6")) {
                        packagingOptions.exclude'lib/lib_for_s6.so')
                    }
                }
            }
            println 'libraries will be excluded: ' + packagingOptions.excludes
        }
    }
}

Upvotes: 1

3c71
3c71

Reputation: 4541

Stumble across the same issue but wanted to remove some assets files, running Android Studio 4.0.

It was actually super easy to exclude .so files per variant/flavor:

productFlavors {
    flavor1 {
        packagingOptions {
            exclude 'lib/x86/libshared.so'
        }
    }
}

Sadly, it doesn't work for assets!?

Upvotes: 0

L.J.
L.J.

Reputation: 131

I've encountered the same issue, and then, I developed a gradle plugin to resolve this. You can check it(android-soexcluder) out here

It is super easy to use:

soexcluder {
    s2 {
        exclude 'lib/arm64-v8a/libs6.so'
    }

    s6 {
        exclude 'lib/armeabi/libs2.so'
    }
}

Beside this, there is even a sample project inside the repository

Upvotes: 9

Related Questions