Skip to content

Commit

Permalink
fix(core.gradle-plugin): 兼容更高版本cmdline-tools中decodeXml方法签名变化
Browse files Browse the repository at this point in the history
  • Loading branch information
aprz512 authored and shifujun committed Nov 23, 2024
1 parent c6c87b6 commit dc5bafb
Showing 1 changed file with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,12 @@ class ShadowPlugin : Plugin<Project> {
it.outputs.file(decodeXml).withPropertyName("decodeXml")

it.doLast {
val jarPath = File(project.locateApkanalyzerResultPath().readText())
val tempCL = URLClassLoader(arrayOf(jarPath.toURL()), contextClassLoader)
val binaryXmlParserClass =
tempCL.loadClass("com.android.tools.apk.analyzer.BinaryXmlParser")
val decodeXmlMethod = binaryXmlParserClass.getDeclaredMethod(
"decodeXml",
String::class.java,
ByteArray::class.java
)

val zipFile = ZipFile(processedResFile)
val binaryXml = zipFile.getInputStream(
zipFile.getEntry("AndroidManifest.xml")
).readBytes()

val outputXmlBytes = decodeXmlMethod.invoke(
null,
"AndroidManifest.xml",
binaryXml
) as ByteArray
val outputXmlBytes = decodeXml(project, binaryXml)
decodeXml.parentFile.mkdirs()
decodeXml.writeBytes(outputXmlBytes)
}
Expand Down Expand Up @@ -293,6 +279,50 @@ class ShadowPlugin : Plugin<Project> {
(javacTask as JavaCompile).source(project.fileTree(relativePath))
}

/**
* 反射apkanalyzer中的BinaryXmlParser类的decodeXml方法
*/
@Suppress("PrivateApi")
private fun decodeXml(project: Project, binaryXml: ByteArray): ByteArray {
val jarPath = File(project.locateApkanalyzerResultPath().readText())
val tempCL = URLClassLoader(arrayOf(jarPath.toURL()), contextClassLoader)
val binaryXmlParserClass =
tempCL.loadClass("com.android.tools.apk.analyzer.BinaryXmlParser")
return try {
decodeXmlMethodV1(binaryXmlParserClass, binaryXml)
} catch (ignored: Exception) {
decodeXmlMethodV2(binaryXmlParserClass, binaryXml)
}
}

private fun decodeXmlMethodV1(binaryXmlParserClass: Class<*>, binaryXml: ByteArray): ByteArray {
val decodeXmlMethod = binaryXmlParserClass.getDeclaredMethod(
"decodeXml",
String::class.java,
ByteArray::class.java
)
return decodeXmlMethod.invoke(
null,
"AndroidManifest.xml",
binaryXml
) as ByteArray
}

/**
* 新版本代码中删掉了一个String参数,这个参数原来只用于log输出了
* https://cs.android.com/android-studio/platform/tools/base/+/6a81855c2fa102ae4532ad9a645e40177770a26a:apkparser/analyzer/src/main/java/com/android/tools/apk/analyzer/BinaryXmlParser.java;dlc=598c38100e4fb2b001385faea994fcb54cc515b1
*/
private fun decodeXmlMethodV2(binaryXmlParserClass: Class<*>, binaryXml: ByteArray): ByteArray {
val decodeXmlMethod = binaryXmlParserClass.getDeclaredMethod(
"decodeXml",
ByteArray::class.java
)
return decodeXmlMethod.invoke(
null,
binaryXml
) as ByteArray
}

/**
* 检查插件是否修改了资源ID分区
*
Expand Down

0 comments on commit dc5bafb

Please sign in to comment.