ByteBuddy Android在加载时崩溃(getClass()。getClassLoader())

时间:2017-10-12 12:05:02

标签: android byte-buddy

我创造了“Hello World!” Android Studio中的android应用程序编译成功并运行。 接下来我添加了“Hello World!” ByteBuddy代码:

Class<?> dynamicType = new ByteBuddy()
                .subclass(Object.class)
                .method(ElementMatchers.named("toString"))
                .intercept(FixedValue.value("Hello World!"))
                .make()
                .load(getClass().getClassLoader())
                .getLoaded();

接下来我添加了依赖:

compile 'net.bytebuddy:byte-buddy-android:1.7.6'

应用程序在执行时崩溃了

.load(getClass().getClassLoader()) 

与以下logcat输出一致:

10-12 11:47:59.977 30562-30562/com.example.bytebuddyandroid D/AndroidRuntime: Shutting down VM


  --------- beginning of crash

10-12 11:47:59.979 30562-30562/com.example.bytebuddyandroid E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.bytebuddyandroid, PID: 30562
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bytebuddyandroid/com.example.bytebuddyandroid.MainActivity}: java.lang.UnsupportedOperationException: Cannot define class using reflection
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
      at android.app.ActivityThread.access$800(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5254)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
   Caused by: java.lang.UnsupportedOperationException: Cannot define class using reflection
      at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Unavailable.defineClass(ClassInjector.java:821)
      at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:185)
      at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:187)
      at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default.load(ClassLoadingStrategy.java:120)
      at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
      at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4456)
      at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4446)
      at com.example.bytebuddyandroid.MainActivity.onCreate(MainActivity.java:22)
      at android.app.Activity.performCreate(Activity.java:5990)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
   Caused by: java.lang.IllegalStateException: This JVM's version string does not seem to be valid: 0
      at net.bytebuddy.ClassFileVersion$VersionLocator$ForLegacyVm.locate(ClassFileVersion.java:388)
      at net.bytebuddy.ClassFileVersion.ofThisVm(ClassFileVersion.java:147)
      at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$CreationAction.run(ClassInjector.java:301)
      at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$CreationAction.run(ClassInjector.java:290)
      at java.security.AccessController.doPrivileged(AccessController.java:45)
      at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.<clinit>(ClassInjector.java:70)
      at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:184)
      at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default.load(ClassLoadingStrategy.java:120) 
      at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79) 
      at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4456) 
      at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4446) 
      at com.example.bytebuddyandroid.MainActivity.onCreate(MainActivity.java:22) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 



  --------- beginning of system

为什么

"java.lang.UnsupportedOperationException: Cannot define class using reflection"

抛出异常?如何避免此异常?

1 个答案:

答案 0 :(得分:3)

好的,我必须使用指定ClassLoadingStrategy的参数的其他版本的load()。所以我把它改成了

.load(getClass().getClassLoader(), new AndroidClassLoadingStrategy.Wrapping(this.getDir("dexgen", Context.MODE_PRIVATE)))

现在可以使用了!