Search in sources :

Example 1 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project atlas by alibaba.

the class TPatchTool method main.

//    public static void main(String[] args) throws Exception {
//        File newApk = new File("/Users/shenghua/Downloads/tpatch/new.apk");
//        File baseApk = new File("/Users/shenghua/Downloads/tpatch/base.apk");
//        File atalsPatchFoder = new File("/Users/shenghua/Downloads/tpatch/tpatch");
//        atalsPatchFoder.mkdirs();
//        TPatchTool tPatchTool = new TPatchTool(baseApk, newApk, "5.5.3.39", "5.5.3.40", true);
//        tPatchTool.setMainBundleName("libcom_taobao_maindex");
//        tPatchTool.setNotIncludeFiles(new String[] { "lib/x86/**" });
//        tPatchTool.setRetainMainBundleRes(false);
//        List<ArtifactBundleInfo> bundleInfoList = new ArrayList<ArtifactBundleInfo>();
//        Set<ArtifactBundleInfo> artifactBundleInfos = Sets.newHashSet();
//        bundleInfoList = JSON.parseArray(FileUtils.readFileToString(new File("/Users/shenghua/Downloads/tpatch/tpatch-bundles.json")),
//                                         ArtifactBundleInfo.class);
//        artifactBundleInfos.addAll(bundleInfoList);
//        tPatchTool.setArtifactBundleInfos(artifactBundleInfos);
//        tPatchTool.setOnlyIncludeModifyBundle(true);
//        tPatchTool.doPatch(atalsPatchFoder, true, new File(atalsPatchFoder, "patchs.json"), false, null, "");
//    }
public static void main(String[] args) throws Exception {
    //        String response = HttpClientUtils.getUrl(url);
    //        if (response.equals("\"\"")){
    //            System.out.println("xxx");
    //        }
    //        String aaa = StringEscapeUtils.unescapeJava(response);
    //        URL url1 = new URL(aaa.substring(1,aaa.length()-1));
    ////        downloadTPath(aaa.substring(1,aaa.length()-1), new File("/Users/lilong/Downloads/1111.patch"));
    ////        PatchUtils.getTpatchClassDef(lastPatchFile, bundleClassMap)
    Map<String, Map<String, ClassDef>> bundleClassMap = new ConcurrentHashMap<String, Map<String, ClassDef>>();
    PatchUtils.getTpatchClassDef(new File("/Users/lilong/Downloads/temp/patch-6.1.1@6.1.0.zip"), bundleClassMap);
    System.out.println(bundleClassMap.size());
    TPatchTool tPatchTool = new TPatchTool(new File("/Users/lilong/Downloads/taobao-android.apk"), new File("/Users/lilong/Downloads/tpatch-diff.apk"), "1.0.0", "2.0.0", true);
    tPatchTool.bundleClassMap = bundleClassMap;
    tPatchTool.doPatch(new File("/Users/lilong/Downloads/aaa"), false, null, true, null, "taobao4android");
//
//        TPatchDexTool dexTool = new TPatchDexTool(new File("/Users/lilong/Downloads/10006492@taobao_android_5.7.2/classes.dex"), new File("/Users/lilong/Downloads/taobao-android/classes.dex"), DEFAULT_API_LEVEL);
//        File dexDiffFile = new File(new File("/Users/lilong/Downloads/10006492@taobao_android_5.7.2"), "diff.dex");
//        DexDiffInfo dexDiffInfo = dexTool.createTPatchDex(dexDiffFile);
//        TPatchTool.debug = true;
//        TPatchTool.isTpatch = true;
//
//        TPatchTool tPatchTool = new TPatchTool(new File("/Users/lilong/Downloads/taobao-android3.apk"), new File("/Users/lilong/Downloads/tpatch-diff.apk"),"5.8.0","5.8.4", true);
//
//        File dexDiffFile = new File("/Users/lilong/Downloads/taobao-android");
//        tPatchTool.doPatch(dexDiffFile,false,null,false,null,null);
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) File(java.io.File)

Example 2 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project atlas by alibaba.

the class ApkPatch method buildPrepareClass.

private static Set<String> buildPrepareClass(File smaliDir, List<File> newFiles, DexDiffInfo info) throws PatchException {
    Set<DexBackedClassDef> classes = Sets.newHashSet();
    classes = SmaliDiffUtils.scanClasses(smaliDir, newFiles);
    ArrayList<String> methods = new ArrayList<String>();
    {
        Set<DexBackedMethod> tempSet = info.getModifiedMethods();
        for (DexBackedMethod methodRef : tempSet) {
            String template = methodRef.getDefiningClass() + "->" + methodRef.getName();
            methods.add(template);
            System.out.println("template: " + template);
            if (superClasses.containsKey(methodRef.getDefiningClass())) {
                ArrayList<String> derivedClasses = superClasses.get(methodRef.getDefiningClass());
                for (int i = 0; i < derivedClasses.size(); i++) {
                    template = derivedClasses.get(i) + "->" + methodRef.getName();
                    System.out.println("template: " + template);
                    methods.add(template);
                }
            }
        }
    }
    Set<String> prepareClasses = new HashSet<String>();
    try {
        final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        for (DexBackedClassDef classDef : classes) {
            currentClassType = null;
            String className = TypeGenUtil.newType(classDef.getType());
            // baksmali.disassembleClass(classDef, outFileNameHandler, options);
            File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
            if (!smaliFile.exists()) {
                continue;
            }
            //增加class注解到prepare
            getClassAnnotaionPrepareClasses(classDef, prepareClasses, info);
            BufferedReader br = new BufferedReader(new FileReader(smaliFile));
            // 一次读入一行,直到读入null为文件结束
            String data = br.readLine();
            while (data != null) {
                boolean find = false;
                for (String m : methods) {
                    if (data.contains(m)) {
                        find = true;
                        break;
                    }
                }
                if (find) {
                    prepareClasses.add(className.substring(1, className.length() - 1).replace('/', '.'));
                    System.out.println("prepare class: " + className);
                    break;
                }
                // 接着读下一行
                data = br.readLine();
            }
            br.close();
        }
    } catch (Exception e) {
        throw new PatchException(e);
    }
    for (DexBackedMethod method : info.getModifiedMethods()) {
        prepareClasses.add(method.getDefiningClass().substring(1, method.getDefiningClass().length() - 1).replace("/", "."));
    }
    //        getMethodAnnotaionPrepareClasses(info,prepareClasses);
    return prepareClasses;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DexBackedMethod(org.jf.dexlib2.dexbacked.DexBackedMethod) ClassFileNameHandler(org.jf.util.ClassFileNameHandler) ArrayList(java.util.ArrayList) PatchException(com.taobao.android.differ.dex.PatchException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PatchException(com.taobao.android.differ.dex.PatchException) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project atlas by alibaba.

the class ApkPatch method getClassAnnotaionPrepareClasses.

public static void getClassAnnotaionPrepareClasses(DexBackedClassDef classDef, Set<String> prepareclasses, DexDiffInfo dexDiffInfo) {
    for (DexBackedClassDef modifyClasses : dexDiffInfo.getModifiedClasses()) {
        if (classDef.getType().equals(modifyClasses.getType())) {
            if (classDef.getAnnotations() != null) {
                Set<? extends DexBackedAnnotation> annotations = classDef.getAnnotations();
                for (DexBackedAnnotation annotation : annotations) {
                    String type = annotation.getType();
                    if (type != null && type.startsWith("L") && type.endsWith(";")) {
                        prepareclasses.add(type.substring(1, type.length() - 1).replace('/', '.'));
                        System.out.println("prepare class: " + type);
                    }
                    Set<? extends DexBackedAnnotationElement> elements = annotation.getElements();
                    for (DexBackedAnnotationElement dexBackedAnnotationElement : elements) {
                        if (dexBackedAnnotationElement.getValue() instanceof DexBackedArrayEncodedValue) {
                            List<? extends EncodedValue> values = ((DexBackedArrayEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
                            for (EncodedValue encodedValue : values) {
                                if (encodedValue instanceof TypeEncodedValue) {
                                    prepareclasses.add(((TypeEncodedValue) encodedValue).getValue().substring(1, ((TypeEncodedValue) encodedValue).getValue().length() - 1).replace('/', '.'));
                                    System.out.println("prepare class: " + ((TypeEncodedValue) encodedValue).getValue());
                                }
                            }
                        } else if (dexBackedAnnotationElement.getValue() instanceof DexBackedTypeEncodedValue) {
                            String value = ((DexBackedTypeEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
                            prepareclasses.add(value.substring(1, value.length() - 1).replace('/', '.'));
                            System.out.println("prepare class: " + value);
                        } else if (dexBackedAnnotationElement.getValue() instanceof DexBackedAnnotationEncodedValue) {
                            String value = ((DexBackedAnnotationEncodedValue) dexBackedAnnotationElement.getValue()).getType();
                        }
                    }
                }
            }
        }
    }
}
Also used : DexBackedAnnotationElement(org.jf.dexlib2.dexbacked.DexBackedAnnotationElement) DexBackedArrayEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedArrayEncodedValue) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue) DexBackedArrayEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedArrayEncodedValue) DexBackedAnnotationEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedAnnotationEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) DexBackedAnnotationEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedAnnotationEncodedValue) DexBackedAnnotation(org.jf.dexlib2.dexbacked.DexBackedAnnotation) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue)

Example 4 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project atlas by alibaba.

the class PatchFieldTool method addField.

public static void addField(String inFile, String outFile, DexBackedClassDef dexBackedClassDef, ImmutableField immutableField) throws IOException {
    DexFile dexFile = readDexFile(inFile);
    for (ClassDef classDef : dexFile.getClasses()) {
        if (dexBackedClassDef != null && dexBackedClassDef.getType().equals(classDef.getType())) {
            dexFile = addField(dexFile, classDef.getType(), immutableField);
        } else if (dexBackedClassDef == null) {
            dexFile = addField(dexFile, classDef.getType(), immutableField);
        }
    }
    reDexFile(dexFile);
    DexFileFactory.writeDexFile(outFile, new DexFile() {

        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<ClassDef>() {

                @Nonnull
                @Override
                public Iterator<ClassDef> iterator() {
                    return classes.iterator();
                }

                @Override
                public int size() {
                    return classes.size();
                }
            };
        }
    });
}
Also used : DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Nonnull(javax.annotation.Nonnull)

Example 5 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project atlas by alibaba.

the class SmaliDiffUtils method getBuildOption.

public static baksmaliOptions getBuildOption(Iterable<? extends ClassDef> collection, int apiLevel) {
    baksmaliOptions options = new baksmaliOptions();
    options.deodex = false;
    options.noParameterRegisters = false;
    options.useLocalsDirective = true;
    options.useSequentialLabels = true;
    options.outputDebugInfo = true;
    options.addCodeOffsets = false;
    options.jobs = -1;
    options.noAccessorComments = false;
    // 128
    options.registerInfo = 0;
    options.ignoreErrors = false;
    options.inlineResolver = null;
    options.apiLevel = apiLevel;
    options.checkPackagePrivateAccess = false;
    if (!options.noAccessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(collection);
    }
    return options;
}
Also used : org.jf.baksmali.baksmaliOptions(org.jf.baksmali.baksmaliOptions) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)47 DexFile (org.jf.dexlib2.iface.DexFile)23 Test (org.junit.Test)21 Method (org.jf.dexlib2.iface.Method)18 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)14 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)14 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)12 IndentingWriter (org.jf.util.IndentingWriter)11 File (java.io.File)10 IOException (java.io.IOException)10 HashSet (java.util.HashSet)8 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)8 Instruction (org.jf.dexlib2.iface.instruction.Instruction)8 Nonnull (javax.annotation.Nonnull)7 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6