Java – Creates a class in the Android runtime

Creates a class in the Android runtime… here is a solution to the problem.

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{

         super("A name that is saved on AbstractClass");
         fieldFromAbstractClass =...

    public void aMethodFromAbstractClass(){
       some code....

I want to generate it at runtime. Is this possible?


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.


don’t know about Dalvik-specific runtime bytecode generators, so I don’t think it’s possible (currently) to do what you’re described.


There is a library called Dexmaker that might achieve this. I found out from this related answer

Related Problems and Solutions