Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active April 4, 2025 06:08
Show Gist options
  • Save SeongUgJung/3405bed6adf923188e6055224adcc630 to your computer and use it in GitHub Desktop.
Save SeongUgJung/3405bed6adf923188e6055224adcc630 to your computer and use it in GitHub Desktop.
distractionOptimized for android auto
// in app/build.gradle.kts
// to configure ProcessApplicationManifest task
tasks.withType<com.android.build.gradle.tasks.ProcessApplicationManifest>().configureEach {
doLast {
val manifestFile = mergedManifest.get().asFile
if (manifestFile.exists()) {
// Read the content
val document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(manifestFile)
// Add <meta-data> to <activity>
val activityNodes = document.getElementsByTagName("activity")
//noinspection WrongGradleMethod
activityNodes.forEach { activityNode ->
val metaDataElement = document.createElement("meta-data")
metaDataElement.setAttribute("android:name", "distractionOptimized")
metaDataElement.setAttribute("android:value", "true")
activityNode.appendChild(metaDataElement)
}
// Save the updated content back to the file
val transformer = TransformerFactory.newInstance().newTransformer()
val source = DOMSource(document)
val result = StreamResult(manifestFile)
transformer.transform(source, result)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment