Gradle - execute only single build type

2019-07-14 22:47发布

I have multiple build types defined in my build.gradle. In variant window I selected build variant (ex debugAPI23). I expected that code in only one build type will be executed. But in Gradle Console I can see output for all build types.

As you can see I am trying to remove specific file for each build type. But everytime all build types are executed. So in the end I am missing the file that should be present for my selected build type.

android {
    buildTypes {
        debug {
            println "build type debug"
            debuggable true
            signingConfig signingConfigs.debug

            sourceSets {
                main.java {
                    exclude '/cz/kctdata/kmf/Reader/KMFReaderCN51A60.java'
                }

                main.java.getIncludes().each { println "Added include: $it" }
                main.java.sourceFiles.each { println "File in source set: " + it }
            }
        }
        release {
            println "build type release"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            sourceSets {
                main.java {
                    exclude '/cz/kctdata/kmf/Reader/KMFReaderCN51A60.java'
                }

                main.java.getIncludes().each { println "Added include: $it" }
                main.java.sourceFiles.each { println "File in source set: " + it }
            }
        }
        debugAPI23 {
            println "build type debugAPI23"
            debuggable true
            signingConfig signingConfigs.debug

            sourceSets {
                main.java {
                    exclude '/cz/kctdata/kmf/Reader/KMFReaderCN51A42.java'
                }

                main.java.getIncludes().each { println "Added include: $it" }
                main.java.sourceFiles.each { println "File in source set: " + it }
            }
        }
        releaseAPI23 {
            println "build type releaseAPI23"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

            sourceSets {
                main.java {
                    exclude '/cz/kctdata/kmf/Reader/KMFReaderCN51A42.java'
                }

                main.java.getIncludes().each { println "Added include: $it" }
                main.java.sourceFiles.each { println "File in source set: " + it }
            }
        }
    }
}

I can not use build type specific folder because I have more build types and some files should be presented in multiple build types. I don't want to have multiple copies of same file in my project.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-14 23:11

You can build through the Terminal window in Android Studio, choosing by hand which flavor/build variant you want:

./gradlew assembleRelease

Or:

./gradlew assembleDebug

Or:

./gradlew assemble debugAPI23
查看更多
做个烂人
3楼-- · 2019-07-14 23:12

Last gradle android plugins have a new concept of "dimensions". https://developer.android.com/studio/build/build-variants.html

So you can try to use flavors and dimensions. A example:

android {
    flavorDimensions "dim1", "dim2"
}

productFlavors {
    flavor1 {
        dimension "dim1"
    }
    flavor2 {
        dimension "dim1"
    }
    flavor3 {
        dimension "dim1"
    }
    flavor4 {
        dimension "dim2"
    }
}

Here you will get a combination between build type + favor with dim1 + flavor with dim2, in other words files from flavor4 will be accessible in all flavors. For example in variant debugFlavor1Flavor4 you will have all resources which belongs to debug, flavor1 and flavor4

查看更多
登录 后发表回答