Apktool/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliBuilder.java

126 lines
4.4 KiB
Java
Raw Normal View History

2012-09-20 03:27:35 +02:00
/**
* Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
*
* 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 brut.androlib.src;
import brut.androlib.AndrolibException;
2013-06-24 18:46:26 +02:00
import brut.androlib.mod.SmaliMod;
2012-09-20 03:27:35 +02:00
import brut.androlib.res.util.ExtFile;
import brut.directory.DirectoryException;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Logger;
2013-06-24 18:46:26 +02:00
import org.antlr.runtime.RecognitionException;
2012-09-20 03:27:35 +02:00
import org.apache.commons.io.IOUtils;
2013-06-24 18:46:26 +02:00
import org.jf.dexlib2.writer.builder.DexBuilder;
2013-09-15 17:35:00 +02:00
import org.jf.dexlib2.writer.io.FileDataStore;
2012-09-20 03:27:35 +02:00
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/
public class SmaliBuilder {
public static void build(ExtFile smaliDir, File dexFile, HashMap<String, Boolean> flags)
throws AndrolibException {
2014-02-10 02:01:57 +01:00
new SmaliBuilder(smaliDir, dexFile, flags).build();
}
private SmaliBuilder(ExtFile smaliDir, File dexFile, HashMap<String, Boolean> flags) {
2014-02-10 02:01:57 +01:00
mSmaliDir = smaliDir;
mDexFile = dexFile;
mFlags = flags;
}
private void build() throws AndrolibException {
try {
2013-06-24 18:46:26 +02:00
DexBuilder dexBuilder = DexBuilder.makeDexBuilder();
for (String fileName : mSmaliDir.getDirectory().getFiles(true)) {
2014-02-10 02:01:57 +01:00
buildFile(fileName, dexBuilder);
}
2013-09-15 17:35:00 +02:00
dexBuilder.writeTo(new FileDataStore( new File(mDexFile.getAbsolutePath())));
2014-02-10 02:01:57 +01:00
} catch (IOException | DirectoryException ex) {
throw new AndrolibException(ex);
}
}
private void buildFile(String fileName, DexBuilder dexBuilder)
throws AndrolibException, IOException {
2014-02-10 02:01:57 +01:00
File inFile = new File(mSmaliDir, fileName);
InputStream inStream = new FileInputStream(inFile);
2014-02-10 02:01:57 +01:00
if (fileName.endsWith(".smali")) {
2013-06-24 18:46:26 +02:00
try {
if (!SmaliMod.assembleSmaliFile(inFile,dexBuilder, false, false)) {
throw new AndrolibException("Could not smali file: " + fileName);
}
} catch (IOException | RecognitionException ex) {
throw new AndrolibException(ex);
}
2014-02-10 02:01:57 +01:00
return;
}
if (!fileName.endsWith(".java")) {
LOGGER.warning("Unknown file type, ignoring: " + inFile);
return;
}
StringBuilder out = new StringBuilder();
List<String> lines = IOUtils.readLines(inStream);
if (!mFlags.get("debug")) {
final String[] linesArray = lines.toArray(new String[0]);
for (int i = 1; i < linesArray.length - 1; i++) {
out.append(linesArray[i].split("//", 2)[1]).append('\n');
}
} else {
lines.remove(lines.size() - 1);
ListIterator<String> it = lines.listIterator(1);
out.append(".source \"").append(inFile.getName()).append("\"\n");
while (it.hasNext()) {
String line = it.next().split("//", 2)[1].trim();
if (line.isEmpty() || line.charAt(0) == '#' || line.startsWith(".source")) {
2014-02-10 02:01:57 +01:00
continue;
}
if (line.startsWith(".method ")) {
it.previous();
DebugInjector.inject(it, out);
continue;
}
out.append(line).append('\n');
}
}
try {
if (!SmaliMod.assembleSmaliFile(out.toString(),dexBuilder, false, false, inFile)) {
throw new AndrolibException("Could not smali file: " + fileName);
}
} catch (IOException | RecognitionException ex) {
throw new AndrolibException(ex);
}
2014-02-10 02:01:57 +01:00
}
2014-02-10 02:01:57 +01:00
private final ExtFile mSmaliDir;
private final File mDexFile;
private final HashMap<String, Boolean> mFlags;
private final static Logger LOGGER = Logger.getLogger(SmaliBuilder.class.getName());
2012-09-20 03:27:35 +02:00
}