I tested a lot of cases with gradle and introduced external build.number variables. In my case, I use docker as the build environment, so the environment provided by the command will not be successfully implemented.
There are two options for entering variables in gradle. Note that the differences are below -D and -P below.
// 1. gradle build -Pbuild.number=555 aaa = rootProject.properties.get("build.number") // 2. gradle bulid -Dbuild.number=444 bbb = System.getProperty("build.number")
gradle --help says [option] task as the order of the commands, but task [option] works.
An example build.gradle script is given below:
ext { buildNumber = (rootProject.properties.get("build.number") as Integer) ?: 1 } android { defaultConfig { versionCode project.buildNumber versionName "1.3.7." + project.buildNumber.toString() }
In my case, I want to use build.number as android versionCode, so Integer parsing is required with :? zero treatment.
source share