com.android.dx.command.dexer.DxContext Java Examples
The following examples show how to use
com.android.dx.command.dexer.DxContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaToDex.java From Box with Apache License 2.0 | 6 votes |
public List<Path> convert(Path jar) throws JadxException { try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream errOut = new ByteArrayOutputStream()) { DxContext context = new DxContext(out, errOut); Path dir = FileUtils.createTempDir("jar-to-dex-"); DxArgs args = new DxArgs( context, dir.toAbsolutePath().toString(), new String[] { jar.toAbsolutePath().toString() }); int result = new Main(context).runDx(args); dxErrors = errOut.toString(CHARSET_NAME); if (result != 0) { throw new JadxException("Java to dex conversion error, code: " + result); } List<Path> list = new ArrayList<>(); try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) { for (Path child : ds) { list.add(child); child.toFile().deleteOnExit(); } } return list; } catch (Exception e) { throw new JadxException("dx exception: " + e.getMessage(), e); } }
Example #2
Source File: DxConverter.java From jadx with Apache License 2.0 | 6 votes |
public static void run(Path path, Path tempDirectory) { int result; String dxErrors; try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream errOut = new ByteArrayOutputStream()) { DxContext context = new DxContext(out, errOut); DxArgs args = new DxArgs( context, tempDirectory.toAbsolutePath().toString(), new String[] { path.toAbsolutePath().toString() }); result = new Main(context).runDx(args); dxErrors = errOut.toString(CHARSET_NAME); } catch (Exception e) { throw new RuntimeException("dx exception: " + e.getMessage(), e); } if (result != 0) { throw new RuntimeException("Java to dex conversion error, code: " + result + "\n errors: " + dxErrors); } }
Example #3
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testMonodex_alwaysWritesSingleShard() throws Exception { DexFileAggregator dexer = new DexFileAggregator( new DxContext(), dest, newDirectExecutorService(), MultidexStrategy.OFF, /*forceJumbo=*/ false, 2 /* dex has more than 2 methods and fields */, WASTE, DexFileMergerTest.DEX_PREFIX); Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class)); dexer.add(dex); dexer.add(dex2); verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class)); dexer.close(); verify(dest).addFile(any(ZipEntry.class), written.capture()); assertThat(Iterables.size(written.getValue().classDefs())).isEqualTo(2); }
Example #4
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testMultidex_overLimitWritesSecondShard() throws Exception { DexFileAggregator dexer = new DexFileAggregator( new DxContext(), dest, newDirectExecutorService(), MultidexStrategy.BEST_EFFORT, /*forceJumbo=*/ false, 2 /* dex has more than 2 methods and fields */, WASTE, DexFileMergerTest.DEX_PREFIX); Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class)); dexer.add(dex); // classFile is already over limit but we take anything in empty shard dexer.add(dex2); // this should start a new shard // Make sure there was one file written and that file is dex verify(dest).addFile(any(ZipEntry.class), written.capture()); assertThat(written.getValue()).isSameInstanceAs(dex); dexer.close(); verify(dest).addFile(any(ZipEntry.class), eq(dex2)); }
Example #5
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testMultidex_underLimitWritesOneShard() throws Exception { DexFileAggregator dexer = new DexFileAggregator( new DxContext(), dest, newDirectExecutorService(), MultidexStrategy.BEST_EFFORT, /*forceJumbo=*/ false, DEX_LIMIT, WASTE, DexFileMergerTest.DEX_PREFIX); Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class)); dexer.add(dex); dexer.add(dex2); verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class)); dexer.close(); verify(dest).addFile(any(ZipEntry.class), written.capture()); assertThat(Iterables.size(written.getValue().classDefs())).isEqualTo(2); }
Example #6
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testAddAndClose_forceJumboRewrites() throws Exception { DexFileAggregator dexer = new DexFileAggregator( new DxContext(), dest, newDirectExecutorService(), MultidexStrategy.MINIMAL, /*forceJumbo=*/ true, 0, WASTE, DexFileMergerTest.DEX_PREFIX); dexer.add(dex); try { dexer.close(); } catch (IllegalStateException e) { assertThat(e).hasMessageThat().isEqualTo("--forceJumbo flag not supported"); System.err.println("Skipping this test due to missing --forceJumbo support in Android SDK."); e.printStackTrace(); return; } verify(dest).addFile(any(ZipEntry.class), written.capture()); assertThat(written.getValue()).isNotEqualTo(dex); assertThat(written.getValue().getLength()).isGreaterThan(dex.getLength()); }
Example #7
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testAddAndClose_singleInputWritesThatInput() throws Exception { DexFileAggregator dexer = new DexFileAggregator( new DxContext(), dest, newDirectExecutorService(), MultidexStrategy.MINIMAL, /*forceJumbo=*/ false, 0, WASTE, DexFileMergerTest.DEX_PREFIX); dexer.add(dex); dexer.close(); verify(dest).addFile(any(ZipEntry.class), eq(dex)); }
Example #8
Source File: DexFileAggregator.java From bazel with Apache License 2.0 | 6 votes |
public DexFileAggregator( DxContext context, DexFileArchive dest, ListeningExecutorService executor, MultidexStrategy multidex, boolean forceJumbo, int maxNumberOfIdxPerDex, int wasteThresholdPerDex, String dexPrefix) { this.context = context; this.dest = dest; this.executor = executor; this.multidex = multidex; this.forceJumbo = forceJumbo; this.wasteThresholdPerDex = wasteThresholdPerDex; this.dexPrefix = dexPrefix; tracker = new DexLimitTracker(maxNumberOfIdxPerDex); }
Example #9
Source File: DexBuilder.java From bazel with Apache License 2.0 | 6 votes |
private static void processRequest( ExecutorService executor, Cache<DexingKey, byte[]> dexCache, DxContext context, List<String> args) throws OptionsParsingException, IOException, InterruptedException, ExecutionException { OptionsParser optionsParser = OptionsParser.builder() .optionsClasses(Options.class, DexingOptions.class) .allowResidue(false) .build(); optionsParser.parse(args); Options options = optionsParser.getOptions(Options.class); try (ZipFile in = new ZipFile(options.inputJar.toFile()); ZipOutputStream out = createZipOutputStream(options.outputZip)) { produceDexArchive( in, out, executor, /*convertOnReaderThread*/ false, new Dexing(context, optionsParser.getOptions(DexingOptions.class)), dexCache); } }
Example #10
Source File: BaseAndroidClassLoader.java From rhino-android with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public Class<?> defineClass(String name, byte[] data) { try { DexOptions dexOptions = new DexOptions(); DexFile dexFile = new DexFile(dexOptions); DirectClassFile classFile = new DirectClassFile(data, name.replace('.', '/') + ".class", true); classFile.setAttributeFactory(StdAttributeFactory.THE_ONE); classFile.getMagic(); DxContext context = new DxContext(); dexFile.add(CfTranslator.translate(context, classFile, null, new CfOptions(), dexOptions, dexFile)); Dex dex = new Dex(dexFile.toDex(null, false)); Dex oldDex = getLastDex(); if (oldDex != null) { dex = new DexMerger(new Dex[]{dex, oldDex}, CollisionPolicy.KEEP_FIRST, context).merge(); } return loadClass(dex, name); } catch (IOException | ClassNotFoundException e) { throw new FatalLoadingException(e); } }
Example #11
Source File: JavaToDex.java From Box with Apache License 2.0 | 6 votes |
public List<Path> convert(Path jar) throws JadxException { try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream errOut = new ByteArrayOutputStream()) { DxContext context = new DxContext(out, errOut); Path dir = FileUtils.createTempDir("jar-to-dex-"); DxArgs args = new DxArgs( context, dir.toAbsolutePath().toString(), new String[] { jar.toAbsolutePath().toString() }); int result = new Main(context).runDx(args); dxErrors = errOut.toString(CHARSET_NAME); if (result != 0) { throw new JadxException("Java to dex conversion error, code: " + result); } List<Path> list = new ArrayList<>(); try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) { for (Path child : ds) { list.add(child); child.toFile().deleteOnExit(); } } return list; } catch (Exception e) { throw new JadxException("dx exception: " + e.getMessage(), e); } }
Example #12
Source File: DexLimitTrackerTest.java From bazel with Apache License 2.0 | 5 votes |
private static DexFile convertClass(Class<?> clazz) throws IOException { String path = clazz.getName().replace('.', '/') + ".class"; try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) { return new DexConverter(new Dexing(new DxContext(), new DexOptions(), new CfOptions())) .toDexFile(ByteStreams.toByteArray(in), path); } }
Example #13
Source File: CfTranslator.java From Box with Apache License 2.0 | 5 votes |
/** * Helper that updates the dex statistics. */ private static void updateDexStatistics(DxContext context, CfOptions cfOptions, DexOptions dexOptions, RopMethod optRmeth, RopMethod nonOptRmeth, LocalVariableInfo locals, int paramSize, int originalByteCount) { /* * Run rop->dex again on optimized vs. non-optimized method to * collect statistics. We have to totally convert both ways, * since converting the "real" method getting added to the * file would corrupt it (by messing with its constant pool * indices). */ DalvCode optCode = RopTranslator.translate(optRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions); DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions); /* * Fake out the indices, so code.getInsns() can work well enough * for the current purpose. */ DalvCode.AssignIndicesCallback callback = new DalvCode.AssignIndicesCallback() { @Override public int getIndex(Constant cst) { // Everything is at index 0! return 0; } }; optCode.assignIndices(callback); nonOptCode.assignIndices(callback); context.codeStatistics.updateDexStatistics(nonOptCode, optCode); context.codeStatistics.updateOriginalByteCount(originalByteCount); }
Example #14
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 5 votes |
private static DexFile convertClass(Class<?> clazz) throws IOException { String path = clazz.getName().replace('.', '/') + ".class"; try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) { return new DexConverter(new Dexing(new DxContext(), new DexOptions(), new CfOptions())) .toDexFile(ByteStreams.toByteArray(in), path); } }
Example #15
Source File: DexFileAggregatorTest.java From bazel with Apache License 2.0 | 5 votes |
@Test public void testClose_emptyWritesNothing() throws Exception { DexFileAggregator dexer = new DexFileAggregator( new DxContext(), dest, newDirectExecutorService(), MultidexStrategy.MINIMAL, /*forceJumbo=*/ false, DEX_LIMIT, WASTE, DexFileMergerTest.DEX_PREFIX); dexer.close(); verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class)); }
Example #16
Source File: DexFileMergerTest.java From bazel with Apache License 2.0 | 5 votes |
private Path buildDexArchive(Path inputJar, String outputZip) throws Exception { DexBuilder.Options options = new DexBuilder.Options(); options.inputJar = inputJar; options.outputZip = FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR"), outputZip); options.maxThreads = 1; Dexing.DexingOptions dexingOptions = new Dexing.DexingOptions(); dexingOptions.optimize = true; dexingOptions.positionInfo = PositionList.LINES; DexBuilder.buildDexArchive(options, new Dexing(new DxContext(), dexingOptions)); return options.outputZip; }
Example #17
Source File: DexFileSplitterTest.java From bazel with Apache License 2.0 | 5 votes |
private Path buildDexArchive(Path inputJar, String outputZip) throws Exception { DexBuilder.Options options = new DexBuilder.Options(); // Use Jar file that has this test in it as the input Jar options.inputJar = inputJar; options.outputZip = FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR"), outputZip); options.maxThreads = 1; Dexing.DexingOptions dexingOptions = new Dexing.DexingOptions(); dexingOptions.optimize = true; dexingOptions.positionInfo = PositionList.LINES; DexBuilder.buildDexArchive(options, new Dexing(new DxContext(), dexingOptions)); return options.outputZip; }
Example #18
Source File: DexConversionEnqueuerTest.java From bazel with Apache License 2.0 | 5 votes |
private void makeStuffer() { stuffer = new DexConversionEnqueuer( zip, newDirectExecutorService(), new DexConverter(new Dexing(new DxContext(), new DexOptions(), new CfOptions())), cache); }
Example #19
Source File: Dexing.java From bazel with Apache License 2.0 | 5 votes |
public CfOptions toCfOptions(DxContext context) { CfOptions result = new CfOptions(); result.localInfo = this.localInfo; result.optimize = this.optimize; result.warn = printWarnings ? context.err : Dexing.nullout; // Use dx's defaults result.optimizeListFile = null; result.dontOptimizeListFile = null; result.positionInfo = positionInfo; result.strictNameCheck = true; result.statistics = false; // we're not supporting statistics anyways return result; }
Example #20
Source File: DexFileMerger.java From bazel with Apache License 2.0 | 5 votes |
private static DexFileAggregator createDexFileAggregator( Options options, ListeningExecutorService executor) throws IOException { String filePrefix = options.dexPrefix; if (options.multidexMode == MultidexStrategy.GIVEN_SHARD) { checkArgument(options.inputArchives.size() == 1, "--multidex=given_shard requires exactly one --input"); Pattern namingPattern = Pattern.compile("([0-9]+)\\..*"); Matcher matcher = namingPattern.matcher(options.inputArchives.get(0).toFile().getName()); checkArgument(matcher.matches(), "expect input named <N>.xxx.zip for --multidex=given_shard but got %s", options.inputArchives.get(0).toFile().getName()); int shard = Integer.parseInt(matcher.group(1)); checkArgument(shard > 0, "expect positive N in input named <N>.xxx.zip but got %s", shard); if (shard > 1) { // first shard conventionally isn't numbered filePrefix += shard; } } return new DexFileAggregator( new DxContext(options.verbose ? System.out : ByteStreams.nullOutputStream(), System.err), new DexFileArchive( new ZipOutputStream( new BufferedOutputStream(Files.newOutputStream(options.outputArchive)))), executor, options.multidexMode, options.forceJumbo, options.maxNumberOfIdxPerDex, options.wasteThresholdPerDex, filePrefix); }
Example #21
Source File: DexMerger.java From Box with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { if (args.length < 2) { printUsage(); return; } Dex[] dexes = new Dex[args.length - 1]; for (int i = 1; i < args.length; i++) { dexes[i - 1] = new Dex(new File(args[i])); } Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge(); merged.writeTo(new File(args[0])); }
Example #22
Source File: JavaToDex.java From Box with Apache License 2.0 | 5 votes |
public DxArgs(DxContext context, String dexDir, String[] input) { super(context); outName = dexDir; fileNames = input; jarOutput = false; multiDex = true; optimize = true; localInfo = true; coreLibrary = true; debug = true; warnings = true; minSdkVersion = 28; }
Example #23
Source File: CfTranslator.java From J2ME-Loader with Apache License 2.0 | 5 votes |
/** * Helper that updates the dex statistics. */ private static void updateDexStatistics(DxContext context, CfOptions cfOptions, DexOptions dexOptions, RopMethod optRmeth, RopMethod nonOptRmeth, LocalVariableInfo locals, int paramSize, int originalByteCount) { /* * Run rop->dex again on optimized vs. non-optimized method to * collect statistics. We have to totally convert both ways, * since converting the "real" method getting added to the * file would corrupt it (by messing with its constant pool * indices). */ DalvCode optCode = RopTranslator.translate(optRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions); DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions); /* * Fake out the indices, so code.getInsns() can work well enough * for the current purpose. */ DalvCode.AssignIndicesCallback callback = new DalvCode.AssignIndicesCallback() { @Override public int getIndex(Constant cst) { // Everything is at index 0! return 0; } }; optCode.assignIndices(callback); nonOptCode.assignIndices(callback); context.codeStatistics.updateDexStatistics(nonOptCode, optCode); context.codeStatistics.updateOriginalByteCount(originalByteCount); }
Example #24
Source File: DxConverter.java From jadx with Apache License 2.0 | 5 votes |
public DxArgs(DxContext context, String dexDir, String[] input) { super(context); outName = dexDir; fileNames = input; jarOutput = false; multiDex = true; optimize = true; localInfo = true; coreLibrary = true; debug = true; warnings = true; minSdkVersion = 28; }
Example #25
Source File: JavaToDex.java From Box with Apache License 2.0 | 5 votes |
public DxArgs(DxContext context, String dexDir, String[] input) { super(context); outName = dexDir; fileNames = input; jarOutput = false; multiDex = true; optimize = true; localInfo = true; coreLibrary = true; debug = true; warnings = true; minSdkVersion = 28; }
Example #26
Source File: CfTranslator.java From buck with Apache License 2.0 | 5 votes |
/** * Helper that updates the dex statistics. */ private static void updateDexStatistics(DxContext context, CfOptions cfOptions, DexOptions dexOptions, RopMethod optRmeth, RopMethod nonOptRmeth, LocalVariableInfo locals, int paramSize, int originalByteCount) { /* * Run rop->dex again on optimized vs. non-optimized method to * collect statistics. We have to totally convert both ways, * since converting the "real" method getting added to the * file would corrupt it (by messing with its constant pool * indices). */ DalvCode optCode = RopTranslator.translate(optRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions); DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth, cfOptions.positionInfo, locals, paramSize, dexOptions); /* * Fake out the indices, so code.getInsns() can work well enough * for the current purpose. */ DalvCode.AssignIndicesCallback callback = new DalvCode.AssignIndicesCallback() { public int getIndex(Constant cst) { // Everything is at index 0! return 0; } }; optCode.assignIndices(callback); nonOptCode.assignIndices(callback); context.codeStatistics.updateDexStatistics(nonOptCode, optCode); context.codeStatistics.updateOriginalByteCount(originalByteCount); }
Example #27
Source File: DexArchiveMergerHook.java From atlas with Apache License 2.0 | 5 votes |
public DexArchiveMergerHook( @NonNull DxContext dxContext, @NonNull DexMergingStrategy mergingStrategy, @NonNull ForkJoinPool forkJoinPool) { this.dxContext = dxContext; this.mergingStrategy = mergingStrategy; this.forkJoinPool = forkJoinPool; }
Example #28
Source File: DexMergeTransformCallable.java From atlas with Apache License 2.0 | 5 votes |
@Override public Void call() throws Exception { DexArchiveMerger merger; switch (dexMerger) { case DX: DxContext dxContext = new DxContext( processOutput.getStandardOutput(), processOutput.getErrorOutput()); merger = DexArchiveMerger.createDxDexMerger(dxContext,forkJoinPool); ReflectUtils.updateField(merger,"mergingStrategy",new AtlasDexArchiveMerger.AtlasDexRefMergingStrategy()); // merger = new DexArchiveMergerHook(dxContext,new AtlasDexArchiveMerger.AtlasDexRefMergingStrategy(),forkJoinPool); break; case D8: int d8MinSdkVersion = minSdkVersion; if (d8MinSdkVersion < 21 && dexingType == DexingType.NATIVE_MULTIDEX) { // D8 has baked-in logic that does not allow multiple dex files without // main dex list if min sdk < 21. When we deploy the app to a device with api // level 21+, we will promote legacy multidex to native multidex, but the min // sdk version will be less than 21, which will cause D8 failure as we do not // supply the main dex list. In order to prevent that, it is safe to set min // sdk version to 21. d8MinSdkVersion = 21; } merger = new AtlasD8Merger( processOutput.getErrorOutput(), d8MinSdkVersion, isDebuggable ? CompilationMode.DEBUG : CompilationMode.RELEASE); break; default: throw new AssertionError("Unknown dex merger " + dexMerger.name()); } merger.mergeDexArchives(dexArchives, dexOutputDir.toPath(), mainDexList, dexingType); return null; }
Example #29
Source File: AtlasDexArchiveBuilderTransform.java From atlas with Apache License 2.0 | 5 votes |
private DexArchiveBuilder getDexArchiveBuilder( int minSdkVersion, List<String> dexAdditionalParameters, int inBufferSize, int outBufferSize, DexerTool dexer, boolean isDebuggable, boolean awb, OutputStream outStream, OutputStream errStream) throws IOException { DexArchiveBuilder dexArchiveBuilder; switch (dexer) { case DX: boolean optimizedDex = !dexAdditionalParameters.contains("--no-optimize"); DxContext dxContext = new DxContext(outStream, errStream); DexArchiveBuilderConfig config = new DexArchiveBuilderConfig( dxContext, optimizedDex, inBufferSize, minSdkVersion, DexerTool.DX, outBufferSize, AtlasDexArchiveBuilderCacheHander.isJumboModeEnabledForDx()); dexArchiveBuilder = DexArchiveBuilder.createDxDexBuilder(config); break; case D8: dexArchiveBuilder = new AtlasD8DexArchiveBuilder(minSdkVersion,isDebuggable,AtlasDexArchiveBuilderTransform.this.variantContext.getScope().getMainDexListFile().toPath(),awb); break; default: throw new AssertionError("Unknown dexer type: " + dexer.name()); } return dexArchiveBuilder; }
Example #30
Source File: MainActivity.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
public DexFile generateDexFile(List<byte[]> classByteCodes){ DxContext dxContext=new DxContext(); DexOptions dexOptions=new DexOptions(); CfOptions cfOptions=new CfOptions(); DexFile dexFile=new DexFile(dexOptions); for (byte[] cls:classByteCodes){ DirectClassFile directClassFile=new DirectClassFile(cls,"",false); directClassFile.setAttributeFactory(StdAttributeFactory.THE_ONE); directClassFile.getMagic(); dexFile.add(CfTranslator.translate(dxContext,directClassFile,cls,cfOptions,dexOptions,dexFile)); } return dexFile; }