使用groovy代码动态创建build variants
通过使用groovy代码来动态创建build variants,可以在build.gradle中实现动态性。以下是一些示例代码,在文件中声明一个额外的变量库,然后使用它来创建不同的build variant:
ext {
    // Define a new variable for the library
    libraryVersion = [
        'foo': '1.0.0',
        'bar': '1.2.3'
    ]
}
android {
    ...
    // Use the libraryVersion variable to create different build variants
    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-debug'
        }
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        foo {
            // Use the libraryVersion variable to set the version of the library for this flavor
            versionName libraryVersion['foo']
            // ...
        }
        bar {
            // Use the libraryVersion variable to set the version of the library for this flavor
            versionName libraryVersion['bar']
            // ...
        }
    }
}
通过这种方式,就可以使用groovy代码在build.gradle中实现动态的build variants。