If the versionName
is hard-coded in your app/build.gradle
file, then you can extract the string using bash/shell commands like awk
and perl
, which are available on Linux Docker machines:
1. app/build.gradle contains a hard-coded versionName:
android {
defaultConfig {
...
versionName "2.1.0"
...
}
}
In the yml file, extract the versionName
string from app/build.gradle
using awk
or perl
:
script:
- export APP_VERSION_NAME=$(awk '/versionName .*"/{gsub(/\"/,"",$2);print $2}' "app/build.gradle")
- echo "APP_VERSION_NAME = $APP_VERSION_NAME" # This prints: 2.1.0
- export APP_VERSION_NAME2=$(perl -nle 'print $& while m{(?<=versionName ").*?(?=")}g' "app/build.gradle")
- echo "APP_VERSION_NAME2 = $APP_VERSION_NAME2" # This prints: 2.1.0
If the versionName
is auto-generated using an environment variable or other string templates, then you must build the code first, and then extract the string from the automatically-generated BuildConfig.java
file:
2. app/build.gradle contains string templates:
project.ext.my_lib_version = "2.1.0"
android {
defaultConfig {
...
versionName "${project.ext.my_lib_version}-debug"
...
}
}
After building the code, the auto-generated BuildConfig.java
file will contain the real string:
public final class BuildConfig {
...
public static final String VERSION_NAME = "2.1.0-debug";
...
}
In the yml file, extract the versionName
string from BuildConfig.java
using awk
or perl
:
script:
# Build the Android app
- ./gradlew assembleDebug
# There may be multiple 'BuildConfig.java' files. Get the path of the
# the first 'BuildConfig.java' file which is closest to the root folder
- BUILD_CONFIG_FILE_PATH=$(find "$(pwd -P)" -name BuildConfig.java | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2- | head -1)
- echo "BUILD_CONFIG_FILE_PATH = $BUILD_CONFIG_FILE_PATH"
- export APP_VERSION_NAME=$(awk '/VERSION_NAME/{gsub(/\"/,"",$7) ; gsub(/;/,"",$7) ; print $7}' $BUILD_CONFIG_FILE_PATH)
- echo "APP_VERSION_NAME = $APP_VERSION_NAME" # This prints: 2.1.0-debug
- export APP_VERSION_NAME2=$(perl -nle 'print $& while m{(?<=VERSION_NAME = ").*?(?=")}g' $BUILD_CONFIG_FILE_PATH)
- echo "APP_VERSION_NAME2 = $APP_VERSION_NAME2" # This prints: 2.1.0-debug
3. More info and references about these commands and how they work: