Last active
June 28, 2022 20:44
-
-
Save chantellosejo/758f59bf7b88a88346e3 to your computer and use it in GitHub Desktop.
Gradle Build file that creates different "apps" based on the build type, letting you load apps side-by-side (beta vs. production)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
def final yourApplicationId = 'com.yourId.android' | |
android { | |
compileSdkVersion 21 | |
buildToolsVersion "21.1.2" | |
sourceSets { | |
androidTest { | |
java.srcDirs = ['test/java'] | |
} | |
} | |
defaultConfig { | |
applicationId = yourApplicationId | |
minSdkVersion 15 | |
targetSdkVersion 21 | |
// Version name should actually be numbers representing "Major, Minor, and Bugfix" release numbers | |
versionName "Major.Minor.Bugfix" | |
// Version code should be your own code, whatever you want it to be (Jenkins builds could be a cool option here) | |
versionCode 100 | |
} | |
signingConfigs { | |
google { | |
storeFile file("/some/path/to/your/keystore") | |
storePassword "yourStorePassword" | |
keyAlias "yourKeyAlias" | |
keyPassword "yourKeyPassword" | |
} | |
} | |
buildTypes { | |
debug { | |
versionNameSuffix ".${generateRevisionVersionSuffix()}.date=${getDate()}.debug" | |
applicationIdSuffix "." + getBranch().replaceAll("/", ".") | |
buildConfigField "String", "URL_AUTHORITY", "\"www.debugserver.com\"" | |
debuggable true | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-android.txt' | |
} | |
release { | |
buildConfigField "String", "URL_AUTHORITY", "\"www.releaseserver.com\"" | |
debuggable false | |
minifyEnabled true | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-android.txt' | |
signingConfig signingConfigs.google | |
} | |
} | |
productFlavors { | |
google { | |
applicationId = yourApplicationId + ".google" | |
buildConfigField "String", "ACCOUNT_TYPE", "\"${applicationId}.account\"" | |
buildConfigField "String", "AUTHORITY", "\"${applicationId}.provider\"" | |
resValue "string", "account_type", "${applicationId}.account" | |
resValue "string", "authority", "${applicationId}.provider" | |
// Insert buildConfigFields below...great for market-dependent constants! | |
// Like below! | |
buildConfigField 'boolean', 'IS_GOOGLE', 'true' | |
buildConfigField 'boolean', 'IS_KINDLE', 'false' | |
// TODO Insert your own custom variables | |
} | |
amazon { | |
applicationId = yourApplicationId + ".amazon" | |
buildConfigField "String", "ACCOUNT_TYPE", "\"${applicationId}.account\"" | |
buildConfigField "String", "AUTHORITY", "\"${applicationId}.provider\"" | |
resValue "string", "account_type", "${applicationId}.account" | |
resValue "string", "authority", "${applicationId}.provider" | |
// Again, these are optional! | |
buildConfigField 'boolean', 'IS_GOOGLE', 'false' | |
buildConfigField 'boolean', 'IS_KINDLE', 'true' | |
// TODO Insert your own custom variables | |
} | |
} | |
lintOptions { | |
checkReleaseBuilds false | |
abortOnError false | |
} | |
} | |
dependencies { | |
// Lots of dependencies follow! Here's a few examples | |
compile 'com.android.support:support-v4:21.0.3' | |
compile 'com.android.support:appcompat-v7:21.0.3' | |
compile 'com.google.code.gson:gson:2.3' | |
// Supposedly the stuff below can be written as | |
// compile fileTree(dir: 'libs', include: '*.jar') | |
// But my build hates me | |
compile files('libs/ALibIDownloaded.jar') | |
compile files('libs/SomeOtherLibIDownloaded.jar') | |
// If you have other "modules" added to your project, they should be | |
// added as a dependency like below | |
compile project(':moduleName') | |
// TODO INSERT YOUR DEPENDENCIES BELOW | |
} | |
def getDate() { | |
def date = new Date() | |
def formattedDate = date.format('yyyyMMddHHmmss') | |
return formattedDate | |
} | |
def getRevId() { | |
if (System.getProperty("BUILD_TAG")) | |
return System.getProperty("BUILD_TAG") | |
if (System.getProperty("ghprbActualCommit")) | |
return System.getProperty("ghprbActualCommit") | |
try { | |
def proc = """git rev-parse HEAD""".execute() | |
proc.waitFor() | |
proc.in.text.trim() | |
} catch (e) { | |
"" | |
} | |
} | |
def getBranch() { | |
if (System.getProperty("sha1")) | |
return System.getProperty("sha1") | |
def proc = """git rev-parse --abbrev-ref HEAD""".execute() | |
proc.waitFor() | |
proc.in.text.trim() | |
} | |
def getDirtyFlag() { | |
def proc = """git status --porcelain""".execute() | |
proc.waitFor() | |
if (proc.in.text.trim() && !System.getenv()['JOB_NAME']) | |
return "dirty" | |
else | |
return "" | |
} | |
def generateRevisionVersionSuffix() { | |
return getRevId() ? "commit=${getRevId().substring(0, 7)}.branch=${getBranch()}(${getDirtyFlag()})" : "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment