Skip to content

Commit 4f2ef3c

Browse files
Taknokkitadai31oSumAtrIX
authored
fix: Encode XML files as UTF-8 to fix compilation of resources (#339)
Co-authored-by: kitadai31 <[email protected]> Co-authored-by: oSumAtrIX <[email protected]>
1 parent 062ae14 commit 4f2ef3c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/main/kotlin/app/revanced/patcher/util/Document.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import org.w3c.dom.Document
44
import java.io.Closeable
55
import java.io.File
66
import java.io.InputStream
7+
import java.io.StringWriter
78
import javax.xml.parsers.DocumentBuilderFactory
9+
import javax.xml.transform.OutputKeys
810
import javax.xml.transform.TransformerFactory
911
import javax.xml.transform.dom.DOMSource
1012
import javax.xml.transform.stream.StreamResult
@@ -35,14 +37,19 @@ class Document internal constructor(
3537
}
3638

3739
it.outputStream().buffered().use { stream ->
38-
TransformerFactory.newInstance()
39-
.newTransformer()
40-
.transform(DOMSource(this), StreamResult(stream))
40+
val transformer = TransformerFactory.newInstance().newTransformer()
41+
// Set to UTF-16 but encode as UTF-8 to prevent surrogate pairs from being escaped to broken numeric character references.
42+
if (isAndroid) {
43+
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16")
44+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
45+
}
46+
transformer.transform(DOMSource(this), StreamResult(stream))
4147
}
4248
}
4349
}
4450

4551
private companion object {
4652
private val readerCount = mutableMapOf<File, Int>()
53+
private val isAndroid = System.getProperty("java.runtime.name").equals("Android Runtime")
4754
}
4855
}

0 commit comments

Comments
 (0)