I have this code which works fine with byte buddy in normal Java but it does not work in Android.
Class c = new ByteBuddy().makeInterface().name("TestInterface")
.defineMethod("test",String.class, Modifier.PUBLIC | Modifier.STATIC)
.intercept(FixedValue.value("Hello")).make()
.load(this.getClass().getClassLoader(),
new AndroidClassLoadingStrategy.Wrapping(context.getCacheDir())).getLoaded();
Object o = c.getMethod("test").invoke(c);
After running this in Android I get the error Cannot define non-virtual method test for a pre-java 8 interface type
.
I actually checked and from Android version 24 and later Interfaces with static methods is supported but I think the issue is that when you query Android for 'java.version' property it returns 0 which makes byte-buddy think we are an older version of JVM.
Byte Buddy validates against the Java version that is inferred from the platform context and on Android, this is indeed too strict. You can disable validation by setting:
new ByteBuddy().with(TypeValidation.DISABLED)