Creates a class in the Android runtime
I need to create a class that extends the abstract class at runtime for Android.
What I really need is to generate something like this:
Class A extends AbstractClass{
A(){
super("A name that is saved on AbstractClass");
fieldFromAbstractClass =...
}
@Override
public void aMethodFromAbstractClass(){
some code....
}
}
I want to generate it at runtime. Is this possible?
Solution
In “traditional” Java, you can create and compile classes at run time, or use bytecode generators, such as ASM, to augment or generate class files.
However, you need to remember that Android is not a Java virtual machine. When you create an APK, the class file is converted to private bytecode that is processed by Android’s Dalvik virtual machine.
I
don’t know about Dalvik-specific runtime bytecode generators, so I don’t think it’s possible (currently) to do what you’re described.
Edit
There is a library called Dexmaker that might achieve this. I found out from this related answer