2019-10-16 07:49:58 +02:00
|
|
|
/*
|
2020-04-11 12:33:05 +02:00
|
|
|
* Copyright (C) 2010 Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
* Copyright (C) 2010 Connor Tumbleson <connor.tumbleson@gmail.com>
|
2012-09-20 03:27:35 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2021-08-24 15:31:41 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2012-09-20 03:27:35 +02:00
|
|
|
*
|
|
|
|
* 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.directory.DirectoryException;
|
2021-03-07 21:06:45 +01:00
|
|
|
import brut.directory.ExtFile;
|
2013-06-24 18:46:26 +02:00
|
|
|
import org.antlr.runtime.RecognitionException;
|
2023-04-29 01:30:06 +02:00
|
|
|
import com.android.tools.smali.dexlib2.Opcodes;
|
|
|
|
import com.android.tools.smali.dexlib2.writer.builder.DexBuilder;
|
|
|
|
import com.android.tools.smali.dexlib2.writer.io.FileDataStore;
|
2021-03-07 21:06:45 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2012-09-20 03:27:35 +02:00
|
|
|
public class SmaliBuilder {
|
2021-07-03 22:05:04 +02:00
|
|
|
|
2016-09-05 11:52:58 +02:00
|
|
|
public static void build(ExtFile smaliDir, File dexFile, int apiLevel) throws AndrolibException {
|
|
|
|
new SmaliBuilder(smaliDir, dexFile, apiLevel).build();
|
|
|
|
}
|
2012-09-20 03:27:35 +02:00
|
|
|
|
2016-09-05 11:52:58 +02:00
|
|
|
private SmaliBuilder(ExtFile smaliDir, File dexFile, int apiLevel) {
|
2014-02-10 02:01:57 +01:00
|
|
|
mSmaliDir = smaliDir;
|
|
|
|
mDexFile = dexFile;
|
2016-09-05 11:52:58 +02:00
|
|
|
mApiLevel = apiLevel;
|
2014-02-10 02:01:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void build() throws AndrolibException {
|
|
|
|
try {
|
2016-09-05 11:52:58 +02:00
|
|
|
DexBuilder dexBuilder;
|
|
|
|
if (mApiLevel > 0) {
|
2017-03-27 15:16:11 +02:00
|
|
|
dexBuilder = new DexBuilder(Opcodes.forApi(mApiLevel));
|
2016-09-05 11:52:58 +02:00
|
|
|
} else {
|
2017-03-27 15:16:11 +02:00
|
|
|
dexBuilder = new DexBuilder(Opcodes.getDefault());
|
2016-09-05 11:52:58 +02:00
|
|
|
}
|
2013-06-24 18:46:26 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-02-13 04:12:17 +01:00
|
|
|
|
2014-08-16 15:29:57 +02:00
|
|
|
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);
|
2013-02-13 04:12:17 +01:00
|
|
|
|
2014-02-10 02:01:57 +01:00
|
|
|
if (fileName.endsWith(".smali")) {
|
2013-06-24 18:46:26 +02:00
|
|
|
try {
|
2019-01-31 13:18:14 +01:00
|
|
|
if (!SmaliMod.assembleSmaliFile(inFile, dexBuilder, mApiLevel, false, false)) {
|
2013-06-24 18:46:26 +02:00
|
|
|
throw new AndrolibException("Could not smali file: " + fileName);
|
|
|
|
}
|
|
|
|
} catch (IOException | RecognitionException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
2016-03-10 15:29:05 +01:00
|
|
|
} else {
|
2014-02-10 02:01:57 +01:00
|
|
|
LOGGER.warning("Unknown file type, ignoring: " + inFile);
|
|
|
|
}
|
2016-02-15 02:34:06 +01:00
|
|
|
inStream.close();
|
2014-02-10 02:01:57 +01:00
|
|
|
}
|
2013-02-13 04:12:17 +01:00
|
|
|
|
2014-02-10 02:01:57 +01:00
|
|
|
private final ExtFile mSmaliDir;
|
|
|
|
private final File mDexFile;
|
2021-07-03 20:26:16 +02:00
|
|
|
private final int mApiLevel;
|
2013-02-13 04:12:17 +01:00
|
|
|
|
2014-08-16 15:29:57 +02:00
|
|
|
private final static Logger LOGGER = Logger.getLogger(SmaliBuilder.class.getName());
|
2012-09-20 03:27:35 +02:00
|
|
|
}
|