Skip to content

Instantly share code, notes, and snippets.

@DanteAndroid
Last active August 9, 2023 03:44
Show Gist options
  • Save DanteAndroid/15c08e0e47714238c2e6340cb0f0beff to your computer and use it in GitHub Desktop.
Save DanteAndroid/15c08e0e47714238c2e6340cb0f0beff to your computer and use it in GitHub Desktop.
Add a task which changes your file under project directory, and it could be run first when running.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.myapplication'
compileSdk 33
defaultConfig {
applicationId "com.example.myapplication"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
android.applicationVariants.matching { variant ->
println("matching ${variant.name}")
variant.name == "debug"
}
android.applicationVariants.configureEach { variant ->
tasks.matching { task ->
task.name.equalsIgnoreCase("pre${variant.name}Build")
}.configureEach { task ->
def file = project.file("file_under_app_dir.json")
String content = file.text
if (variant.name == "debug") {
content = content.replaceAll("Release_data", "Debug_data")
} else {
content = content.replaceAll("Debug_data", "Release_data")
}
file.write(content)
println("doLast replaceTask success")
}
if (variant.buildType.name == "release") {
variant.outputs.configureEach { output ->
outputFileName = "app-${versionName}-${versionCode}-${variant.name}.apk"
}
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
import org.gradle.configurationcache.extensions.capitalized
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.myapplication"
compileSdk = 33
defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
android.applicationVariants.configureEach {
val taskName = "replaceTask${buildType.name}"
tasks.register<ReplaceTask>(taskName) {
buildVariant.set(buildType.name)
}
tasks.matching {
it.name.equals("pre${buildType.name}Build",true)
}.configureEach {
dependsOn(taskName)
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
abstract class ReplaceTask : DefaultTask() {
@get:Input
abstract val buildVariant: Property<String>
init {
logger.log(LogLevel.INFO, "ReplaceTask init")
}
@TaskAction
fun greet() {
val file = project.file("file_under_app_dir.json")
var content = file.readText()
if (buildVariant.get() == "debug") {
content = content.replace("Release_data", "Debug_data")
} else {
content = content.replace("Debug_data", "Release_data")
}
file.writeText(content)
println("ReplaceTask success")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment