Skip to content

Commit 380df40

Browse files
Merge pull request #475 from LYS86/master
修复部分jar加载失败
2 parents fa4790c + d992e3f commit 380df40

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ dependencies /* Unclassified */ {
241241

242242
// ICU4J
243243
implementation("com.ibm.icu:icu4j:77.1")
244+
245+
implementation("com.android.tools:r8:8.13.17")
244246
}
245247

246248
dependencies /* MIME */ {

app/src/main/java/org/autojs/autojs/rhino/AndroidClassLoader.kt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package org.autojs.autojs.rhino
33
import android.os.Build
44
import android.util.Log
55
import com.android.dx.command.dexer.Main
6+
import com.android.tools.r8.CompilationMode
7+
import com.android.tools.r8.D8
8+
import com.android.tools.r8.D8Command
9+
import com.android.tools.r8.OutputMode
610
import dalvik.system.DexClassLoader
711
import net.lingala.zip4j.ZipFile
812
import net.lingala.zip4j.exception.ZipException
@@ -84,8 +88,18 @@ class AndroidClassLoader(private val parent: ClassLoader, private val cacheDir:
8488
}
8589
}
8690

87-
@Throws(IOException::class)
8891
fun loadJar(file: File): DexClassLoader {
92+
try {
93+
val dexFile = jarToDex(file, cacheDir)
94+
return loadDex(dexFile)
95+
} catch (e: Exception) {
96+
Log.e(TAG, "loadJar: failed to load jar ${file.path}", e)
97+
return loadJar1(file)
98+
}
99+
}
100+
101+
@Throws(IOException::class)
102+
fun loadJar1(file: File): DexClassLoader {
89103
val path = file.path
90104
Log.d(TAG, "loadJar: jar = $path")
91105
if (!file.exists() || !file.canRead()) {
@@ -215,6 +229,39 @@ class AndroidClassLoader(private val parent: ClassLoader, private val cacheDir:
215229

216230
private fun generateDexFileName(jar: File) = md5("${jar.path}_${jar.lastModified()}")
217231

232+
private fun jarToDex(jar: File, cacheDir: File): File {
233+
val dexName = generateDexFileName(jar)
234+
val dexFile = File(cacheDir, "$dexName.dex")
235+
if (dexFile.exists()) {
236+
return dexFile
237+
}
238+
val tmpOut = File(cacheDir, "${dexName}_tmp").apply { mkdirs() }
239+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
240+
val cmd = D8Command.builder()
241+
.addProgramFiles(jar.toPath())
242+
.setOutput(tmpOut.toPath(), OutputMode.DexIndexed)
243+
.setMode(CompilationMode.RELEASE)
244+
.build()
245+
D8.run(cmd)
246+
} else {
247+
val args = arrayOf(
248+
"--output", tmpOut.absolutePath,
249+
"--release",
250+
jar.absolutePath
251+
)
252+
D8.main(args)
253+
}
254+
val classesDex = File(tmpOut, "classes.dex")
255+
if (classesDex.exists().not()) {
256+
throw IOException("R8 did not produce classes.dex")
257+
}
258+
if (classesDex.renameTo(dexFile).not()) {
259+
dexFile.outputStream().use { out -> classesDex.inputStream().use { it.copyTo(out) } }
260+
}
261+
tmpOut.deleteRecursively()
262+
return dexFile
263+
}
264+
218265
companion object {
219266

220267
private val TAG = AndroidClassLoader::class.java.simpleName

app/src/main/java/org/autojs/autojs/ui/error/ErrorDialogActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class ErrorDialogActivity extends BaseActivity {
2020

2121
@Override
2222
protected void onCreate(Bundle savedInstanceState) {
23-
super.onCreate(savedInstanceState);
2423
requestWindowFeature(Window.FEATURE_NO_TITLE);
24+
super.onCreate(savedInstanceState);
2525

2626
// Get extras from intent
2727
Intent intent = getIntent();

0 commit comments

Comments
 (0)