How to access versionCode from task in Gradle Experimental Plugin

前端 未结 2 1848
心在旅途
心在旅途 2021-01-06 01:26

I would like to access versionCode and versionName in defaultConfig from a gradle task. I am using the experimental gradle plugin. I tried model.android.defaultConfig.versio

相关标签:
2条回答
  • 2021-01-06 01:43

    Create method in your build.gradle:

    def getVersionCode = { ->
        // calculate versionCode code or use hardcoded value
        return 1
    }
    

    Use this method in defaultConfig:

    android {
        defaultConfig {
            versionCode getVersionCode()
        }
    }
    

    Use the same method in your custom task:

    task printVersions{
        print "Version code is " + getVersionCode()
    }
    

    Alternative solution is to access versionCode property directly:

    task printVersions{
        print "Version code is $project.android.defaultConfig.versionCode"
    }
    
    0 讨论(0)
  • 2021-01-06 01:51

    Create method in your build.gradle:

      def getVersionCode = { ->
          return 1
      }
    

    Use this method in defaultConfig:

    android {
      defaultConfig {
        versionCode getVersionCode()
      }
    }
    

    versionCode property can get like:

    task printVersions{
      print "Version code is ${project.android.defaultConfig.versionCode}"
    }
    
    0 讨论(0)
提交回复
热议问题