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

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

The 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 org.antlr.runtime.RecognitionException;
import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.writer.builder.DexBuilder;
import org.jf.dexlib2.writer.io.FileDataStore;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

import brut.directory.ExtFile;

public class SmaliCompiler {
    private final File smaliFile;
    private final File mDexFile;
    private int mApiLevel = 0;
    private final static Logger LOGGER = Logger.getLogger(SmaliCompiler.class.getName());
    private SmaliCompiler(File smaliFile, File dexFile, int apiLevel) {
        this.smaliFile = smaliFile;
        mDexFile = dexFile;
        mApiLevel = apiLevel;
    }

    public static void build(File smaliFile, File dexFile, int apiLevel) throws Exception {
        new SmaliCompiler(smaliFile, dexFile, apiLevel).build();
    }

    private void build()   {
        try {
            DexBuilder dexBuilder;
            if (mApiLevel > 0) {
                dexBuilder = new DexBuilder(Opcodes.forApi(mApiLevel));
            } else {
                dexBuilder = new DexBuilder(Opcodes.getDefault());
            }
            buildFile(smaliFile, dexBuilder);
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    }

    private void buildFile(File smaliFile, DexBuilder dexBuilder) throws Exception {
        if(!smaliFile.getName().endsWith(".smali")){
            return;
        }
        InputStream inStream = new FileInputStream(smaliFile);
        try {
            if (!SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, mApiLevel, false, false)) {
                throw new Exception("Could not smali file: " + smaliFile.getAbsolutePath());
            }else {
                dexBuilder.writeTo(new FileDataStore( new File(mDexFile.getAbsolutePath())));
            }
        } catch (IOException | RecognitionException ex) {
            throw new Exception(ex);
        }
        inStream.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy