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.versionCode but it doesn't recognise android...
apply plugin: 'com.android.model.application'
apply from: "../config.gradle"
apply from: "../gitversion.gradle"
def config = ext.configuration
model {
android {
compileSdkVersion = config.compileSdkVersion
buildToolsVersion = config.buildToolsVersion
defaultConfig {
applicationId = 'eu.tss.apitest'
minSdkVersion.apiLevel = config.minimumSdkVersion
targetSdkVersion.apiLevel = config.targetSdkVersion
defaultConfig {
versionCode = 1
versionName = '1.0'
ext.printVersions(versionCode,versionName)
}
buildTypes {
release {
minifyEnabled false
proguardFiles.add(file('proguard-android.txt'))
}
}
}
android.lintOptions {
abortOnError false
}
}
dependencies {
println rootProject.getName()
compile 'com.android.support:appcompat-v7:25.0.0'
compile project(':tssapiandroid')
compile project(path: ':tssuielements')
}
task printVersions{
//I would like to access versionCode here:)
}
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With