Picking a specific build type in a dependency

后端 未结 5 2412
闹比i
闹比i 2021-02-20 07:14

Suppose I have an Android app with three build types:

buildTypes {
    release {
        ....
    }
    optRelease {
        ....
    }
    debug {
        ....
         


        
相关标签:
5条回答
  • 2021-02-20 07:28

    Starting from gradle 3.0.0 you can use following for default project variants (release, debug, test etc)

    • implementation
    • releaseImplementation
    • testImplementation
    • debugImplementation
    • androidTestImplementation

    Simple example would be:

    android {
       ....
    }
    dependencies {
        implementation "com.google.dagger:dagger-android-support:$DAGGER_VERSION"
        androidTestImplementation jUnit
        testImplementation mockito
    }
    

    If you have multiple flavors of your app, starting from 3.0 you have to declare flavor dimensions first, define fallback and matching strategy in your default config (for remote dependencies, and dependencies without dimensions) and gradle will recognize what dependencies should be included for that flavor. For more info check this migration guide.

    0 讨论(0)
  • 2021-02-20 07:30

    What I did is :

    Created product flavors:

        android {
            flavorDimensions 'tier'
            compileSdkVersion 27
            defaultConfig {
                applicationId "com.example.sample"
                minSdkVersion 15
                targetSdkVersion 27
            }
    
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                    --------------
                }
                debug {
                    debuggable true
                }
            }
            productFlavors {
                qc {
                    applicationIdSuffix ".qc"
                    dimension "tier"
    
                }
                production {
                    applicationIdSuffix ".production"
                    dimension "tier"
                }
            }
        }
    

    Allow dependencies to choose the build types like below

    dependencies { 
        qcDebugCompile project(path: ':libName', configuration: "debug") 
        qcReleaseCompile project(path: ':libName', configuration: "release") 
        productionDebugCompile project(path: ':libName', configuration: "debug") 
        productionReleaseCompile project(path: ':libName', configuration: "release") 
    ... 
    } 
    

    0 讨论(0)
  • 2021-02-20 07:32

    Thanks a lot ahasbini for your solution. Copying ahasbini's answer from this thread for easy reference to the solution.

    You'll need to specify matchingFallback with the Android Gradle Plugin 3.0.0 for the plugin to know which fallback build type of library to use when being compiled with app code in case a certain build type defined in your app is not found in the library.

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix '.debug'
        }
        staging {
            initWith release
            applicationIdSuffix '.staging'
            matchingFallbacks = ['release']
        }
    }
    

    More info here: Migrate to Android Plugin for Gradle 3.0.0.

    0 讨论(0)
  • 2021-02-20 07:36

    You can use matchingFallback

    buildTypes {
        release {
           matchingFallbacks = ['debug']
           ... 
        }
        optRelease {
           matchingFallbacks = ['release']  
           ...
        }
        debug {
           matchingFallbacks = ['debug']  
           ...
        }
    }
    

    You can also specify list of fallback as follows:

    optRelease {
        matchingFallbacks = ['release','debug']  
    }
    

    This will specify a sorted list of fallback build types that the plugin should try to use when a dependency does not include a "optRelease" build type. You may specify as many fallbacks as you like, and the plugin selects the first build type that's available in the dependency.

    You can refer to official document for more details.

    However if you want to use variant-specific configurations when targeting external dependencies. You can do it as follows:

    debugImplementation project(':myDependency')

    This will make myDependency as a dependency to only the "debug" version of your module.

    0 讨论(0)
  • 2021-02-20 07:52

    If you want to add a dependency for a variant that combines a product flavor and a build type, then you must initialize the configuration name in the configurations block.(如果想为自定义的buildTypes来添加dependencies,需要配置configurations {})

    android {
        buildTypes {
            release {
                ....
            }
            optRelease {
                ....
            }
            debug {
                ....
            }
        }
    }
    
    configurations {
        // Initializes a placeholder for the optReleaseImplementation dependency configuration
        optReleaseImplementation {}
    }
    
    dependencies {
            debugImplementation xxx
            // Then it can work
            optReleaseImplementation xxx
            releaseImplementation xxx
        }
    
    0 讨论(0)
提交回复
热议问题