Android studio flavor dimensions processed all, not selected one

595 Views Asked by At

I have a flavor dimensions in module build.gradle file, and gradle build process runs through all build variants whatever actual build variant is. Here is module build.gradle:

flavorDimensions 'type', 'jnitype'
    productFlavors {
        demo {
            dimension 'type'
            versionNameSuffix '.demo'
        }
        production {
            dimension 'type'
            versionNameSuffix '.production'
        }
        usejni {
            dimension 'jnitype'
            versionNameSuffix '.usejni'
            copy {
                from('../jnilib/data') {
                    include 'sdk_data.gpu'
                    .... 
                }
                into 'src/main/assets/data'
            }
        }
        nojni {
            dimension 'jnitype'
            versionNameSuffix '.nojni'
            delete('src/main/assets/data/*.*')
            packagingOptions {
                exclude 'lib/arm64-v8a/sdk.so'
                ...
            }
        }
    }

So no matter what build variant selected, demoUsejni or demoNojni, gradle runs through 'usejni' and then 'nojni' variants - it copies files and libraries, and then deletes them. I used gradle debug to confirm this.

How can I tell gradle to use just a selected build flavor?

AS 3.5.2, gradle plugin 5.4.1, android build tools 3.5.2.

1

There are 1 best solutions below

0
Alex Gata On

Finally, the solution is found: use gradle task name to check for required flavor. For an action at compile type gradle task name will be ":module_name:assembleFlavor1Flavor2...FlavornBuildtype". So i will check for "assemble" and "Nojni"/"Usejni".

String taskName = "";
if (gradle.startParameter.taskNames.size > 0)
    taskName = gradle.startParameter.taskNames.get(0)

usejni {
    dimension 'jnitype'
    versionNameSuffix '.usejni'
    if (taskName.contains("Usejni") && taskName.contains("assemble")) {
        copy {
            from('../jnilib/data') {
                include 'sdk_data.gpu'
                .... 
            }
            into 'src/main/assets/data'
        }
    }
}
nojni {
    dimension 'jnitype'
    versionNameSuffix '.nojni'
    if (taskName.contains("Nojni") && taskName.contains("assemble")) {
        delete('src/main/assets/data/*.*')
        packagingOptions {
            exclude 'lib/arm64-v8a/sdk.so'
            ...
        }
    }
}

Like a charm!

Hint for solution found in this answer: How to get current buildType in Android Gradle configuration