/** * Generate the dex file * * @return byte code */ public static byte[] generate() { ApplicationWriter aw = new ApplicationWriter(); aw.visit(); generateFirstActivity(aw); aw.visitEnd(); return aw.toByteArray(); }
private static void generateFirstActivity(ApplicationWriter aw) { ClassVisitor cv; MethodVisitor mv; cv = aw.visitClass(ACC_PUBLIC, "Lft/nevo/FirstActivity;", null, "Landroid/app/Activity;", null); cv.visit(0, ACC_PUBLIC, "Lft/nevo/FirstActivity;", null, "Landroid/app/Activity;", null); cv.visitSource("FirstActivity.java", null); { mv = cv.visitMethod(ACC_PUBLIC + ACC_CONSTRUCTOR, "<init>", "V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(6, l0); mv.visitMethodInsn( INSN_INVOKE_DIRECT, "Landroid/app/Activity;", "<init>", "V", new int[] {0}); mv.visitInsn(INSN_RETURN_VOID); mv.visitMaxs(1, 0); mv.visitEnd(); } { mv = cv.visitMethod(ACC_PUBLIC, "onCreate", "VLandroid/os/Bundle;", null, null); mv.visitCode(); mv.visitParameters(new String[] {"savedInstanceState"}); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(10, l0); mv.visitMethodInsn( INSN_INVOKE_SUPER, "Landroid/app/Activity;", "onCreate", "VLandroid/os/Bundle;", new int[] {1, 2}); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(11, l1); mv.visitVarInsn(INSN_CONST_HIGH16, 0, 2130903040); mv.visitMethodInsn( INSN_INVOKE_VIRTUAL, "Lft/nevo/FirstActivity;", "setContentView", "VI", new int[] {1, 0}); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLineNumber(14, l2); mv.visitInsn(INSN_RETURN_VOID); mv.visitMaxs(3, 0); mv.visitEnd(); } { mv = cv.visitMethod(ACC_PUBLIC, "myMethod", "V", null, null); mv.visitCode(); // Added code. Label label1 = new Label(); Label label2 = new Label(); mv.visitLabel(label1); mv.visitJumpInsn(INSN_GOTO_16, label2, 0, 0); mv.visitJumpInsn(INSN_GOTO_16, label2, 0, 0); // NOPs are inserted here. for (int i = 0; i < MethodAdapterResizeGoto8BitsDoubleRefs.NB_NOPS; i++) { mv.visitInsn(INSN_NOP); } mv.visitLabel(label2); mv.visitJumpInsn(INSN_GOTO_16, label1, 0, 0); mv.visitJumpInsn(INSN_GOTO_16, label1, 0, 0); mv.visitInsn(INSN_RETURN_VOID); mv.visitMaxs(1, 0); mv.visitEnd(); } cv.visitEnd(); }