4

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.

4

3 回答 3

3

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

于 2018-03-21T10:00:03.700 回答
2

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
于 2018-03-21T10:10:37.287 回答
0

I was able to do it with product flavors, suppose I have three flavors (Stage, Prod, QA) . Then I can create builds with the following commands:-

  1. prod with releaseAPI23 (will create a build at PROJECT/android/app/build/outputs/apk/prod/releaseAPI23 )

./gradlew assembleProdReleaseAPI23.

  1. QA with releaseAPI23 (will create a build at PROJECT/android/app/build/outputs/apk/qa/releaseAPI23)

./gradlew assembleQaReleaseAPI23.

  1. Stage with releaseAPI23 (will create a build at PROJECT/android/app/build/outputs/apk/stage/releaseAPI23)

./gradlew assembleStageReleaseAPI23.

**You can try different variants as per your need

于 2020-06-22T21:24:15.457 回答