Skip to content

Instantly share code, notes, and snippets.

@Pasi-D
Last active May 28, 2021 16:46
Show Gist options
  • Save Pasi-D/f6aaa9abfddbb837f481289fda81b2c9 to your computer and use it in GitHub Desktop.
Save Pasi-D/f6aaa9abfddbb837f481289fda81b2c9 to your computer and use it in GitHub Desktop.
Android build gradle for react native with configurable versioning
apply plugin: "com.android.application"
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
....
android {
...
defaultConfig {
applicationId "com.rnversioner"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode generateVersionCode()
versionName generateVersionName()
}
...
}
/**
* Internal version number generator
* Refer https://developer.android.com/google/play/publishing/multiple-apks.html#using-a-version-code-scheme
*/
private Integer generateVersionCode() {
return rootProject.ext.minSdkVersion * 10000000 + project.env.get("MAJOR_VERSION").toInteger() * 10000 + project.env.get("MINOR_VERSION").toInteger() * 100 + project.env.get("PATCH_VERSION").toInteger()
}
/**
* Internal version name generator
*/
private String generateVersionName() {
String versionName = "${project.env.get("MAJOR_VERSION")}.${project.env.get("MINOR_VERSION")}.${project.env.get("PATCH_VERSION")}"
if(project.env.get("PRE_RELEASE") != null && !project.env.get("PRE_RELEASE").isEmpty()) {
versionName = versionName + "-" + project.env.get("PRE_RELEASE")
}
return versionName
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment