Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

android - Dynamically generating product flavors

I've created an Android App which needs to be build in many (30+) flavors.

My idea was to generate the different productFlavors directly from the folder structure in my src directory, since the configuration is always very similar (basically just another packageName, a new launcher icon and some strings change).

The src folder look like this:

└── src
    ├── flavor1
    │?? ├── flavor2.keystore
    │?? ├── res
    ├── flavor2
    │?? ├── res
    │?? ├── flavor2.keystore    
    └── main
     ?? ├── AndroidManifest.xml
     ?? ├── java
     ?? └── res

If I had to create the gradle properties by hand it would look somehow like this:

android {

    ....

    productFlavors {
        flavor1 {
            packageName 'com.example.flavor1'
        }
        flavor2 {
            packageName 'com.example.flavor2'
        }
    }

}

Everytime I try to change the productFlavors configuration after its creation I get either an error or the changes / additions are ignored silently.
This could a be problem caused by me, because my Gradle / Groovy experience is very limited, or this isn't possible.

I mostly get error, saying that GroupableProductFlavorDsl_Decorated could not be manipulated the way I want.

What I'm trying to archive should somehow look like this:

android {

    ....

    def flavors = getMyFlavorsFromFileSystem()

    productFlavors {

    }

    flavors.each { name, config ->
        productFlavors[name] << config
    }

}

Note: I know this question is basically an duplicate of an older question, which sadly was never answered. Since Gradle is kind of new to the Android world, I'm hoping to get more answers as since the last time the question was asked, because more developers are using Gradle now.

Update:

Here some very simple approaches I tried:

Variant 1:

android {

    productFlavors {

    }

    productFlavors['flavor1'] << {
        packageName "com.example.flavor1"
    }

    productFlavors['flavor2'] << {
        packageName "com.example.flavor2"
    }
}

/*

A problem occurred evaluating root project 'MyProject'.
> GroupableProductFlavorDsl with name 'flavor1' not found.

*/

Variant 2:

android {

    productFlavors {

    }

    productFlavors['flavor1'] = {
        packageName "com.example.flavor1"
    }

    productFlavors['flavor2'] = {
        packageName "com.example.flavor2"
    }
}

/*

no error, but does not work

*/

Variant 3:

android {

    productFlavors {

    }

    productFlavors['flavor1'] = [packageName: "com.example.flavor1"]

    productFlavors['flavor2'] = [packageName: "com.example.flavor2"]
}

/*

no error, but does not work

*/

All of them as a Gist.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved by trial and error:

android {

    // let's assume these are return by a function which reads the filesystem
    def myFlavors = [
        flavor1: [
            packageName: "com.example.flavor1"
        ],
        flavor2: [
            packageName: "com.example.flavor2"
        ]
    ]

    productFlavors {
        myFlavors.each { name, config ->
            "$name" {
                packageName config.packageName
            }
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...