gpt4 book ai didi

java - Android : load(getClass(). getClassLoader(),classLoadingStrategy) 抛出 java.lang.IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 20:42:38 25 4
gpt4 key购买 nike

我试图在 Android 中的方法代理(...)下创建 byteBuddy 对象,然后在 byteBuddy 对象上调用一些方法:

<T> T proxy(Class<T> clz, InvocationHandler invocationHandler) {
ByteBuddy byteBuddy = null;
try {
byteBuddy = new ByteBuddy(ClassFileVersion.JAVA_V6);
} catch (Throwable e) {
//e.printStackTrace();
}
if(byteBuddy!=null) {
Class<?> enhanced = byteBuddy
.subclass(clz, ConstructorStrategy.Default.IMITATE_SUPER_TYPE)
.method(ElementMatchers.not(ElementMatchers.isDeclaredBy(Object.class)))
.intercept(InvocationHandlerAdapter.of(invocationHandler))
.make().load(getClass().getClassLoader(), classLoadingStrategy)
.getLoaded();

...

但是符合:

load(getClass().getClassLoader(), classLoadingStrategy)

ByteBuddy 抛出异常:

06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ java.lang.IllegalStateException: Cannot load class pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:138) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:3380) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.Messenger.proxy(Messenger.java:320) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.Messenger.async(Messenger.java:382) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.Messenger.async(Messenger.java:373) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.overlay.fissione.FissioneTransport.join(FissioneTransport.java:221) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.overlay.fissione.FissioneTransport.open(FissioneTransport.java:202) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.overlay.util.DHT.(DHT.java:37) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at pdl.transport.overlay.util.DHT.main(DHT.java:117) 06-11 21:29:23.351 12028-12028/com.example.AndroidOverlay W/System.err﹕ at com.example.AndroidOverlay.MyActivity_newbie.onCreate(MyActivity_newbie.java:72) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.Activity.performCreate(Activity.java:6289) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.ActivityThread.access$900(ActivityThread.java:177) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.os.Looper.loop(Looper.java:145) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5942) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ Caused by: java.lang.ClassNotFoundException: Didn't find class "pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79" on path: DexPathList[[zip file "/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:469) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:136) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ ... 22 more 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ Suppressed: java.io.IOException: Failed to open dex file '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar' from memory: Unrecognized version number in /data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar: 0 3 6 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexFile.openDexFileNative(Native Method) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexFile.openDexFile(DexFile.java:295) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexFile.(DexFile.java:111) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexFile.loadDex(DexFile.java:151) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexPathList.loadDexFile(DexPathList.java:265) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexPathList.makeDexElements(DexPathList.java:231) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexPathList.(DexPathList.java:109) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.BaseDexClassLoader.(BaseDexClassLoader.java:48) 06-11 21:29:23.361 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.DexClassLoader.(DexClassLoader.java:57) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at net.bytebuddy.android.AndroidClassLoadingStrategy$DexProcessor$ForSdkCompiler.makeClassLoader(AndroidClassLoadingStrategy.java:257) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:132) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ ... 22 more 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ Caused by: java.io.IOException: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --instruction-set=arm --instruction-set-features=div --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --dex-file=/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar --oat-fd=64 --art-fd=-1 --oat-location=/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex --runtime-arg -Xms64m --runtime-arg -Xmx512m) because non-0 exit status 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ ... 33 more 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ Caused by: java.io.IOException: Failed to find dex file '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar' in oat location '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex': Failed to find existing oat file at /data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex: File size of 0 bytes not large enough to contain ELF header of 52 bytes: '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex' 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ ... 33 more 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ Suppressed: java.lang.ClassNotFoundException: Didn't find class "pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79" on path: DexPathList[[zip file "/data/app/com.example.AndroidOverlay-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:504) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ ... 24 more 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ Suppressed: java.lang.ClassNotFoundException: pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.Class.classForName(Native Method) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.BootClassLoader.findClass(ClassLoader.java:781) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:504) 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ ... 25 more 06-11 21:29:23.371 12028-12028/com.example.AndroidOverlay W/System.err﹕ Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我正在使用来自 http://central.maven.org/maven2/com/google/android/tools/dx/1.7/dx-1.7.jar 的 dx-1.7.jar和 byte-buddy-0.6.8.jar,byte-buddy-android-0.6.8.jar

我错过了什么吗?我也得到了 Could not run Dynamic type.(Failed resolution of: Lorg/objectweb/asmType;) 在你的 android 示例应用程序上,似乎我缺少一些东西,一些库或者我不知道......谢谢你的回答。

编辑 1:我正在使用 Lollipop,这就是我设置 AndroidClassLoadingStrategy 的方式:

final File dir = this.getDir("dexgen", Context.MODE_PRIVATE);
Messenger.setClassLoadingStrategy(new AndroidClassLoadingStrategy(dir));

它设置为 Messenger 类,其中我有方法 proxy(...)

public static void setClassLoadingStrategy(ClassLoadingStrategy cls) {
classLoadingStrategy = cls;
}

classLoadingStrategy 定义为

private static ClassLoadingStrategy classLoadingStrategy;

编辑 2:毕竟我采用了 ByteBuddy 示例应用程序,尝试了 Android Lollipop 替换行

File file = TestActivity.this.getDir(RandomString.make(), Context.MODE_PRIVATE);

对于应该替换的其中一个,尝试了所有这些并且都给出了相同的异常,只是来自不同的类 Activity

File file = getCodeCacheDir(); //NOT WORKING
// File file = getApplicationContext().getCodeCacheDir(); //NOT WORKING
// File file = getBaseContext().getCodeCacheDir(); //NOT WORKING
// File file = TestActivity.this.getCodeCacheDir(); //NOT WORKING

异常依旧:

06-12 23:18:57.916    1947-1947/net.bytebuddy.android.test W/net.bytebuddy﹕ java.lang.IllegalStateException: Cannot load class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$uSYJ5787$auxiliary$MBywjCuh

我创建并在我的网站上放置了资源,以便将来在此处重现错误:

bashism.com/shared/ByteBuddyTest.tar.gz

编辑 3:

安卓版本:5.0.1 Lollipop

设备:三星 galaxy S4

集成环境:IntelliJ Idea 14.1.3

编辑 4:在 Main Activity 内部更改后:

File file = TestActivity.this.getDir(RandomString.make(), Context.MODE_PRIVATE);

在 net.bytebuddy.android.AndroidClassLoadingStrategy 中

public ForSdkCompiler(DexOptions dexFileOptions, CfOptions dexCompilerOptions) {
dexFileOptions.targetApiLevel = 13;
this.dexFileOptions = dexFileOptions;
this.dexCompilerOptions = dexCompilerOptions;

似乎工作/稳定,即将进一步调试

最佳答案

正如我在上面的评论中提到的,问题是生成的 DEX 文件当前太新(较新的版本显然引入了“扩展操作码”),底层 ART 机制无法处理。实际问题可能源于具有不同默认 DexOptions.targetApiLevel 值(可能会忽略实际 API 构建目标)的不同设备。要解决此问题,任何涉及 classes.dex 文件的类加载策略都应专门将此值设置为 13 或更低,以确保生成正确的 dex 文件。例如,在@raphw 的 AndroidClassLoadingStrategy 中,可以修改 DexProcessor.ForSdkCompiler 构造函数以专门设置此值,例如:

public ForSdkCompiler(DexOptions dexFileOptions, CfOptions dexCompilerOptions) {
dexFileOptions.targetApiLevel = 13;
this.dexFileOptions = dexFileOptions;
this.dexCompilerOptions = dexCompilerOptions;
}

关于java - Android : load(getClass(). getClassLoader(),classLoadingStrategy) 抛出 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30791561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com