I need to create a class that extends an abstract class in runtime in 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 this at runtime. Is this possible?
In “traditional” Java, you can create and compile classes at runtime, or use byte code generators such as ASM to augment or generate class files.
However, you need to keep in mind that Android is not a Java virtual machine. When you create an APK, class files are converted into specialized bytecode that is processed by Android’s Dalvik virtual machine.
I’m not aware of Dalvik-specific runtime bytecode generators, so I don’t believe that it is (currently) possible to do what you described.EDIT
There is a library called Dexmaker, which might accomplish this. I discovered it from this related answer.