Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for App Check #264

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build-android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config">
<activity android:name="com.google.firebase.codelab.friendlychat.MainActivity"
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
52 changes: 52 additions & 0 deletions build-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(".", "/")}"