Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@phatmann
Created December 20, 2019 13:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phatmann/21f1150c2cc8d6287e6524e43647e1fc to your computer and use it in GitHub Desktop.
Save phatmann/21f1150c2cc8d6287e6524e43647e1fc to your computer and use it in GitHub Desktop.
Using an environment variable instead of a flavor in Android build script to work around App Center limitation
apply plugin: "com.android.application"
apply plugin: 'io.fabric'
import com.android.build.OutputFile
def enableProguardInReleaseBuilds = true
def endpoint = System.env.NEXT_ANDROID_APP_ENDPOINT
android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
signingConfigs {
release {
keyAlias /* REDACTED */
keyPassword System.env.PLAYSTORE_PASSWORD
storeFile file(/* REDACTED */)
storePassword System.env.PLAYSTORE_PASSWORD
}
}
defaultConfig {
applicationId /* REDACTED */
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
multiDexEnabled true
versionCode project.hasProperty('versionCode') ? project.property('versionCode') as int : getVersionCodeTimestamp()
versionName /* REDACTED */
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
release {
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
if (endpoint) {
flavorDimensions "appType"
} else {
flavorDimensions "appType", "endpoint"
}
productFlavors {
app1 {
dimension "appType"
switch (endpoint) {
case null:
case "production":
applicationIdSuffix ".app1"
break
case "stage":
applicationIdSuffix ".app1.staging"
break
case "developer":
applicationIdSuffix ".app1.developer"
break
}
/* Config here */
}
app2 {
dimension "appType"
switch (endpoint) {
case null:
case "production":
applicationIdSuffix ".app2"
break
case "stage":
applicationIdSuffix ".app2.staging"
break
case "developer":
applicationIdSuffix ".app2.developer"
break
}
/* Config here */
}
if (!endpoint) {
production {
dimension "endpoint"
}
stage {
dimension "endpoint"
applicationIdSuffix ".staging"
}
developer {
dimension "endpoint"
applicationIdSuffix ".developer"
}
}
}
sourceSets {
if (endpoint) {
app1Debug {
setRoot("src/app1${endpoint.capitalize()}Debug")
}
app1Release {
setRoot("src/app1${endpoint.capitalize()}Release")
}
app2Debug {
setRoot("src/app2${endpoint.capitalize()}Debug")
}
app2Release {
setRoot("src/app2${endpoint.capitalize()}Release")
}
}
}
buildTypes.each {
// Config here
}
variantFilter { variant ->
switch (variant.getName()) {
case "app1ProductionRelease":
case "app1StageDebug":
case "app1StageRelease":
case "app1DeveloperDebug":
case "app2ProductionRelease":
case "app2StageDebug":
case "app2StageRelease":
case "app2DeveloperDebug":
setIgnore(endpoint != null)
break
case "app1Debug":
case "app1Release":
case "app2Debug":
case "app2Release":
setIgnore(endpoint == null)
break
default:
setIgnore(true)
}
}
applicationVariants.all { variant ->
switch (variant.getName()) {
case "app1Debug":
case "app1Release":
if (endpoint) {
configureApp1Endpoint(variant, endpoint)
configureGoogleServices(variant, endpoint)
}
break
case "app1ProductionRelease":
configureApp1ProductionEndpoint(variant)
break
case "app1StageDebug":
case "app1StageRelease":
configureApp1StageEndpoint(variant)
break
case "app1DeveloperDebug":
configureApp1DeveloperEndpoint(variant)
break
case "app2Debug":
case "app2Release":
if (endpoint) {
configureApp1Endpoint(variant, endpoint)
configureGoogleServices(variant, endpoint)
}
break
case "app2ProductionRelease":
configureApp2ProductionEndpoint(variant)
break
case "app2StageDebug":
case "app2StageRelease":
configureApp2StageEndpoint(variant)
break
case "app2DeveloperDebug":
configureApp2DeveloperEndpoint(variant)
break
}
}
}
dependencies {
// Dependencies here
}
def configureProductionEndpoint(variant) {
// Config here
}
def configureStageEndpoint(variant) {
// Config here
}
def configureDeveloperEndpoint(variant) {
// Config here
}
def configureApp1ProductionEndpoint(variant) {
configureProductionEndpoint(variant)
// Config here
}
def configureApp2ProductionEndpoint(variant) {
configureProductionEndpoint(variant)
// Config here
}
def configureApp1StageEndpoint(variant) {
configureStageEndpoint(variant)
// Config here
}
def configureApp2StageEndpoint(variant) {
configureStageEndpoint(variant)
// Config here
}
def configureApp1DeveloperEndpoint(variant) {
configureDeveloperEndpoint(variant)
// Config here
}
def configureApp2DeveloperEndpoint(variant) {
configureDeveloperEndpoint(variant)
// Config here
}
def configureApp1Endpoint(variant, endpoint) {
switch (endpoint) {
case "production":
configureApp1ProductionEndpoint(variant)
break
case "stage":
configureApp1StageEndpoint(variant)
break
case "developer":
configureApp1DeveloperEndpoint(variant)
break
}
}
def configureApp2Endpoint(variant, endpoint) {
switch (endpoint) {
case "production":
configureApp2ProductionEndpoint(variant)
break
case "stage":
configureApp2StageEndpoint(variant)
break
case "developer":
configureApp2DeveloperEndpoint(variant)
break
}
}
def configureGoogleServices(variant, endpoint) {
task("copyGoogleServices${variant.name.capitalize()}", type: Copy) {
from "src/${variant.productFlavors[0].name}${endpoint.capitalize()}${variant.buildType.name.capitalize()}"
include "google-services.json"
into "."
}
}
afterEvaluate {
if (endpoint) {
processApp1DebugGoogleServices.dependsOn copyGoogleServicesApp1Debug
processApp1ReleaseGoogleServices.dependsOn copyGoogleServicesApp1Release
processApp2DebugGoogleServices.dependsOn copyGoogleServicesApp2Debug
processApp2ReleaseGoogleServices.dependsOn copyGoogleServicesApp2Release
}
}
apply plugin: 'com.google.gms.google-services'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment