From ff119a61e410800fb9055aaa25924774d9dd3815 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 23 Aug 2023 01:26:53 +0100 Subject: [PATCH 1/2] chore: rename MainActivity in AndroidManifest.xml --- build-android/app/src/main/AndroidManifest.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-android/app/src/main/AndroidManifest.xml b/build-android/app/src/main/AndroidManifest.xml index 1ccae8c6..34e1a860 100644 --- a/build-android/app/src/main/AndroidManifest.xml +++ b/build-android/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:networkSecurityConfig="@xml/network_security_config"> - From c954ec4613e2589b1ad9519150f7db0111b9dfdb Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 23 Aug 2023 01:28:08 +0100 Subject: [PATCH 2/2] chore: create Gradle script to change applicationId --- build-android/build.gradle.kts | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/build-android/build.gradle.kts b/build-android/build.gradle.kts index 7948041a..8d334562 100644 --- a/build-android/build.gradle.kts +++ b/build-android/build.gradle.kts @@ -1,3 +1,8 @@ +import java.nio.file.Files +import java.nio.file.Paths +import kotlin.io.path.readText +import kotlin.io.path.writeText + // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id("com.android.application") version "8.1.0" apply false @@ -18,4 +23,51 @@ tasks { register("clean", Delete::class) { delete(rootProject.buildDir) } + + register("rename") { + val applicationId = project.properties["applicationId"] as String? + val DEFAULT_APP_ID = "com.google.firebase.codelab.friendlychat" + val DEFAULT_DIRECTORY = appIdToPath(DEFAULT_APP_ID) + + doFirst { + // Assert that an applicationId argument was passed + if (applicationId == null) { + throw IllegalArgumentException( + "Please specify an applicationId when running this command:\n" + + "./gradlew rename -PapplicationId=com.example.id" + ) + } + } + + doLast { + // Iterate through all files under DEFAULT_DIRECTORY + val defaultDirectoryFile = file(DEFAULT_DIRECTORY) + val absolutePath = Paths.get(defaultDirectoryFile.absolutePath) + Files.walk(absolutePath).forEach { path -> + val file = path.toFile() + if (file.isFile) { + // Replace the default applicationId with the new one + // For the package declaration and any relevant imports + path.writeText(path.readText() + .replace("(package|import)\\s+${DEFAULT_APP_ID}".toRegex(), "$1 $applicationId")) + } + + // Copy file to new directory + val newDirectory = file(appIdToPath(applicationId!!)) + val newFile = file("${newDirectory.absolutePath}/${file.relativeTo(defaultDirectoryFile)}") + file.copyTo(target = newFile) + // Delete original file + file.delete() + } + + // Update applicationId and namespace in build.gradle.kts + with (file("app/build.gradle.kts").toPath()) { + writeText( + readText().replace("(namespace|applicationId)\\s*=\\s*\"${DEFAULT_APP_ID}\"".toRegex(), + "$1 = \"$applicationId\"")) + } + } + } } + +fun appIdToPath(appId: String) = "app/src/main/java/${appId.replace(".", "/")}" \ No newline at end of file