尝试在运行时生成 Dex 字节码
我尝试在运行时调用 DX Tool。我的主要想法是生成一个Dexfile,然后动态加载到Android系统中。我有一些错误,但我认为可以直接生成骰子。以下代码尝试使用构造函数生成一个简单的 HelloWorld Bean。有人可以帮助我吗?
public class HelloWorld{
private String message;
public HelloWorld(){}
public String getMessage(){
return message;}
public void setMessage(String message){
this.message=message;
}
}
我的示例代码如下:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.util.ArrayList;
import com.android.dx.rop.code.BasicBlock;
import com.android.dx.rop.code.BasicBlockList;
import com.android.dx.rop.code.FillArrayDataInsn;
import com.android.dx.rop.code.Insn;
import com.android.dx.rop.code.InsnList;
import com.android.dx.rop.code.LocalVariableInfo;
import com.android.dx.rop.code.PlainInsn;
import com.android.dx.rop.code.RegisterSpec;
import com.android.dx.rop.code.RegisterSpecList;
import com.android.dx.rop.code.Rop;
import com.android.dx.rop.code.RopMethod;
import com.android.dx.rop.code.SourcePosition;
import com.android.dx.rop.cst.Constant;
import com.android.dx.rop.cst.CstFieldRef;
import com.android.dx.rop.cst.CstMethodRef;
import com.android.dx.rop.cst.CstNat;
import com.android.dx.rop.cst.CstType;
import com.android.dx.rop.cst.CstUtf8;
import com.android.dx.rop.type.StdTypeList;
import com.android.dx.rop.type.Type;
import com.android.dx.util.IntList;
import com.android.dx.dex.code.BlockAddresses;
import com.android.dx.dex.code.CatchBuilder;
import com.android.dx.dex.code.CatchHandlerList;
import com.android.dx.dex.code.CatchTable;
import com.android.dx.dex.code.CstInsn;
import com.android.dx.dex.code.DalvOps;
import com.android.dx.dex.code.Dop;
import com.android.dx.dex.code.InsnFormat;
import com.android.dx.dex.code.SimpleInsn;
import com.android.dx.dex.code.StdCatchBuilder;
import com.android.dx.dex.code.OutputFinisher;
import com.android.dx.dex.code.DalvCode;
import com.android.dx.dex.code.form.Form10t;
import com.android.dx.dex.code.form.Form10x;
import com.android.dx.dex.code.form.Form11x;
import com.android.dx.dex.code.form.Form12x;
import com.android.dx.dex.code.form.Form22c;
import com.android.dx.dex.code.form.Form22x;
import com.android.dx.dex.code.form.Form35c;
import com.android.dx.dex.file.ClassDefItem;
import com.android.dx.dex.file.DexFile;
import com.android.dx.dex.file.EncodedField;
import com.android.dx.dex.file.EncodedMethod;
public class Main {
private static DexFile outputDex;
/**
* Prints the usage message.
*/
public void createDexFile() throws Exception{
CstType type=new CstType(Type.intern("Lmnt/sdcard/HolaMundo;"));
outputDex=new DexFile();
OutputStream fileOutputStream=new FileOutputStream("classes.dex");
/***Class***/
ClassDefItem clazz=new ClassDefItem(type, 1, new CstType(Type.intern("Ljava/lang/Object;")), new StdTypeList(0), new CstUtf8("HolaMundo.java"));
/***Fields***/
EncodedField field=new EncodedField(new CstFieldRef(type,new CstNat(new CstUtf8("message"),new CstUtf8("Ljava/lang/String;"))),2);
clazz.addInstanceField(field);
/***Methods***/
/***Constructor***/
CstMethodRef methodref=new CstMethodRef(type,new CstNat(new CstUtf8("<init>"),new CstUtf8("()V")));
/*Rop opcode1=new Rop(1,new Type("V", Type.BT_VOID),new StdTypeList(0),new StdTypeList(0),1,false,"nop");
Rop opcode2=new Rop(2,new Type("I",Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"move-int");
Rop opcode3=new Rop(2,new Type("J",Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"move-long");
Rop opcode4=new Rop(2,new Type("F",Type.BT_FLOAT),new StdTypeList(Type.BT_FLOAT),new StdTypeList(0),1,false,"move-float");
Rop opcode5=new Rop(2,new Type("D",Type.BT_DOUBLE),new StdTypeList(Type.BT_DOUBLE),new StdTypeList(0),1,false,"move-double");
Rop opcode6=new Rop(2,Type.intern("Ljava/lang/Object;"),new StdTypeList(Type.intern("Ljava/lang/Object;").getBasicType()),new StdTypeList(0),1,false,"move-object");
Rop opcode7=new Rop(2,new Type("<addr>", Type.BT_ADDR),new StdTypeList(Type.BT_ADDR),new StdTypeList(0),1,false,"move-return-address");
Rop opcode8=new Rop(3,new Type("I",Type.BT_INT),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-int");
Rop opcode9=new Rop(3,new Type("J",Type.BT_LONG),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-long");
Rop opcode10=new Rop(3,new Type("F",Type.BT_FLOAT),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-float");
Rop opcode11=new Rop(3,new Type("D",Type.BT_DOUBLE),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-double");
Rop opcode12=new Rop(3,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-object");
Rop opcode13=new Rop(5,new Type("I",Type.BT_INT),new StdTypeList(0),new StdTypeList(0),1,false,"const-int");
Rop opcode14=new Rop(5,new Type("J",Type.BT_LONG),new StdTypeList(0),new StdTypeList(0),1,false,"const-long");
Rop opcode15=new Rop(5,new Type("F",Type.BT_FLOAT),new StdTypeList(0),new StdTypeList(0),1,false,"const-float");
Rop opcode16=new Rop(5,new Type("D",Type.BT_DOUBLE),new StdTypeList(0),new StdTypeList(0),1,false,"const-double");
Rop opcode17=new Rop(5,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(Type.intern("Ljava/lang/Error;").getBasicType()),6,false,"const-object");
Rop opcode18=new Rop(5,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(0),1,false,"const-object-nothrow");
Rop opcode19=new Rop(6,new Type("V", Type.BT_VOID),new StdTypeList(0),new StdTypeList(0),3,false,"goto");
Rop opcode20=new Rop(7,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-eqz-int");
Rop opcode21=new Rop(8,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-nez-int");
Rop opcode22=new Rop(9,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-ltz-int");
Rop opcode23=new Rop(10,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-eqz-int");
Rop opcode24=new Rop(11,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-lez-int");
Rop opcode25=new Rop(12,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-gtz-int");
Rop opcode26=new Rop(7,new Type("V", Type.BT_VOID),new StdTypeList(Type.intern("Ljava/lang/Object;").getBasicType()),new StdTypeList(0),4,false,"if-nez-object");
Rop opcode27=new Rop(8,new Type("V", Type.BT_VOID),new StdTypeList(Type.intern("Ljava/lang/Object;").getBasicType()),new StdTypeList(0),4,false,"if-eqz-object");
Rop opcode28=new Rop(7,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-eq-NTI");
Rop opcode29=new Rop(8,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-ne-int");
Rop opcode30=new Rop(9,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-lt-int");
Rop opcode31=new Rop(10,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-ge-int");
Rop opcode32=new Rop(11,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-le-int");
Rop opcode33=new Rop(12,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-gt-int");
Rop opcode34=new Rop(7,new Type("V", Type.BT_VOID),StdTypeList.make(Type.intern("Ljava/lang/Object;"),Type.intern("Ljava/lang/Object;")),new StdTypeList(0),4,false,"if-eq-object");
Rop opcode35=new Rop(8,new Type("V", Type.BT_VOID),StdTypeList.make(Type.intern("Ljava/lang/Object;"),Type.intern("Ljava/lang/Object;")),new StdTypeList(0),4,false,"if-ne-object");
Rop opcode36=new Rop(13,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),5,false,"switch");
Rop opcode37=new Rop(14,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"add-int");
Rop opcode38=new Rop(14,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"add-long");
Rop opcode39=new Rop(14,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"add-float");
Rop opcode40=new Rop(14,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"add-double");
Rop opcode41=new Rop(15,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"sub-int");
Rop opcode42=new Rop(15,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"sub-long");
Rop opcode43=new Rop(15,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"sub-float");
Rop opcode44=new Rop(15,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"sub-double");
Rop opcode45=new Rop(16,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"mul-int");
Rop opcode46=new Rop(16,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"mul-long");
Rop opcode47=new Rop(16,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"mul-float");
Rop opcode48=new Rop(16,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"mul-double");
Rop opcode49=new Rop(17,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"div-int");
Rop opcode50=new Rop(17,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"div-long");
Rop opcode51=new Rop(17,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"div-float");
Rop opcode52=new Rop(17,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"div-double");
Rop opcode53=new Rop(18,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"rem-int");
Rop opcode54=new Rop(18,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"rem-long");
Rop opcode55=new Rop(18,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"rem-float");
Rop opcode56=new Rop(18,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"rem-double");
Rop opcode57=new Rop(19,new Type("I", Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"neg-int");
Rop opcode58=new Rop(19,new Type("J", Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"neg-long");
Rop opcode59=new Rop(19,new Type("F", Type.BT_FLOAT),new StdTypeList(Type.BT_FLOAT),new StdTypeList(0),1,false,"neg-float");
Rop opcode60=new Rop(19,new Type("D", Type.BT_DOUBLE),new StdTypeList(Type.BT_DOUBLE),new StdTypeList(0),1,false,"neg-double");
Rop opcode61=new Rop(20,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"and-int");
Rop opcode62=new Rop(20,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"and-long");
Rop opcode63=new Rop(21,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"or-int");
Rop opcode64=new Rop(21,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"or-long");
Rop opcode65=new Rop(22,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"xor-int");
Rop opcode66=new Rop(22,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"xor-long");
Rop opcode67=new Rop(23,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shl-int");
Rop opcode68=new Rop(23,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shl-long");
Rop opcode69=new Rop(24,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shr-int");
Rop opcode70=new Rop(24,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shr-long");
Rop opcode71=new Rop(25,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"ushr-int");
Rop opcode72=new Rop(25,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"ushr-long");
Rop opcode73=new Rop(26,new Type("I", Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"not-int");
Rop opcode74=new Rop(26,new Type("J", Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"not-long");
Rop opcode75=new Rop(14,new Type("I", Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"add-const-int");
Rop opcode76=new Rop(14,new Type("J", Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"add-const-long");
Rop opcode77=new Rop(14,new Type("F", Type.BT_FLOAT),new StdTypeList(Type.BT_FLOAT),new StdTypeList(0),1,false,"add-const-float");
Rop opcode78=new Rop(14,new Type("D", Type.BT_DOUBLE),new StdTypeList(Type.BT_DOUBLE),new StdTypeList(0),1,false,"add-const-double");
*/
Rop opcodeMethod1=new Rop(3,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-object");
Rop opcodeMethod2=new Rop(6,new Type("V", Type.BT_VOID),new StdTypeList(0),new StdTypeList(0),3,false,"goto");
SourcePosition sourcePositionMethod1=new SourcePosition(new CstUtf8("HolaMundo.java"),1,1);
SourcePosition sourcePositionMethod2=new SourcePosition(new CstUtf8("HolaMundo.java"),2,2);
RegisterSpec registerSpecMethod1=RegisterSpec.make(0,new Type("NffffHolaMundo;",Type.BT_OBJECT,2147483647));
RegisterSpec registerSpecMethod2=null;
RegisterSpec registerSpecMethod0=RegisterSpec.make(0,Type.intern("Ljava/lang/Object;"));
RegisterSpecList registerSpecListMethod0=new RegisterSpecList(1);
registerSpecListMethod0.set(0, registerSpecMethod1);
RegisterSpecList registerSpecListMethod1=new RegisterSpecList(0);
RegisterSpecList registerSpecListMethod2=new RegisterSpecList(0);
PlainInsn insn1=new PlainInsn(opcodeMethod1,sourcePositionMethod1,registerSpecMethod1,registerSpecListMethod1);
PlainInsn insn2=new PlainInsn(opcodeMethod2,sourcePositionMethod2,registerSpecMethod2,registerSpecListMethod2);
Dop dopMethod0 = new Dop(DalvOps.INVOKE_DIRECT,DalvOps.INVOKE_DIRECT,Form35c.THE_ONE,false,"invoke-direct");
Dop dopMethod1 = new Dop(DalvOps.RETURN_VOID, DalvOps.RETURN_VOID, Form10x.THE_ONE, false, "return-void");
CstInsn simpleInsnMethod0=new CstInsn(dopMethod0,sourcePositionMethod1,registerSpecListMethod0,methodref);
SimpleInsn simpleInsnMethod1=new SimpleInsn(dopMethod1,sourcePositionMethod2,registerSpecListMethod1);
OutputFinisher outputFinisherMethod1=new OutputFinisher(21,2);
outputFinisherMethod1.add(simpleInsnMethod0);
outputFinisherMethod1.add(simpleInsnMethod1);
/***DalvCode***/
InsnList insnList1=new InsnList(2);
insnList1.set(0, insn1);
insnList1.set(1, insn2);
int primarySuccesor1=0;
int label1=10;
IntList intList1=new IntList(1);
insnList1.setImmutable();
intList1.add(0);
intList1.setImmutable();
BasicBlock basicBlock1=new BasicBlock(label1,insnList1,intList1,primarySuccesor1);
InsnList insnList2=new InsnList(1);
insnList2.set(0, insn2);
int primarySuccesor2=11;
int label2=0;
IntList intList2=new IntList(11);
BasicBlockList basicBlockList1=new BasicBlockList(1);
basicBlockList1.set(0, basicBlock1);
// Bueno!! int[] orderMethod1={14,12,0,13};
/**** Constructor ****/
int[] orderMethod1={10};
RopMethod ropMethod1=new RopMethod(basicBlockList1,10);
BlockAddresses blockAddresses1=new BlockAddresses(ropMethod1);
StdCatchBuilder stdCatchBuilder1=new StdCatchBuilder(ropMethod1,orderMethod1,blockAddresses1);
DalvCode code1=new DalvCode(2,outputFinisherMethod1,stdCatchBuilder1);
EncodedMethod method=new EncodedMethod(methodref,0x10001,code1,new StdTypeList(0));
clazz.addDirectMethod(method);
/*** Method setMessage ***/
CstMethodRef methodref2=new CstMethodRef(type,new CstNat(new CstUtf8("setMessage"),new CstUtf8("(Ljava/lang/String;)V")));
Dop dopMethod2=new Dop(DalvOps.IPUT_OBJECT,DalvOps.IPUT_OBJECT,Form22c.THE_ONE,false,"iput-object");
Dop dopMethod3 = new Dop(DalvOps.RETURN_VOID,DalvOps.RETURN_VOID,Form10x.THE_ONE,false,"return-void");
SourcePosition sourcePositionMethod3=new SourcePosition(new CstUtf8("HolaMundo.java"),2,6);
SourcePosition sourcePositionMethod4=new SourcePosition(new CstUtf8("HolaMundo.java"),5,7);
RegisterSpec registerSpecMethod4=RegisterSpec.make(0,Type.intern("Ljava/lang/String;"));
RegisterSpec registerSpecMethod3=RegisterSpec.make(1, Type.intern("LHolaMundo;"));
RegisterSpecList registerSpecListMethod3=new RegisterSpecList(2);
RegisterSpecList registerSpecListMethod4=new RegisterSpecList(0);
registerSpecListMethod3.set(0,registerSpecMethod3);
registerSpecListMethod3.set(1,registerSpecMethod4);
CstFieldRef cstFieldRef1=new CstFieldRef(type,new CstNat(new CstUtf8("message"),new CstUtf8("Ljava/lang/String;")));
CstInsn simpleInsnMethod2=new CstInsn(dopMethod2,sourcePositionMethod3,registerSpecListMethod3,cstFieldRef1);
SimpleInsn simpleInsnMethod3 = new SimpleInsn(dopMethod3,sourcePositionMethod4, registerSpecListMethod4);
OutputFinisher outputFinisherMethod2=new OutputFinisher(22,2);
outputFinisherMethod2.add(simpleInsnMethod2);
outputFinisherMethod2.add(simpleInsnMethod3);
int[] orderMethod2={10};
RopMethod ropMethod2=new RopMethod(basicBlockList1,10);
BlockAddresses blockAddresses2=new BlockAddresses(ropMethod2);
StdCatchBuilder stdCatchBuilder2=new StdCatchBuilder(ropMethod2,orderMethod2,blockAddresses2);
DalvCode code2=new DalvCode(2,outputFinisherMethod2,stdCatchBuilder2);
EncodedMethod method2=new EncodedMethod(methodref2,1,code2,new StdTypeList(0));
clazz.addVirtualMethod(method2);
/*** Method getMessage ***/
CstMethodRef methodref3=new CstMethodRef(type,new CstNat(new CstUtf8("getMessage"),new CstUtf8("()Ljava/lang/String;")));
Dop dopMethod4 = new Dop(DalvOps.IGET_OBJECT, DalvOps.IGET_OBJECT, Form22c.THE_ONE, true, "iget-object");
Dop dopMethod5 = new Dop(DalvOps.RETURN_OBJECT,DalvOps.RETURN_OBJECT,Form11x.THE_ONE,false,"return-object");
RegisterSpec registerSpecMethod5=RegisterSpec.make(1, Type.intern("LHolaMundo;"));
RegisterSpec registerSpecMethod6=RegisterSpec.make(0,Type.intern("Ljava/lang/String;"));
SourcePosition sourcePositionMethod5=new SourcePosition(new CstUtf8("HolaMundo.java"),2,8);
SourcePosition sourcePositionMethod6=new SourcePosition(new CstUtf8("HolaMundo.java"),5,9);
RegisterSpecList registerSpecListMethod5=new RegisterSpecList(2);
registerSpecListMethod5.set(0, registerSpecMethod5);
registerSpecListMethod5.set(1,registerSpecMethod6);
RegisterSpecList registerSpecListMethod6=new RegisterSpecList(1);
registerSpecListMethod6.set(0, registerSpecMethod6);
CstFieldRef cstFieldRef2=new CstFieldRef(type,new CstNat(new CstUtf8("message"),new CstUtf8("Ljava/lang/String;")));
CstInsn simpleInsnMethod4=new CstInsn(dopMethod4,sourcePositionMethod5,registerSpecListMethod5,cstFieldRef2);
SimpleInsn simpleInsnMethod5=new SimpleInsn(dopMethod5,sourcePositionMethod6,registerSpecListMethod6);
OutputFinisher outputFinisherMethod3=new OutputFinisher(22,2);
outputFinisherMethod3.add(simpleInsnMethod4);
outputFinisherMethod3.add(simpleInsnMethod5);
int[] orderMethod3={10};
RopMethod ropMethod3=new RopMethod(basicBlockList1,10);
BlockAddresses blockAddresses3=new BlockAddresses(ropMethod3);
StdCatchBuilder stdCatchBuilder3=new StdCatchBuilder(ropMethod3,orderMethod3,blockAddresses3);
DalvCode code3=new DalvCode(2,outputFinisherMethod3,stdCatchBuilder3);
EncodedMethod method3=new EncodedMethod(methodref3,1,code3,new StdTypeList(0));
clazz.addVirtualMethod(method3);
outputDex.add(clazz);
outputDex.writeTo(fileOutputStream, null, false);
fileOutputStream.flush();
fileOutputStream.close();
InputStream is = new FileInputStream("mnt/sdcard/classes.dex");
int len;
int size = 1024;
byte[] buf;
if (is instanceof ByteArrayInputStream) {
size = is.available();
buf = new byte[size];
len = is.read(buf, 0, size);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
buf = bos.toByteArray();
}
System.out.println(System.getProperty("user.dir"));
System.out.println("buf "+buf.toString());
com.android.dx.command.dexer.Main m=new com.android.dx.command.dexer.Main();
m.createJar("mnt/sdcard/HelloWorld.jar", buf);
System.out.println("Yeahhhhh");
// ClassDefIt
}
/**
* Returns a copy of the given args array, but without the indicated
* element.
*
* @param orig non-null; original array
* @param n which element to omit
* @return non-null; new array
*/
private static String[] without(String[] orig, int n) {
int len = orig.length - 1;
String[] newa = new String[len];
System.arraycopy(orig, 0, newa, 0, n);
System.arraycopy(orig, n + 1, newa, n, len - n);
return newa;
}
public static void main(String[] args){
String[] args2={"--dex","--output=HolaMundo.jar","HolaMundo.class"};
Main m=new Main();
try{
m.createDexFile();
}catch(Exception e){System.out.println(e.getMessage());}
}
}
I have tried to invoke DX Tool at Runtime. My main idea is to generate a Dexfile and then load into Android System dynamically. I have some mistakes but i think that it is possible to generate a dile directly. The following code try to generate a simple HelloWorld Bean with a constructor. Can someone help me?
public class HelloWorld{
private String message;
public HelloWorld(){}
public String getMessage(){
return message;}
public void setMessage(String message){
this.message=message;
}
}
My sample code is the next:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.util.ArrayList;
import com.android.dx.rop.code.BasicBlock;
import com.android.dx.rop.code.BasicBlockList;
import com.android.dx.rop.code.FillArrayDataInsn;
import com.android.dx.rop.code.Insn;
import com.android.dx.rop.code.InsnList;
import com.android.dx.rop.code.LocalVariableInfo;
import com.android.dx.rop.code.PlainInsn;
import com.android.dx.rop.code.RegisterSpec;
import com.android.dx.rop.code.RegisterSpecList;
import com.android.dx.rop.code.Rop;
import com.android.dx.rop.code.RopMethod;
import com.android.dx.rop.code.SourcePosition;
import com.android.dx.rop.cst.Constant;
import com.android.dx.rop.cst.CstFieldRef;
import com.android.dx.rop.cst.CstMethodRef;
import com.android.dx.rop.cst.CstNat;
import com.android.dx.rop.cst.CstType;
import com.android.dx.rop.cst.CstUtf8;
import com.android.dx.rop.type.StdTypeList;
import com.android.dx.rop.type.Type;
import com.android.dx.util.IntList;
import com.android.dx.dex.code.BlockAddresses;
import com.android.dx.dex.code.CatchBuilder;
import com.android.dx.dex.code.CatchHandlerList;
import com.android.dx.dex.code.CatchTable;
import com.android.dx.dex.code.CstInsn;
import com.android.dx.dex.code.DalvOps;
import com.android.dx.dex.code.Dop;
import com.android.dx.dex.code.InsnFormat;
import com.android.dx.dex.code.SimpleInsn;
import com.android.dx.dex.code.StdCatchBuilder;
import com.android.dx.dex.code.OutputFinisher;
import com.android.dx.dex.code.DalvCode;
import com.android.dx.dex.code.form.Form10t;
import com.android.dx.dex.code.form.Form10x;
import com.android.dx.dex.code.form.Form11x;
import com.android.dx.dex.code.form.Form12x;
import com.android.dx.dex.code.form.Form22c;
import com.android.dx.dex.code.form.Form22x;
import com.android.dx.dex.code.form.Form35c;
import com.android.dx.dex.file.ClassDefItem;
import com.android.dx.dex.file.DexFile;
import com.android.dx.dex.file.EncodedField;
import com.android.dx.dex.file.EncodedMethod;
public class Main {
private static DexFile outputDex;
/**
* Prints the usage message.
*/
public void createDexFile() throws Exception{
CstType type=new CstType(Type.intern("Lmnt/sdcard/HolaMundo;"));
outputDex=new DexFile();
OutputStream fileOutputStream=new FileOutputStream("classes.dex");
/***Class***/
ClassDefItem clazz=new ClassDefItem(type, 1, new CstType(Type.intern("Ljava/lang/Object;")), new StdTypeList(0), new CstUtf8("HolaMundo.java"));
/***Fields***/
EncodedField field=new EncodedField(new CstFieldRef(type,new CstNat(new CstUtf8("message"),new CstUtf8("Ljava/lang/String;"))),2);
clazz.addInstanceField(field);
/***Methods***/
/***Constructor***/
CstMethodRef methodref=new CstMethodRef(type,new CstNat(new CstUtf8("<init>"),new CstUtf8("()V")));
/*Rop opcode1=new Rop(1,new Type("V", Type.BT_VOID),new StdTypeList(0),new StdTypeList(0),1,false,"nop");
Rop opcode2=new Rop(2,new Type("I",Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"move-int");
Rop opcode3=new Rop(2,new Type("J",Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"move-long");
Rop opcode4=new Rop(2,new Type("F",Type.BT_FLOAT),new StdTypeList(Type.BT_FLOAT),new StdTypeList(0),1,false,"move-float");
Rop opcode5=new Rop(2,new Type("D",Type.BT_DOUBLE),new StdTypeList(Type.BT_DOUBLE),new StdTypeList(0),1,false,"move-double");
Rop opcode6=new Rop(2,Type.intern("Ljava/lang/Object;"),new StdTypeList(Type.intern("Ljava/lang/Object;").getBasicType()),new StdTypeList(0),1,false,"move-object");
Rop opcode7=new Rop(2,new Type("<addr>", Type.BT_ADDR),new StdTypeList(Type.BT_ADDR),new StdTypeList(0),1,false,"move-return-address");
Rop opcode8=new Rop(3,new Type("I",Type.BT_INT),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-int");
Rop opcode9=new Rop(3,new Type("J",Type.BT_LONG),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-long");
Rop opcode10=new Rop(3,new Type("F",Type.BT_FLOAT),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-float");
Rop opcode11=new Rop(3,new Type("D",Type.BT_DOUBLE),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-double");
Rop opcode12=new Rop(3,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-object");
Rop opcode13=new Rop(5,new Type("I",Type.BT_INT),new StdTypeList(0),new StdTypeList(0),1,false,"const-int");
Rop opcode14=new Rop(5,new Type("J",Type.BT_LONG),new StdTypeList(0),new StdTypeList(0),1,false,"const-long");
Rop opcode15=new Rop(5,new Type("F",Type.BT_FLOAT),new StdTypeList(0),new StdTypeList(0),1,false,"const-float");
Rop opcode16=new Rop(5,new Type("D",Type.BT_DOUBLE),new StdTypeList(0),new StdTypeList(0),1,false,"const-double");
Rop opcode17=new Rop(5,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(Type.intern("Ljava/lang/Error;").getBasicType()),6,false,"const-object");
Rop opcode18=new Rop(5,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(0),1,false,"const-object-nothrow");
Rop opcode19=new Rop(6,new Type("V", Type.BT_VOID),new StdTypeList(0),new StdTypeList(0),3,false,"goto");
Rop opcode20=new Rop(7,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-eqz-int");
Rop opcode21=new Rop(8,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-nez-int");
Rop opcode22=new Rop(9,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-ltz-int");
Rop opcode23=new Rop(10,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-eqz-int");
Rop opcode24=new Rop(11,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-lez-int");
Rop opcode25=new Rop(12,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),4,false,"if-gtz-int");
Rop opcode26=new Rop(7,new Type("V", Type.BT_VOID),new StdTypeList(Type.intern("Ljava/lang/Object;").getBasicType()),new StdTypeList(0),4,false,"if-nez-object");
Rop opcode27=new Rop(8,new Type("V", Type.BT_VOID),new StdTypeList(Type.intern("Ljava/lang/Object;").getBasicType()),new StdTypeList(0),4,false,"if-eqz-object");
Rop opcode28=new Rop(7,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-eq-NTI");
Rop opcode29=new Rop(8,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-ne-int");
Rop opcode30=new Rop(9,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-lt-int");
Rop opcode31=new Rop(10,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-ge-int");
Rop opcode32=new Rop(11,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-le-int");
Rop opcode33=new Rop(12,new Type("V", Type.BT_VOID),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),4,false,"if-gt-int");
Rop opcode34=new Rop(7,new Type("V", Type.BT_VOID),StdTypeList.make(Type.intern("Ljava/lang/Object;"),Type.intern("Ljava/lang/Object;")),new StdTypeList(0),4,false,"if-eq-object");
Rop opcode35=new Rop(8,new Type("V", Type.BT_VOID),StdTypeList.make(Type.intern("Ljava/lang/Object;"),Type.intern("Ljava/lang/Object;")),new StdTypeList(0),4,false,"if-ne-object");
Rop opcode36=new Rop(13,new Type("V", Type.BT_VOID),new StdTypeList(Type.BT_INT),new StdTypeList(0),5,false,"switch");
Rop opcode37=new Rop(14,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"add-int");
Rop opcode38=new Rop(14,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"add-long");
Rop opcode39=new Rop(14,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"add-float");
Rop opcode40=new Rop(14,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"add-double");
Rop opcode41=new Rop(15,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"sub-int");
Rop opcode42=new Rop(15,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"sub-long");
Rop opcode43=new Rop(15,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"sub-float");
Rop opcode44=new Rop(15,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"sub-double");
Rop opcode45=new Rop(16,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"mul-int");
Rop opcode46=new Rop(16,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"mul-long");
Rop opcode47=new Rop(16,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"mul-float");
Rop opcode48=new Rop(16,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"mul-double");
Rop opcode49=new Rop(17,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"div-int");
Rop opcode50=new Rop(17,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"div-long");
Rop opcode51=new Rop(17,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"div-float");
Rop opcode52=new Rop(17,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"div-double");
Rop opcode53=new Rop(18,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"rem-int");
Rop opcode54=new Rop(18,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),StdTypeList.make(Type.intern("Ljava/lang/Error;"),Type.intern("Ljava/lang/ArithmeticException;")),6,false,"rem-long");
Rop opcode55=new Rop(18,new Type("F", Type.BT_FLOAT),StdTypeList.make(new Type("F",Type.BT_FLOAT),new Type("F",Type.BT_FLOAT)),new StdTypeList(0),1,false,"rem-float");
Rop opcode56=new Rop(18,new Type("D", Type.BT_DOUBLE),StdTypeList.make(new Type("D",Type.BT_DOUBLE),new Type("D",Type.BT_DOUBLE)),new StdTypeList(0),1,false,"rem-double");
Rop opcode57=new Rop(19,new Type("I", Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"neg-int");
Rop opcode58=new Rop(19,new Type("J", Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"neg-long");
Rop opcode59=new Rop(19,new Type("F", Type.BT_FLOAT),new StdTypeList(Type.BT_FLOAT),new StdTypeList(0),1,false,"neg-float");
Rop opcode60=new Rop(19,new Type("D", Type.BT_DOUBLE),new StdTypeList(Type.BT_DOUBLE),new StdTypeList(0),1,false,"neg-double");
Rop opcode61=new Rop(20,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"and-int");
Rop opcode62=new Rop(20,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"and-long");
Rop opcode63=new Rop(21,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"or-int");
Rop opcode64=new Rop(21,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"or-long");
Rop opcode65=new Rop(22,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"xor-int");
Rop opcode66=new Rop(22,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("J",Type.BT_LONG)),new StdTypeList(0),1,false,"xor-long");
Rop opcode67=new Rop(23,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shl-int");
Rop opcode68=new Rop(23,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shl-long");
Rop opcode69=new Rop(24,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shr-int");
Rop opcode70=new Rop(24,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"shr-long");
Rop opcode71=new Rop(25,new Type("I", Type.BT_INT),StdTypeList.make(new Type("I",Type.BT_INT),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"ushr-int");
Rop opcode72=new Rop(25,new Type("J", Type.BT_LONG),StdTypeList.make(new Type("J",Type.BT_LONG),new Type("I",Type.BT_INT)),new StdTypeList(0),1,false,"ushr-long");
Rop opcode73=new Rop(26,new Type("I", Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"not-int");
Rop opcode74=new Rop(26,new Type("J", Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"not-long");
Rop opcode75=new Rop(14,new Type("I", Type.BT_INT),new StdTypeList(Type.BT_INT),new StdTypeList(0),1,false,"add-const-int");
Rop opcode76=new Rop(14,new Type("J", Type.BT_LONG),new StdTypeList(Type.BT_LONG),new StdTypeList(0),1,false,"add-const-long");
Rop opcode77=new Rop(14,new Type("F", Type.BT_FLOAT),new StdTypeList(Type.BT_FLOAT),new StdTypeList(0),1,false,"add-const-float");
Rop opcode78=new Rop(14,new Type("D", Type.BT_DOUBLE),new StdTypeList(Type.BT_DOUBLE),new StdTypeList(0),1,false,"add-const-double");
*/
Rop opcodeMethod1=new Rop(3,Type.intern("Ljava/lang/Object;"),new StdTypeList(0),new StdTypeList(0),1,false,"move-param-object");
Rop opcodeMethod2=new Rop(6,new Type("V", Type.BT_VOID),new StdTypeList(0),new StdTypeList(0),3,false,"goto");
SourcePosition sourcePositionMethod1=new SourcePosition(new CstUtf8("HolaMundo.java"),1,1);
SourcePosition sourcePositionMethod2=new SourcePosition(new CstUtf8("HolaMundo.java"),2,2);
RegisterSpec registerSpecMethod1=RegisterSpec.make(0,new Type("NffffHolaMundo;",Type.BT_OBJECT,2147483647));
RegisterSpec registerSpecMethod2=null;
RegisterSpec registerSpecMethod0=RegisterSpec.make(0,Type.intern("Ljava/lang/Object;"));
RegisterSpecList registerSpecListMethod0=new RegisterSpecList(1);
registerSpecListMethod0.set(0, registerSpecMethod1);
RegisterSpecList registerSpecListMethod1=new RegisterSpecList(0);
RegisterSpecList registerSpecListMethod2=new RegisterSpecList(0);
PlainInsn insn1=new PlainInsn(opcodeMethod1,sourcePositionMethod1,registerSpecMethod1,registerSpecListMethod1);
PlainInsn insn2=new PlainInsn(opcodeMethod2,sourcePositionMethod2,registerSpecMethod2,registerSpecListMethod2);
Dop dopMethod0 = new Dop(DalvOps.INVOKE_DIRECT,DalvOps.INVOKE_DIRECT,Form35c.THE_ONE,false,"invoke-direct");
Dop dopMethod1 = new Dop(DalvOps.RETURN_VOID, DalvOps.RETURN_VOID, Form10x.THE_ONE, false, "return-void");
CstInsn simpleInsnMethod0=new CstInsn(dopMethod0,sourcePositionMethod1,registerSpecListMethod0,methodref);
SimpleInsn simpleInsnMethod1=new SimpleInsn(dopMethod1,sourcePositionMethod2,registerSpecListMethod1);
OutputFinisher outputFinisherMethod1=new OutputFinisher(21,2);
outputFinisherMethod1.add(simpleInsnMethod0);
outputFinisherMethod1.add(simpleInsnMethod1);
/***DalvCode***/
InsnList insnList1=new InsnList(2);
insnList1.set(0, insn1);
insnList1.set(1, insn2);
int primarySuccesor1=0;
int label1=10;
IntList intList1=new IntList(1);
insnList1.setImmutable();
intList1.add(0);
intList1.setImmutable();
BasicBlock basicBlock1=new BasicBlock(label1,insnList1,intList1,primarySuccesor1);
InsnList insnList2=new InsnList(1);
insnList2.set(0, insn2);
int primarySuccesor2=11;
int label2=0;
IntList intList2=new IntList(11);
BasicBlockList basicBlockList1=new BasicBlockList(1);
basicBlockList1.set(0, basicBlock1);
// Bueno!! int[] orderMethod1={14,12,0,13};
/**** Constructor ****/
int[] orderMethod1={10};
RopMethod ropMethod1=new RopMethod(basicBlockList1,10);
BlockAddresses blockAddresses1=new BlockAddresses(ropMethod1);
StdCatchBuilder stdCatchBuilder1=new StdCatchBuilder(ropMethod1,orderMethod1,blockAddresses1);
DalvCode code1=new DalvCode(2,outputFinisherMethod1,stdCatchBuilder1);
EncodedMethod method=new EncodedMethod(methodref,0x10001,code1,new StdTypeList(0));
clazz.addDirectMethod(method);
/*** Method setMessage ***/
CstMethodRef methodref2=new CstMethodRef(type,new CstNat(new CstUtf8("setMessage"),new CstUtf8("(Ljava/lang/String;)V")));
Dop dopMethod2=new Dop(DalvOps.IPUT_OBJECT,DalvOps.IPUT_OBJECT,Form22c.THE_ONE,false,"iput-object");
Dop dopMethod3 = new Dop(DalvOps.RETURN_VOID,DalvOps.RETURN_VOID,Form10x.THE_ONE,false,"return-void");
SourcePosition sourcePositionMethod3=new SourcePosition(new CstUtf8("HolaMundo.java"),2,6);
SourcePosition sourcePositionMethod4=new SourcePosition(new CstUtf8("HolaMundo.java"),5,7);
RegisterSpec registerSpecMethod4=RegisterSpec.make(0,Type.intern("Ljava/lang/String;"));
RegisterSpec registerSpecMethod3=RegisterSpec.make(1, Type.intern("LHolaMundo;"));
RegisterSpecList registerSpecListMethod3=new RegisterSpecList(2);
RegisterSpecList registerSpecListMethod4=new RegisterSpecList(0);
registerSpecListMethod3.set(0,registerSpecMethod3);
registerSpecListMethod3.set(1,registerSpecMethod4);
CstFieldRef cstFieldRef1=new CstFieldRef(type,new CstNat(new CstUtf8("message"),new CstUtf8("Ljava/lang/String;")));
CstInsn simpleInsnMethod2=new CstInsn(dopMethod2,sourcePositionMethod3,registerSpecListMethod3,cstFieldRef1);
SimpleInsn simpleInsnMethod3 = new SimpleInsn(dopMethod3,sourcePositionMethod4, registerSpecListMethod4);
OutputFinisher outputFinisherMethod2=new OutputFinisher(22,2);
outputFinisherMethod2.add(simpleInsnMethod2);
outputFinisherMethod2.add(simpleInsnMethod3);
int[] orderMethod2={10};
RopMethod ropMethod2=new RopMethod(basicBlockList1,10);
BlockAddresses blockAddresses2=new BlockAddresses(ropMethod2);
StdCatchBuilder stdCatchBuilder2=new StdCatchBuilder(ropMethod2,orderMethod2,blockAddresses2);
DalvCode code2=new DalvCode(2,outputFinisherMethod2,stdCatchBuilder2);
EncodedMethod method2=new EncodedMethod(methodref2,1,code2,new StdTypeList(0));
clazz.addVirtualMethod(method2);
/*** Method getMessage ***/
CstMethodRef methodref3=new CstMethodRef(type,new CstNat(new CstUtf8("getMessage"),new CstUtf8("()Ljava/lang/String;")));
Dop dopMethod4 = new Dop(DalvOps.IGET_OBJECT, DalvOps.IGET_OBJECT, Form22c.THE_ONE, true, "iget-object");
Dop dopMethod5 = new Dop(DalvOps.RETURN_OBJECT,DalvOps.RETURN_OBJECT,Form11x.THE_ONE,false,"return-object");
RegisterSpec registerSpecMethod5=RegisterSpec.make(1, Type.intern("LHolaMundo;"));
RegisterSpec registerSpecMethod6=RegisterSpec.make(0,Type.intern("Ljava/lang/String;"));
SourcePosition sourcePositionMethod5=new SourcePosition(new CstUtf8("HolaMundo.java"),2,8);
SourcePosition sourcePositionMethod6=new SourcePosition(new CstUtf8("HolaMundo.java"),5,9);
RegisterSpecList registerSpecListMethod5=new RegisterSpecList(2);
registerSpecListMethod5.set(0, registerSpecMethod5);
registerSpecListMethod5.set(1,registerSpecMethod6);
RegisterSpecList registerSpecListMethod6=new RegisterSpecList(1);
registerSpecListMethod6.set(0, registerSpecMethod6);
CstFieldRef cstFieldRef2=new CstFieldRef(type,new CstNat(new CstUtf8("message"),new CstUtf8("Ljava/lang/String;")));
CstInsn simpleInsnMethod4=new CstInsn(dopMethod4,sourcePositionMethod5,registerSpecListMethod5,cstFieldRef2);
SimpleInsn simpleInsnMethod5=new SimpleInsn(dopMethod5,sourcePositionMethod6,registerSpecListMethod6);
OutputFinisher outputFinisherMethod3=new OutputFinisher(22,2);
outputFinisherMethod3.add(simpleInsnMethod4);
outputFinisherMethod3.add(simpleInsnMethod5);
int[] orderMethod3={10};
RopMethod ropMethod3=new RopMethod(basicBlockList1,10);
BlockAddresses blockAddresses3=new BlockAddresses(ropMethod3);
StdCatchBuilder stdCatchBuilder3=new StdCatchBuilder(ropMethod3,orderMethod3,blockAddresses3);
DalvCode code3=new DalvCode(2,outputFinisherMethod3,stdCatchBuilder3);
EncodedMethod method3=new EncodedMethod(methodref3,1,code3,new StdTypeList(0));
clazz.addVirtualMethod(method3);
outputDex.add(clazz);
outputDex.writeTo(fileOutputStream, null, false);
fileOutputStream.flush();
fileOutputStream.close();
InputStream is = new FileInputStream("mnt/sdcard/classes.dex");
int len;
int size = 1024;
byte[] buf;
if (is instanceof ByteArrayInputStream) {
size = is.available();
buf = new byte[size];
len = is.read(buf, 0, size);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
buf = bos.toByteArray();
}
System.out.println(System.getProperty("user.dir"));
System.out.println("buf "+buf.toString());
com.android.dx.command.dexer.Main m=new com.android.dx.command.dexer.Main();
m.createJar("mnt/sdcard/HelloWorld.jar", buf);
System.out.println("Yeahhhhh");
// ClassDefIt
}
/**
* Returns a copy of the given args array, but without the indicated
* element.
*
* @param orig non-null; original array
* @param n which element to omit
* @return non-null; new array
*/
private static String[] without(String[] orig, int n) {
int len = orig.length - 1;
String[] newa = new String[len];
System.arraycopy(orig, 0, newa, 0, n);
System.arraycopy(orig, n + 1, newa, n, len - n);
return newa;
}
public static void main(String[] args){
String[] args2={"--dex","--output=HolaMundo.jar","HolaMundo.class"};
Main m=new Main();
try{
m.createDexFile();
}catch(Exception e){System.out.println(e.getMessage());}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 ASM 和 BCEL 实现了以下类:
首先,我使用 BCEL 或 ASM 在 SD 卡中创建一个新的临时类。
其次,我使用 SD 卡中的 Dxclient 实用程序将 Java 类转换为 Dex 类。
将此包加载到设备中
最后,我创建了一个 jar 文件,然后从 SD 卡DXClient 参考
https://github.com/headius/dexclient/blob/master/src/DexClient.java
您可以从 https://github.com/sciruela/android
I have implemented the following class using ASM and BCEL:
Firstly I have used BCEL or ASM to create a new ad-hoc class in SD Card.
Secondly I have converted the Java Class to a Dex Class with the Dxclient utiliy in SD Card.
Finally I have created a jar file and then I have loaded this package into the device from SD Card
DXClient reference
https://github.com/headius/dexclient/blob/master/src/DexClient.java
You can chek my code from https://github.com/sciruela/android