Skip to content

Commit 42816ce

Browse files
committed
Create files required for aapt on-device
Allows to add more values dynamically. xmlstarlet is removed, reducing app size.
1 parent 47f147c commit 42816ce

6 files changed

Lines changed: 92 additions & 27 deletions

File tree

app/src/main/assets/AndroidManifest.xml

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/src/main/assets/src/values/colors.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/src/main/assets/src/values/strings.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/src/main/assets/xmlstarlet

-1.28 MB
Binary file not shown.

app/src/main/assets/xmlstarlet64

-1.73 MB
Binary file not shown.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package app.akilesh.qacc.utils
2+
3+
import app.akilesh.qacc.BuildConfig
4+
import org.w3c.dom.Document
5+
import org.w3c.dom.Element
6+
import java.io.File
7+
import javax.xml.parsers.DocumentBuilder
8+
import javax.xml.parsers.DocumentBuilderFactory
9+
import javax.xml.parsers.ParserConfigurationException
10+
import javax.xml.transform.OutputKeys
11+
import javax.xml.transform.TransformerFactory
12+
import javax.xml.transform.dom.DOMSource
13+
import javax.xml.transform.stream.StreamResult
14+
15+
object XmlUtils {
16+
17+
private val transformerFactory: TransformerFactory by lazy { TransformerFactory.newInstance() }
18+
19+
private val documentBuilder: DocumentBuilder by lazy {
20+
try {
21+
DocumentBuilderFactory.newInstance().newDocumentBuilder()
22+
} catch (exception: ParserConfigurationException) {
23+
throw IllegalStateException("Unable to initiate")
24+
}
25+
}
26+
27+
fun createOverlayManifest(file: File, pkgName: String, accentName: String) {
28+
val document = documentBuilder.newDocument()
29+
30+
val rootElement = document.createElement("manifest")
31+
rootElement.setAttribute("xmlns:android",
32+
"http://schemas.android.com/apk/res/android")
33+
rootElement.setAttribute("package", pkgName)
34+
rootElement.setAttribute("android:versionName", BuildConfig.VERSION_NAME)
35+
rootElement.setAttribute("android:versionCode", BuildConfig.VERSION_CODE.toString())
36+
37+
val overlayElement = document.createElement("overlay")
38+
overlayElement.setAttribute("android:priority", "1")
39+
overlayElement.setAttribute("android:targetPackage", "android")
40+
overlayElement.setAttribute("android:category", "android.theme.customization.accent_color")
41+
rootElement.appendChild(overlayElement)
42+
43+
val applicationElement = document.createElement("application")
44+
applicationElement.setAttribute("android:hasCode", "false")
45+
applicationElement.setAttribute("android:allowBackup","false")
46+
applicationElement.setAttribute("android:extractNativeLibs","false")
47+
applicationElement.setAttribute("android:label", accentName)
48+
rootElement.appendChild(applicationElement)
49+
50+
document.appendChild(rootElement)
51+
52+
try {
53+
val transformer = transformerFactory.newTransformer()
54+
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8")
55+
transformer.setOutputProperty(OutputKeys.STANDALONE, "no")
56+
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
57+
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")
58+
val source = DOMSource(document)
59+
transformer.transform(source, StreamResult(file))
60+
}
61+
catch (e: Exception) {
62+
throw RuntimeException("Cannot build the result", e)
63+
}
64+
}
65+
66+
fun createColors(file: File, colorLight: String, colorDark: String) {
67+
val document = documentBuilder.newDocument()
68+
val resourcesElement = document.createElement("resources")
69+
addColor(document, resourcesElement, "accent_device_default_light", colorLight)
70+
addColor(document, resourcesElement, "accent_device_default_dark", colorDark)
71+
document.appendChild(resourcesElement)
72+
73+
try {
74+
val transformer = transformerFactory.newTransformer()
75+
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8")
76+
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
77+
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")
78+
val source = DOMSource(document)
79+
transformer.transform(source, StreamResult(file))
80+
}
81+
catch (e: Exception) {
82+
throw RuntimeException("Cannot build the result", e)
83+
}
84+
}
85+
86+
private fun addColor(document: Document, resourceElement: Element, name: String, value: String) {
87+
val element = document.createElement("color")
88+
element.setAttribute("name", name)
89+
element.appendChild(document.createTextNode(value))
90+
resourceElement.appendChild(element)
91+
}
92+
}

0 commit comments

Comments
 (0)