Android Am I using multiple ClassLoader/PathClassLoaders?

New Guy :

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.

New Guy :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I want to execute multiple linux command lines in one .txt file(as parallel). I am using GNU parallel

If I am chaining multiple operators using RxJava do i need to call .subscribeOn() for each of them?

I am using multiple firebase projects in an Android App. I am getting this error : Missing google_app_id. Firebase Analytics disabled

I am using NOT IN but it is slow

Am I using '&&' incorrectly?

I am using handler for delay in android but it's not working

I am trying to get rid of rows using multiple values, but I am getting an error

Laravel: by using "With" am I avoiding multiple queries/connections into database?

I am unable to change the extension of a multiple files using the powershell

How do I ensure that I am using the same session for multiple HTTP call?

Thumbnail stays blank in RecyclerView (Android). Am I using Glide correctly?

How to parse Multiple JSON Objects and arrays from a url in android? i am using a example i want it in that example,

How can I debug an Android application using ADB/AM?

I am writing a code calculator app using android

Do I need my server if I am using Telegram api's in Android?

How to pretend I am using Windows or Android in Browser settings

Android SQLiteException: am I blind?

I am using android studio 2.1.2 and am trying to get the latest facebook sdk, gradle seem to have an error

Android: Am I using the BroadcastReceiver wrong?

I am trying update multiple rows using values is this correct?

I am developing an android app for job search .here i am using PHP as back end

I am trying to send multiple title and multiple files into database using laravel but i am getting error

I am trying to pass multiple arguments to a flutter screen by using generateRoute

I am not able to create two pages of pdf using PdfDocument of Android

I am using chip view in android using kotlin. I need to implement clickk function

When using the AppCenter API for android Kotlin, Am I allowed to change my UserId multiple times in the same session

I am trying to delete multiple elements from an array through checkbox i am using vue.js but i am unable to figure out how to do it

I am getting error while using CoordinatorTabLayout in android studio

I am unable to append multiple times using the createdocumentfragment JS call