Hank
Hank

Reputation: 3497

Use DexClassLoader to load encrypted class

I was wondering if I can attach a encrypted class or jar file to my apk and within the app decrypt the class/jar file and then use DexClassLoader to load that class?

Upvotes: 0

Views: 3207

Answers (1)

aruwen
aruwen

Reputation: 212

It is possible, refer here for more basic information, what I did was to change the prepareDex function to decrypt the file and save it locally. heres part of the code to load the class via reflection:

final File optimizedDexOutputPath = getDir("outdex", Context.MODE_PRIVATE);
final File dexInternalStoragePath = new File(getDir("dex", Context.MODE_PRIVATE),
                SECONDARY_DEX_NAME);
        // Initialize the class loader with the secondary dex file.
DexClassLoader cl = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(),
        optimizedDexOutputPath.getAbsolutePath(), null, getClassLoader());

try {
     @SuppressWarnings("unchecked")
     final Class<Object> obj = (Class<Object>) cl.loadClass("com.packagename.Classname");

     final Constructor<Object> constr =  obj.getConstructor(Activity.class, String.class, Handler.class);
     myInstance  = constr.newInstance(this, mString, mHandler);

     Method doSomething;
     try {
              doSomething = v.getMethod("YOURMETHOD");
              doSomething.invoke(myInstance);
         }

    }

I also ommited all catches but those should be easy to generate/handle.

Upvotes: 3

Related Questions