-
-
Save ValentinAhrend/180d127a7fd915604afdc2718a16293b to your computer and use it in GitHub Desktop.
The complete code for getting an dex file from .class files and then generating a java.lang.Class object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.exsent.app.example; | |
import android.content.Context; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Objects; | |
import dalvik.system.DexClassLoader; | |
/** | |
* Created by Jim-Linus Valentin Ahrend on 2/3/21. | |
* | |
* This is a part of the exsent source code. | |
* | |
**/ | |
class JavaCommonFileSetup { | |
private static final String TAG = JavaCommonFileSetup.class.getSimpleName(); | |
public interface ClassPro { | |
void classLoaded(Class c); | |
} | |
static void jarToClass(final Context context, File jar_file, ClassPro pro) { | |
final String DEX_FILE_NAME = "YourDexFile"; | |
final String DIR_NAME = "YourDirName"; | |
Thread thread = new Thread(() -> { | |
try { | |
File storage = new File(DIR_NAME); | |
//check space | |
/* | |
Android.jar is always the same, a international file dir is needed! | |
*/ | |
java.util.jar.JarFile jar = new java.util.jar.JarFile(jar_file); | |
java.util.Enumeration enumEntries = jar.entries(); | |
while (enumEntries.hasMoreElements()) { | |
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement(); | |
java.io.File f = new java.io.File(storage.getAbsolutePath() + "/" + file.getName()); | |
if (!f.getAbsolutePath().contains("__MACOSX")) { | |
if (file.isDirectory()) { // if its a directory, create it | |
//noinspection ResultOfMethodCallIgnored | |
f.mkdir(); | |
continue; | |
} | |
java.io.InputStream is = jar.getInputStream(file); // get the input stream | |
java.io.FileOutputStream fos = new java.io.FileOutputStream(f); | |
while (is.available() > 0) { // write contents of 'is' to 'fos' | |
fos.write(is.read()); | |
} | |
fos.close(); | |
is.close(); | |
} | |
} | |
jar.close(); | |
//generating file with code | |
//generated successfully | |
List<File> files2 = Arrays.asList(Objects.requireNonNull(storage.listFiles(pathname -> pathname.getAbsolutePath().endsWith(".class")))); | |
files2.forEach(file -> { | |
if (file.isDirectory()) { | |
List<File> add_files = Arrays.asList(Objects.requireNonNull(file.listFiles(pathname -> pathname.getAbsolutePath().endsWith(".class")))); | |
files2.addAll(add_files); | |
} | |
}); | |
String[] args2 = new String[5 + files2.size()]; | |
args2[0] = "--dex"; | |
args2[1] = "--keep-classes"; | |
args2[2] = "--output=" + DEX_FILE_NAME + ".dex"; | |
args2[3] = "--min-sdk-version=26"; | |
args2[4] = "--verbose"; | |
int var0 = 5; | |
for (File f : | |
files2) { | |
args2[var0] = f.getAbsolutePath(); | |
var0++; | |
} | |
com.android.dx.command.Main.main(args2, () -> { | |
//noinspection ResultOfMethodCallIgnored | |
jar_file.delete(); | |
DexClassLoader cl = new DexClassLoader(storage.getAbsolutePath() + "/" + DEX_FILE_NAME + ".dex", storage.getAbsolutePath(), null, context.getClassLoader()); | |
try { | |
pro.classLoaded(cl.loadClass("YOUR_PACKAGE.YOUR_CLASS")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
pro.classLoaded(null); | |
} | |
}); | |
}catch (IOException ioe){ | |
ioe.printStackTrace(); | |
} | |
}); | |
thread.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment