|
| 1 | +package id.my.alvinq.prokitid.app.libs; |
| 2 | + |
| 3 | +import dalvik.system.DexClassLoader; |
| 4 | +import android.content.Context; |
| 5 | +import java.io.*; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.jar.JarFile; |
| 8 | +import java.util.jar.JarEntry; |
| 9 | +import org.json.JSONObject; |
| 10 | + |
| 11 | +public class LibsManager { |
| 12 | + private final Context context; |
| 13 | + private final File cDir; |
| 14 | + //private final DexClassLoader dcl; |
| 15 | + public LibsManager(Context ctx) { |
| 16 | + String dirPath = context.getDir("alvinqid", Context.MODE_PRIVATE).getAbsolutePath(); |
| 17 | + File cDir = new File(dirPath, "cache/dexout"); |
| 18 | + if(!cDir.exists()) cDir.mkdirs(); |
| 19 | + this.cDir = cDir; |
| 20 | + this.context = context; |
| 21 | + } |
| 22 | + |
| 23 | + public static LibsManager get(Context ctx) { |
| 24 | + LibsManager lm = new LibsManager(ctx); |
| 25 | + return lm; |
| 26 | + } |
| 27 | + public void loadLib(File file) { |
| 28 | + DexClassLoader dcl = new DexClassLoader(file.getAbsolutePath(),this.cDir.getAbsolutePath(),null,this.context.getClassLoader()); |
| 29 | + String className = getMainClassFromManifest(jarFile); |
| 30 | + if (className == null) { |
| 31 | + Logger.get().error("main class tidak ditemukan di manifest.json di jar: " + jarFile.getName()); |
| 32 | + return; |
| 33 | + } |
| 34 | + Logger.get().info("Loaded Class -> " + className); |
| 35 | + try { |
| 36 | + Class<?> clazz = dcl.loadClass(className); |
| 37 | + Method method = clazz.getDeclaredMethod("onLoad", Context.class); |
| 38 | + method.invoke(null, this.context); // static method, no instance |
| 39 | + Logger.get().info("Loaded Class -> " + className + " Done!"); |
| 40 | + } catch (Exception e) { |
| 41 | + Throwable real = e instanceof InvocationTargetException ? ((InvocationTargetException) e).getCause() : e; |
| 42 | + String errorLog = getStackTraceAsString(real); |
| 43 | + //System.err.println(errorLog); // atau kirim ke LeviLogger |
| 44 | + Logger.get().error("Error!: " + errorLog); |
| 45 | + } |
| 46 | + } |
| 47 | + public void unLoadLib(File file) { |
| 48 | + DexClassLoader dcl = new DexClassLoader(file.getAbsolutePath(),this.cDir.getAbsolutePath(),null,this.context.getClassLoader()); |
| 49 | + String className = getMainClassFromManifest(jarFile); |
| 50 | + if (className == null) { |
| 51 | + Logger.get().error("main class tidak ditemukan di manifest.json di jar: " + jarFile.getName()); |
| 52 | + return; |
| 53 | + } |
| 54 | + Logger.get().info("Unloaded Class -> " + className); |
| 55 | + try { |
| 56 | + Class<?> clazz = dcl.loadClass(className); |
| 57 | + Method method = clazz.getDeclaredMethod("onLoad", Context.class); |
| 58 | + method.invoke(null, this.context); // static method, no instance |
| 59 | + Logger.get().info("Unloaded Class -> " + className + " Done!"); |
| 60 | + } catch (Exception e) { |
| 61 | + Throwable real = e instanceof InvocationTargetException ? ((InvocationTargetException) e).getCause() : e; |
| 62 | + String errorLog = getStackTraceAsString(real); |
| 63 | + //System.err.println(errorLog); // atau kirim ke LeviLogger |
| 64 | + Logger.get().error("Error!: " + errorLog); |
| 65 | + } |
| 66 | + } |
| 67 | + private String getMainClassFromManifest(File jarFile) { |
| 68 | + try (JarFile jar = new JarFile(jarFile)) { |
| 69 | + JarEntry entry = jar.getJarEntry("manifest.json"); |
| 70 | + if (entry == null) return null; |
| 71 | + |
| 72 | + InputStream input = jar.getInputStream(entry); |
| 73 | + StringBuilder jsonBuilder = new StringBuilder(); |
| 74 | + |
| 75 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) { |
| 76 | + String line; |
| 77 | + while ((line = reader.readLine()) != null) { |
| 78 | + jsonBuilder.append(line); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + JSONObject json = new JSONObject(jsonBuilder.toString()); |
| 83 | + return json.getString("main"); |
| 84 | + |
| 85 | + } catch (Exception e) { |
| 86 | + Logger.get().error("Error!: " + e.toString()); |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + public static String getStackTraceAsString(Throwable th) { |
| 91 | + StringWriter sw = new StringWriter(); |
| 92 | + PrintWriter pw = new PrintWriter(sw); |
| 93 | + th.printStackTrace(pw); |
| 94 | + return sw.toString(); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments