1

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.

Alex Gata
  • 31
  • 1
  • 4
  • Can you explain a bit more.. Does it runs both flavors no matter what is selected. – vijaypalod Nov 13 '19 at 10:03
  • @vijaypalodes >Does it runs both flavors no matter what is selected? Exactly. While building, it runs thru 'usejni' flavor and copies files, and then thru 'nojni' flavor and removes files and libraries. All in one build pass, yes. – Alex Gata Nov 13 '19 at 10:28

1 Answers1

1

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

Alex Gata
  • 31
  • 1
  • 4