Fix Android loadJar (#10867)

* Fix Android loadJar

* Using another way

* Code formating

* Fix mod update
This commit is contained in:
Wxp
2025-06-02 14:32:24 -04:00
committed by GitHub
parent 18d852b5c1
commit 7076d1bf97
3 changed files with 70 additions and 24 deletions
@@ -73,6 +73,7 @@ public class AndroidLauncher extends AndroidApplication{
@Override @Override
public ClassLoader loadJar(Fi jar, ClassLoader parent) throws Exception{ public ClassLoader loadJar(Fi jar, ClassLoader parent) throws Exception{
//Required to load jar files in Android 14: https://developer.android.com/about/versions/14/behavior-changes-14#safer-dynamic-code-loading //Required to load jar files in Android 14: https://developer.android.com/about/versions/14/behavior-changes-14#safer-dynamic-code-loading
try{
jar.file().setReadOnly(); jar.file().setReadOnly();
return new DexClassLoader(jar.file().getPath(), getFilesDir().getPath(), null, parent){ return new DexClassLoader(jar.file().getPath(), getFilesDir().getPath(), null, parent){
@Override @Override
@@ -95,6 +96,34 @@ public class AndroidLauncher extends AndroidApplication{
return loadedClass; return loadedClass;
} }
}; };
}catch(SecurityException e){
//`setReadOnly` to jar file in `/sdcard/Android/data/...` does not work on some Android 14 device
//But in `/data/...` it works
if(Build.VERSION.SDK_INT < VERSION_CODES.O_MR1){
throw e;
}
Fi cacheDir = new Fi(getCacheDir()).child("mods");
cacheDir.mkdirs();
//long file name support
Fi modCacheDir = cacheDir.child(jar.nameWithoutExtension());
Fi modCache = modCacheDir.child(Long.toHexString(jar.lastModified()) + ".zip");
if(modCacheDir.equals(jar.parent())){
//should not reach here, just in case
throw e;
}
//Cache will be deleted when mod is removed
if(!modCache.exists() || jar.length() != modCache.length()){
modCacheDir.mkdirs();
jar.copyTo(modCache);
}
modCache.file().setReadOnly();
return loadJar(modCache, parent);
}
} }
@Override @Override
+1
View File
@@ -182,3 +182,4 @@ MonoChronos
RushieWashie RushieWashie
ITY ITY
Iniquit Iniquit
DSFdsfWxp
+18 -2
View File
@@ -413,19 +413,30 @@ public class Mods implements Loadable{
/** Removes a mod file and marks it for requiring a restart. */ /** Removes a mod file and marks it for requiring a restart. */
public void removeMod(LoadedMod mod){ public void removeMod(LoadedMod mod){
if(!android && mod.loader != null){ boolean deleted = true;
if(mod.loader != null){
if(android){
//Try to remove cache for Android 14 security problem
Fi cacheDir = new Fi(Core.files.getCachePath()).child("mods");
Fi modCacheDir = cacheDir.child(mod.file.nameWithoutExtension());
if(modCacheDir.exists()){
deleted = modCacheDir.deleteDirectory();
}
}else{
try{ try{
ClassLoaderCloser.close(mod.loader); ClassLoaderCloser.close(mod.loader);
}catch(Exception e){ }catch(Exception e){
Log.err(e); Log.err(e);
} }
} }
}
if(mod.root instanceof ZipFi){ if(mod.root instanceof ZipFi){
mod.root.delete(); mod.root.delete();
} }
boolean deleted = mod.file.isDirectory() ? mod.file.deleteDirectory() : mod.file.delete(); deleted &= mod.file.isDirectory() ? mod.file.deleteDirectory() : mod.file.delete();
if(!deleted){ if(!deleted){
ui.showErrorMessage("@mod.delete.error"); ui.showErrorMessage("@mod.delete.error");
@@ -1112,6 +1123,11 @@ public class Mods implements Loadable{
//close the classloader for jar mods //close the classloader for jar mods
if(!android){ if(!android){
ClassLoaderCloser.close(other.loader); ClassLoaderCloser.close(other.loader);
}else if(other.loader != null){
//Try to remove cache for Android 14 security problem
Fi cacheDir = new Fi(Core.files.getCachePath()).child("mods");
Fi modCacheDir = cacheDir.child(other.file.nameWithoutExtension());
modCacheDir.deleteDirectory();
} }
//close zip file //close zip file