简体   繁体   中英

Custom class loading fails with java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

I am trying to adjust the example from http://android-developers.blogspot.de/2011/07/custom-class-loading-in-dalvik.html to my needs. For this, I have a setup of 3 projects:

classloadingtest: Standard android application project containing the source of the MainActivity in the link above, slightly adjusted (moved the DexClassLoader business to a class named "AndroidClassLoader"), referencing the "iface" project below

iface: An android library project containing just an interface with only one function ("getMessage()") returning a String

loadable: A standard android application project also referencing the "iface" library containing just one class implementing that very simple interface and with a custom_rules.txt file adjusting the build process:

<?xml version="1.0" encoding="UTF-8"?>
<project name="BuildLoadableDex" default="debug">
    <target name="-dex" depends="-compile, -post-compile, -obfuscate">
        <echo level="info">Custom -dex target</echo>
        <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />
        <dex executable="${dx}"
                output="${intermediate.dex.file}"
                dexedlibs="${out.dexed.absolute.dir}"
                nolocals="@{nolocals}"
                forceJumbo="${dex.force.jumbo}"
                disableDexMerger="${dex.disable.merger}"
                verbose="${verbose}">
            <path path="${out.dex.input.absolute.dir}"/>
        </dex>
    </target>

    <target name="-package" depends="-dex, -package-resources">
        <echo level="info">Custom -package target</echo>
        <jar destfile="${out.absolute.dir}/${ant.project.name}.jar"
            basedir="${out.absolute.dir}"
            includes="${dex.file.name}" />
    </target>
</project>

So this essentially just modifies the -dex target to leave out the library classes from the classes.dex file (verified via dexdump not showing the interface) and changes the -package target to output a jar file instead of an apk.

I then copy the loadable.jar file to classloadingtest/assets and build that apk file.

Everything compiles fine, but when running and triggering the class loading, I get the following stack trace:

W/dalvikvm(14393): Class resolved by unexpected DEX: Lloadable/Test;(0x4294b620):0x5a6a7000 ref [Lclassloading/iface/LoadableClass;] Lclassloading/iface/LoadableClass;(0x42912fb8):0x5a4bf000
W/dalvikvm(14393): (Lloadable/Test; had used a different Lclassloading/iface/LoadableClass; during pre-verification)
I/dalvikvm(14393): Failed resolving Lloadable/Test; interface 6 'Lclassloading/iface/LoadableClass;'
W/dalvikvm(14393): Link of class 'Lloadable/Test;' failed
D/AndroidRuntime(14393): Shutting down VM
W/dalvikvm(14393): threadid=1: thread exiting with uncaught exception (group=0x41c622a0)
E/AndroidRuntime(14393): FATAL EXCEPTION: main
E/AndroidRuntime(14393): java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
E/AndroidRuntime(14393):        at dalvik.system.DexFile.defineClass(Native Method)
E/AndroidRuntime(14393):        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:211)
E/AndroidRuntime(14393):        at dalvik.system.DexPathList.findClass(DexPathList.java:315)
E/AndroidRuntime(14393):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:58)
E/AndroidRuntime(14393):        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(14393):        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(14393):        at classloading.loading.AndroidClassLoader.findClass(AndroidClassLoader.java:56)
E/AndroidRuntime(14393):        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(14393):        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(14393):        at classloading.loading.AndroidClassLoader.loadClass(AndroidClassLoader.java:37)
E/AndroidRuntime(14393):        at classloading.ClassLoadingTest$1.onClick(ClassLoadingTest.java:60)
E/AndroidRuntime(14393):        at android.view.View.performClick(View.java:4211)
E/AndroidRuntime(14393):        at android.view.View$PerformClick.run(View.java:17267)
E/AndroidRuntime(14393):        at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(14393):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(14393):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(14393):        at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(14393):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(14393):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(14393):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(14393):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(14393):        at dalvik.system.NativeStart.main(Native Method)

I do get that this means that somehow the LoadableClass interface "seems" to exist two times with differences, but I just can't understand how this happens, seeing both the APK (classloadingtest) and the loadable.jar use the exact same library project for compiling, thus I have no idea of how to fix this error.

Please feel free to request edits with more code, I just don't know what might be useful here and don't want to paste everything .

Stupidity resolved, I've been trying to load an old version of loadable.jar. Leaving this here so others might benefit and not waste that much time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM