Last active
January 23, 2025 15:25
-
-
Save Wikinaut/d3eb229e1dd1ec703f00 to your computer and use it in GitHub Desktop.
Generate fully-flavoured apk filename with app_name, versionName, versionCode, git hash
This file contains hidden or 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: 'android-sdk-manager' | |
apply plugin: 'com.android.application' | |
apply plugin: 'com.jakewharton.hugo' | |
apply from: '../config/quality.gradle' | |
import com.android.ddmlib.DdmPreferences | |
import java.util.concurrent.TimeUnit | |
// Copy the signing.properties.sample file to ~/.sign/signing.properties and adjust the values. | |
final File PROD_PROPS_FILE = new File(System.getProperty('user.home'), '.sign/signing.properties') | |
final File REPO_PROPS_FILE = new File('repo.properties') | |
final Properties PROD_PROPS = loadProperties(PROD_PROPS_FILE) | |
final Properties REPO_PROPS = loadProperties(REPO_PROPS_FILE) | |
final int ADB_TIMEOUT = TimeUnit.MINUTES.toMillis(5) | |
final boolean continuousIntegrationBuild = System.getenv('JENKINS_HOME') != null | |
final boolean preDexEnabled = hasProperty('pre.dex') ? | |
Boolean.valueOf(getProperty('pre.dex').toString()) : | |
!continuousIntegrationBuild | |
if (!preDexEnabled) { | |
println 'Pre-dexing disabled.' | |
} | |
if (continuousIntegrationBuild) { | |
DdmPreferences.setTimeOut(ADB_TIMEOUT) | |
println "Device timeout is ${DdmPreferences.getTimeOut()}ms" | |
} | |
def getDate() { | |
def date = new Date() | |
def formattedDate = date.format('yyyy-MM-dd') | |
return formattedDate | |
} | |
def computeVersionName(label) { | |
return "2.1.${android.defaultConfig.versionCode}-${label}-${date}" | |
} | |
final JavaVersion JAVA_VERSION = JavaVersion.VERSION_1_7 | |
if (JavaVersion.current() != JAVA_VERSION) { | |
throw new IllegalStateException("Incorrect Java Development Kit version; expected " | |
+ JAVA_VERSION + " but found " + JavaVersion.current()) | |
} | |
android { | |
compileSdkVersion 23 | |
buildToolsVersion '23.0.1' | |
compileOptions { | |
sourceCompatibility = JAVA_VERSION | |
targetCompatibility = JAVA_VERSION | |
} | |
dexOptions { | |
preDexLibraries = preDexEnabled | |
} | |
defaultConfig { | |
applicationId 'org.wikipedia' | |
minSdkVersion 15 | |
targetSdkVersion 22 | |
versionCode 132 | |
testApplicationId 'org.wikipedia.test' | |
// Define your application name here. | |
// It must neither be present in /res/values/strings.xml | |
// nor in /res/values/string_no_translate.xml | |
resValue 'string', 'app_name', 'Wikipedia' | |
} | |
sourceSets { | |
test { | |
java.srcDirs += 'src/testlib/java' | |
} | |
androidTest { | |
java.srcDirs += 'src/testlib/java' | |
} | |
} | |
signingConfigs { | |
prod { | |
setSigningConfigKey(prod, PROD_PROPS) | |
} | |
debug { | |
setSigningConfigKey(debug, REPO_PROPS) | |
} | |
} | |
buildTypes { | |
release { | |
minifyEnabled true | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
productFlavors { | |
dev { | |
versionName computeVersionName("dev") | |
applicationId 'org.wikipedia.dev' | |
signingConfig signingConfigs.debug | |
} | |
prod { | |
versionName computeVersionName("r") | |
signingConfig signingConfigs.prod | |
} | |
releasesprod { | |
versionName computeVersionName("releasesprod") | |
signingConfig signingConfigs.prod | |
} | |
alpha { | |
versionName computeVersionName("alpha") | |
applicationId 'org.wikipedia.alpha' | |
signingConfig signingConfigs.debug | |
} | |
beta { | |
versionName computeVersionName("beta") | |
applicationId 'org.wikipedia.beta' | |
signingConfig signingConfigs.prod | |
} | |
amazon { | |
versionName computeVersionName("amazon") | |
signingConfig signingConfigs.prod | |
} | |
custom { | |
versionName computeVersionName(customChannel) | |
applicationId getProperty('customApplicationId') | |
// next line is for injecting a custom channel value into the custom/AndroidManifest.xml | |
manifestPlaceholders = [customChannel:getProperty('customChannel').toString()] | |
signingConfig signingConfigs.prod | |
} | |
} | |
// while we still have lint errors; remove once those are fixed | |
lintOptions { | |
disable 'MissingTranslation' | |
warning 'NewApi' // until https://code.google.com/p/android/issues/detail?id=137195 is released | |
} | |
// https://stackoverflow.com/questions/24649240/build-release-apk-with-customize-name-format-in-android-studio | |
// https://stackoverflow.com/questions/32092665/resolve-application-label-for-each-build-type/32220436#32220436 | |
// https://stackoverflow.com/questions/18332474/how-to-set-versionname-in-apk-filename-using-gradle | |
android.applicationVariants.all { variant -> | |
variant.outputs.each { output -> | |
// get app_name field from defaultConfig | |
def appName = variant.mergedFlavor.resValues.get('app_name').getValue() | |
// concat new App name with each flavor's name | |
appName = "${appName}" | |
variant.productFlavors.each { flavor -> | |
appName += "-${flavor.name}" | |
} | |
// optionally add buildType name | |
appName += "-${variant.buildType.name}" | |
// your requirement: if buildType == debug, add DEV to App name | |
if (variant.buildType.name == "debug") { | |
appName += "-DEV" | |
} | |
// if you want, you can set a new resValue | |
// variant.resValue 'string', 'app_name', appName | |
/* | |
* Gets the version name from the latest Git tag | |
*/ | |
def gitVersion = { -> | |
def stdout = new ByteArrayOutputStream() | |
exec { | |
commandLine 'git', 'rev-parse', '--short' ,'HEAD' | |
standardOutput = stdout | |
} | |
return stdout.toString().trim() | |
} | |
def manifestParser = new com.android.builder.core.DefaultManifestParser() | |
def manifestVersionName = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) | |
def finalName = appName + "-" | |
if (!manifestVersionName.isEmpty()) { | |
finalName += manifestVersionName + "." + variant.versionCode + "-" + gitVersion() + ".apk" | |
} | |
finalName += variant.versionCode + "-" + gitVersion() + ".apk" | |
output.outputFile = new File(output.outputFile.parent, finalName) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://gerrit.wikimedia.org/r/#/c/248699/