Created
October 31, 2023 12:31
-
-
Save megaacheyounes/d9952b8cbf981841bc7aa3c1d740adca to your computer and use it in GitHub Desktop.
sample build.gradle script with dynamic APK naming and versioning
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
plugins { | |
id 'com.android.application' | |
id 'org.jetbrains.kotlin.android' | |
id 'kotlin-kapt' | |
} | |
android { | |
compileSdk 34 | |
namespace "com.megaache.myapp" | |
def versionPropsFile = file('../version.properties') | |
def releaseValue = 0 | |
def debugValue = 0 | |
Properties versionProps = new Properties() | |
def runTasks = gradle.startParameter.taskNames | |
if ('assembleRelease' in runTasks) { | |
releaseValue = 1 | |
} | |
def mVersionName = "" | |
def mFileName = "" | |
//read and set version name and version code from 'version.properties' | |
if (versionPropsFile.canRead()) { | |
versionProps.load(new FileInputStream(versionPropsFile)) | |
versionProps['MAJOR'] = (versionProps['MAJOR'].toInteger()).toString() | |
versionProps['MINOR'] = (versionProps['MINOR'].toInteger()).toString() | |
versionProps['PATCH'] = (versionProps['PATCH'].toInteger() + releaseValue).toString() | |
versionProps['BUILD'] = (versionProps['BUILD'].toInteger() + 1).toString() | |
versionPropsFile.withWriter { versionProps.store(it, null) } | |
//ex: code: v2.0.1 | |
mVersionName = "v${versionProps['MAJOR']}.${versionProps['MINOR']}.${versionProps['PATCH']}" | |
//todo: update name | |
//ex: MyApp_v2.0.1.build-230 | |
mFileName = "MyApp_${mVersionName}.build-${versionProps['BUILD']}.apk" | |
defaultConfig { | |
applicationId "com.megaache.myapp" | |
minSdk 26 | |
targetSdk 33 | |
versionCode versionProps['MAJOR'].toInteger() * 1000000 + versionProps['MINOR'].toInteger() * 1000 + versionProps['PATCH'].toInteger() | |
versionName "${mVersionName}" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
vectorDrawables { | |
useSupportLibrary true | |
} | |
buildConfigField 'boolean', 'SHOW_DEBUG_VIEWS', 'false' | |
} | |
} else { | |
/** | |
* create version.properties file under same folder | |
* example content: | |
* | |
* #Tue Oct 31 12:37:51 GST 2023 | |
* BUILD=1 | |
* MAJOR=1 | |
* MINOR=1 | |
* PATCH=0 | |
* | |
*/ | |
throw new FileNotFoundException("version.properties not found") | |
} | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
allWarningsAsErrors = false | |
} | |
buildFeatures { | |
compose true | |
buildConfig true | |
} | |
composeOptions { | |
kotlinCompilerExtensionVersion "1.5.2" | |
} | |
packagingOptions { | |
resources { | |
excludes += ['META-INF/*.version', 'META-INF/gradle/incremental.annotation.processors', 'META-INF/proguard/androidx-annotations.pro', 'META-INF/DEPENDENCIES', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/license.txt', 'META-INF/NOTICE', 'META-INF/NOTICE.txt', 'META-INF/notice.txt', 'META-INF/ASL2.0'] | |
} | |
} | |
signingConfigs { | |
//todo: update | |
release { | |
storeFile file('keystore.jks') | |
storePassword '1234' | |
keyAlias 'key0' | |
keyPassword '1234' | |
} | |
} | |
buildTypes { | |
release { | |
signingConfig signingConfigs.release | |
minifyEnabled true | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
//rename generated APK | |
applicationVariants.all { variant -> | |
variant.outputs.all { output -> | |
if (output.outputFile != null) { | |
//releaseApkName = buildName + '_' + type + '_' + ver + '.apk' | |
outputFileName = mFileName | |
} | |
} | |
} | |
} | |
} | |
lint { | |
abortOnError false | |
checkReleaseBuilds false | |
} | |
kotlin.sourceSets.all { | |
languageSettings.optIn("kotlin.RequiresOptIn") | |
} | |
} | |
kapt { | |
correctErrorTypes true | |
} | |
dependencies { | |
//todo: add dependencies | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment