Use different VersionCode for Debug/Release android gradle build

前端 未结 6 1657
感动是毒
感动是毒 2020-12-15 07:30

I\'d like to apply different VersionCode to make apk file. For debug only fix it to 1, and for release whatever number specified in defaultConf

相关标签:
6条回答
  • 2020-12-15 07:45

    Here's an updated version:

    android {
      defaultConfig { ... }
    
      applicationVariants.all { variant ->
        if (variant.name == 'debug') {
          variant.outputs.each { output ->
            output.versionCodeOverride = 1
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-15 07:45

    Me too, but I think defaultConfig.versionCode was set when build.gradle be compiling. It's global static variable, and assigned at compiletime, not runtime.

    I think we can intercept gradle task execution, and modify defaultConfig.versionCode at runtime.


    After goooooooogle, I found this one works for me: https://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2

    0 讨论(0)
  • 2020-12-15 07:53

    To use with Flavors:

    applicationVariants.all { variant ->
        def flavor = variant.mergedFlavor
        def name = flavor.getVersionName()
        def code = flavor.getVersionCode()
    
        if (variant.buildType.isDebuggable()) {
            name += '-d'
            code = 1
        }
    
        variant.outputs.each { output ->
            output.versionNameOverride = name
            output.versionCodeOverride = code
        }
    }
    
    0 讨论(0)
  • 2020-12-15 07:54
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (variant.buildType.isDebuggable()) {
                output.versionCodeOverride = 26
                output.versionNameOverride = "2.2.6"
            }
        }
    }
    

    put it in android{}

    0 讨论(0)
  • 2020-12-15 07:55

    Late on the party...

    The entire gradle file evaluated before any task execution, so you are basically changing the default versionCode while declaring debug configs. There is no direct way to reset versionCode from buildType, but the link on the other answer do the trick by declaring a task on build variants.

    android {
        ...
        defaultConfig {
             ...
        }
        buildTypes {
             ...
        }
        applicationVariants.all { variant ->
            def flavor = variant.mergedFlavor
            def versionCode = flavor.versionCode
            if (variant.buildType.isDebuggable()) {
                versionCode += 1
            }
            flavor.versionCode = versionCode
        }
    }
    
    0 讨论(0)
  • 2020-12-15 08:01

    So recently I had to deal with the same scenario and all the examples I could find use the applicationVariants property which is ill-documented imo.

    So after some digging through the source code a bit, I realized that in the end versionCode and versionName properties from ProductFlavor get merged into the AndroidManifest which got me thinking: couldn't we just inject them by ourselves, cause we have manifestPlaceholders property on ProductFlavor AND on BuildType DSL objects, so I came up with this -- don't hesitate to give feedback and tell me why it's wrong

    In build.gradle(app)

    android {
        ...
        buildTypes {
            debug {
                manifestPlaceholder = [versionCode: X, versionName: "X.Y.Z"]
            }
            release {
                manifestPlaceholder = [versionCode: A, versionName: "A.B.C"]
            }
        }
        ...
    }
    

    In AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="..."
        android:versionCode="${versionCode}"
        android:versionName="${versionName}">
        ...
    </manifest>
    
    0 讨论(0)
提交回复
热议问题