fix:使用D8处理jar,解决部分jar无法加载的问题

This commit is contained in:
Lin
2025-12-25 18:07:01 +08:00
parent 8519e816a3
commit d992e3fa2b
2 changed files with 50 additions and 1 deletions

View File

@@ -241,6 +241,8 @@ dependencies /* Unclassified */ {
// ICU4J // ICU4J
implementation("com.ibm.icu:icu4j:77.1") implementation("com.ibm.icu:icu4j:77.1")
implementation("com.android.tools:r8:8.13.17")
} }
dependencies /* MIME */ { dependencies /* MIME */ {

View File

@@ -3,6 +3,10 @@ package org.autojs.autojs.rhino
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import com.android.dx.command.dexer.Main import com.android.dx.command.dexer.Main
import com.android.tools.r8.CompilationMode
import com.android.tools.r8.D8
import com.android.tools.r8.D8Command
import com.android.tools.r8.OutputMode
import dalvik.system.DexClassLoader import dalvik.system.DexClassLoader
import net.lingala.zip4j.ZipFile import net.lingala.zip4j.ZipFile
import net.lingala.zip4j.exception.ZipException import net.lingala.zip4j.exception.ZipException
@@ -84,8 +88,18 @@ class AndroidClassLoader(private val parent: ClassLoader, private val cacheDir:
} }
} }
@Throws(IOException::class)
fun loadJar(file: File): DexClassLoader { fun loadJar(file: File): DexClassLoader {
try {
val dexFile = jarToDex(file, cacheDir)
return loadDex(dexFile)
} catch (e: Exception) {
Log.e(TAG, "loadJar: failed to load jar ${file.path}", e)
return loadJar1(file)
}
}
@Throws(IOException::class)
fun loadJar1(file: File): DexClassLoader {
val path = file.path val path = file.path
Log.d(TAG, "loadJar: jar = $path") Log.d(TAG, "loadJar: jar = $path")
if (!file.exists() || !file.canRead()) { if (!file.exists() || !file.canRead()) {
@@ -215,6 +229,39 @@ class AndroidClassLoader(private val parent: ClassLoader, private val cacheDir:
private fun generateDexFileName(jar: File) = md5("${jar.path}_${jar.lastModified()}") private fun generateDexFileName(jar: File) = md5("${jar.path}_${jar.lastModified()}")
private fun jarToDex(jar: File, cacheDir: File): File {
val dexName = generateDexFileName(jar)
val dexFile = File(cacheDir, "$dexName.dex")
if (dexFile.exists()) {
return dexFile
}
val tmpOut = File(cacheDir, "${dexName}_tmp").apply { mkdirs() }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val cmd = D8Command.builder()
.addProgramFiles(jar.toPath())
.setOutput(tmpOut.toPath(), OutputMode.DexIndexed)
.setMode(CompilationMode.RELEASE)
.build()
D8.run(cmd)
} else {
val args = arrayOf(
"--output", tmpOut.absolutePath,
"--release",
jar.absolutePath
)
D8.main(args)
}
val classesDex = File(tmpOut, "classes.dex")
if (classesDex.exists().not()) {
throw IOException("R8 did not produce classes.dex")
}
if (classesDex.renameTo(dexFile).not()) {
dexFile.outputStream().use { out -> classesDex.inputStream().use { it.copyTo(out) } }
}
tmpOut.deleteRecursively()
return dexFile
}
companion object { companion object {
private val TAG = AndroidClassLoader::class.java.simpleName private val TAG = AndroidClassLoader::class.java.simpleName