@@ -593,16 +593,44 @@ private void generateMethod(ClassVisitor cv, ClassDescriptor classTo, MethodDesc
593593
594594 int thisRegister = generateMaxStackSize (mv , method );
595595
596- if (!classTo .isInterface ())
597- {
598- generateInitializedBlock (mv , thisRegister , classSignature , tnsClassSignature );
596+ if (!classTo .isInterface ()) {
597+ if (isApplicationClass (classTo ) && method .getName ().equals ("onCreate" )) {
598+ generateRuntimeInitializedBlock (mv , thisRegister , classSignature , tnsClassSignature , classTo .getName ());
599+ } else {
600+ generateInitializedBlock (mv , thisRegister , classSignature , tnsClassSignature );
601+ }
599602 }
600603
601604 generateCallOverrideBlock (mv , method , thisRegister , classSignature , tnsClassSignature , methodDexSignature , fieldBit );
602605
603606 mv .visitEnd ();
604607 }
605608
609+ private boolean isApplicationClass (ClassDescriptor clazz ) {
610+ boolean isApplicationClass = false ;
611+ String applicationClassName = "android.app.Application" ;
612+ ClassDescriptor currentClass = clazz ;
613+ while ((currentClass != null ) && !isApplicationClass ) {
614+ isApplicationClass = currentClass .getName ().equals (applicationClassName );
615+ if (!isApplicationClass ) {
616+ currentClass = currentClass .getSuperclass ();
617+ }
618+ }
619+ return isApplicationClass ;
620+ }
621+
622+ private void generateRuntimeInitializedBlock (MethodVisitor mv , int thisRegister , String classSignature , String tnsClassSignature , String superClassname ) {
623+ String name = "L" + superClassname .replace ('.' , '/' ) + ";" ;
624+
625+ mv .visitMethodInsn (Opcodes .INSN_INVOKE_SUPER , name , "onCreate" , "V" , new int [] { thisRegister });
626+ mv .visitMethodInsn (Opcodes .INSN_INVOKE_STATIC , "Lcom/tns/RuntimeHelper;" , "initRuntime" , "Lcom/tns/Runtime;Landroid/app/Application;" , new int [] { thisRegister });
627+ mv .visitIntInsn (org .ow2 .asmdex .Opcodes .INSN_MOVE_RESULT_OBJECT , 0 );
628+ Label label = new Label ();
629+ mv .visitJumpInsn (Opcodes .INSN_IF_EQZ , label , 0 , 0 );
630+ mv .visitMethodInsn (Opcodes .INSN_INVOKE_VIRTUAL , LCOM_TNS_RUNTIME , "run" , "V" , new int [] { 0 });
631+ mv .visitLabel (label );
632+ }
633+
606634 private int generateMaxStackSize (MethodVisitor mv , MethodDescriptor method )
607635 {
608636 //3 local vars are enough for NativeScript bindings methods. Local vars start from 0 register till register 2.
0 commit comments