All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.faker.android.SmaliBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.38
Show newest version
/*
 *  Copyright (C) 2010 Ryszard Wiśniewski 
 *  Copyright (C) 2010 Connor Tumbleson 
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.faker.android;

import java.io.*;
import java.util.List;
import java.util.logging.Logger;

import org.antlr.runtime.RecognitionException;
import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.writer.builder.DexBuilder;
import org.jf.dexlib2.writer.io.FileDataStore;

import brut.directory.ExtFile;

public class SmaliBuilder {
    private final ExtFile mSmaliDir;
    private final File mDexFile;
    private int mApiLevel = 0;
    private List classExclusions;

    private final static Logger LOGGER = Logger.getLogger(SmaliBuilder.class.getName());

    public static void build(ExtFile smaliDir, File dexFile, int apiLevel,List classExclusions) throws Exception {
        new SmaliBuilder(smaliDir, dexFile, apiLevel,classExclusions).build();
    }


    public static void build(ExtFile smaliDir, File dexFile) throws Exception {
        new SmaliBuilder(smaliDir, dexFile, 0,null).build();
    }

    private SmaliBuilder(ExtFile smaliDir, File dexFile, int apiLevel,List classExclusions) {
        mSmaliDir = smaliDir;
        mDexFile = dexFile;
        mApiLevel = apiLevel;
        this.classExclusions = classExclusions;
    }

    private void build()   {
        try {
            DexBuilder dexBuilder;
            if (mApiLevel > 0) {
                dexBuilder = new DexBuilder(Opcodes.forApi(mApiLevel));
            } else {
                dexBuilder = new DexBuilder(Opcodes.getDefault());
            }

            for (String fileName : mSmaliDir.getDirectory().getFiles(true)) {
                if(classExclusions!=null){
                    if(isExclude(classExclusions,fileName)){
                        continue;
                    }
                }
                buildFile(fileName, dexBuilder);
            }
            dexBuilder.writeTo(new FileDataStore( new File(mDexFile.getAbsolutePath())));
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    }

    private boolean isExclude(List classExclusions,String fileName){
        for (ClassExclusion classExclusion: classExclusions) {
            if(classExclusion.exclude(fileName)){
                return true;
            }
        }
        return false;
    };

    private void buildFile(String fileName, DexBuilder dexBuilder)
            throws Exception {
        File inFile = new File(mSmaliDir, fileName);
        InputStream inStream = new FileInputStream(inFile);

        if (fileName.endsWith(".smali")) {
            try {
                if (!SmaliMod.assembleSmaliFile(inFile, dexBuilder, mApiLevel, false, false)) {
                    throw new Exception("Could not smali file: " + fileName);
                }
            } catch (IOException | RecognitionException ex) {
                throw new Exception(ex);
            }
        } else {
            LOGGER.warning("Unknown file type, ignoring: " + inFile);
        }
        inStream.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy