New Guy
New Guy

Reputation: 596

Android Am I using multiple ClassLoader/PathClassLoaders?

I am having issues with ClassLoaders in Android. At least I think it has to do with ClassLoaders. So here is the issue... I have a project that utilizes a custom widget library, lets call it CustomDialogView. It is a subclass of an android View object. I have a dialog that I created that extends CustomDialogView... So its important to mention the structure of how views are created and setup within this process (and eventually the activity).

My project is an Android service that registers Views to another process via a ContentResolver. The other process then pulls this data and will create the views based off of the classes sent. So, the view is eventually created and inflated in this other process (in an activity). I know, that is confusing but that is the method that is used for our application. So, in the view code eventually I may need to show a dialog. They give me a standard Object and it uses reflection to call the show dialog method. So here is the code snippet:

private static void showTheDialog(Object mainActivity, CustomDialogView view, boolean isModal) {
        try {
            mainActivity.getClass().getMethod("showDialog", View.class, Boolean.TYPE).invoke(mainActivity, view, isModal);
        } catch (Exception var4) {
            Log.w(TAG, var4.getClass().getName(), var4);
        }

    }

So on their end, they have this method within that object of interest:

public void showDialog(View dialogView, boolean isModal)

So here is where the issue comes in. In the method mentioned above (showDialog). They try to cast the dialogView, into a CustomDialogView so:

CustomDialogView dialogContent = (CustomDialogView)dialogView;

I get a ClassCastException as a result. I confirmed that I am using the same version of library that implements the CustomDialogView between both projects. I print the class loaders when I call showTheDialog and I get:

dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.this.is.project.myproject-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.this.is.project.myproject-1, /vendor/lib, /system/lib]]]

I print out the class loader in the showDialog (in thier project) and I get:

dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.this.is.thier.project.thierproject-1.apk", zip file "/data/data/com.this.is.thier.project/code_cache/secondary-dexes/com.this.is.thier.project.thierproject-1.apk.classes2.zip", zip file "/data/data/com.this.is.thier.project/code_cache/secondary-dexes/com.this.is.thier.project.thierproject-1.apk.classes3.zip", zip file "/data/data/com.this.is.thier.project/code_cache/secondary-dexes/com.this.is.thier.project.thierproject-1.apk.classes4.zip", zip file "/data/data/com.this.is.thier.project/code_cache/secondary-dexes/com.this.is.thier.project.thierproject-1.apk.classes5.zip", zip file "/data/data/com.this.is.thier.project/code_cache/secondary-dexes/com.this.is.thier.project.thierproject-1.apk.classes6.zip"],nativeLibraryDirectories=[/data/app-lib/com.this.is.thier.project.thierproject-1, /vendor/lib, /system/lib]]]

So, we are using two different class loaders right? I have a hard time understanding this concept. Please let me know if you have any other questions if needed.

Upvotes: 1

Views: 411

Answers (1)

New Guy
New Guy

Reputation: 596

So, it appears that I am using multiple class loaders. As you may know, this can cause the class cast exception to occur. To solve, I can use reflection on the passed object. So here is a small snippit:

obj.getClass().getMethod("getterMethod").invoke(obj);

Above is the example of taking in the passed object (obj), using reflection to obtain its class then calling the method that I need. I hope this helps others. No class loader issues now. Thanks.

Upvotes: 1

Related Questions