com.android.tools.r8.CompilationMode Java Examples

The following examples show how to use com.android.tools.r8.CompilationMode. 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: CompatDexBuilder.java    From bazel with Apache License 2.0 6 votes vote down vote up
private DexConsumer dexEntry(ZipFile zipFile, ZipEntry classEntry, ExecutorService executor)
    throws IOException, CompilationFailedException {
  DexConsumer consumer = new DexConsumer();
  D8Command.Builder builder = D8Command.builder();
  builder
      .setProgramConsumer(consumer)
      .setMode(noLocals ? CompilationMode.RELEASE : CompilationMode.DEBUG)
      .setMinApiLevel(13) // H_MR2.
      .setDisableDesugaring(true);
  if (backportStatics) {
    CompatDxSupport.enableDesugarBackportStatics(builder);
  }
  try (InputStream stream = zipFile.getInputStream(classEntry)) {
    builder.addClassProgramData(
        ByteStreams.toByteArray(stream),
        new ArchiveEntryOrigin(
            classEntry.getName(), new PathOrigin(Paths.get(zipFile.getName()))));
  }
  D8.run(builder.build(), executor);
  return consumer;
}
 
Example #2
Source File: D8DexMerger.java    From bundletool with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableList<Path> merge(
    ImmutableList<Path> dexFiles,
    Path outputDir,
    Optional<Path> mainDexListFile,
    boolean isDebuggable,
    int minSdkVersion) {

  try {
    validateInput(dexFiles, outputDir);

    // Many of the D8 parameters are not being set because those are used when compiling into dex,
    // however we are merging existing dex files. The parameters considered are:
    // - classpathFiles, libraryFiles: Required for desugaring during compilation.
    D8Command.Builder command =
        D8Command.builder()
            .setOutput(outputDir, OutputMode.DexIndexed)
            .addProgramFiles(dexFiles)
            .setMinApiLevel(minSdkVersion)
            // Compilation mode affects whether D8 produces minimal main-dex.
            // In debug mode minimal main-dex is always produced, so that the validity of the
            // main-dex can be debugged. For release mode minimal main-dex is not produced and the
            // primary dex file will be filled as appropriate.
            .setMode(isDebuggable ? CompilationMode.DEBUG : CompilationMode.RELEASE);
    mainDexListFile.ifPresent(command::addMainDexListFiles);

    // D8 throws when main dex list is not provided and the merge result doesn't fit into a single
    // dex file.
    D8.run(command.build());

    File[] mergedFiles = outputDir.toFile().listFiles();

    return Arrays.stream(mergedFiles).map(File::toPath).collect(toImmutableList());

  } catch (CompilationFailedException e) {
    throw translateD8Exception(e);
  }
}
 
Example #3
Source File: DexMergeTransformCallable.java    From atlas with Apache License 2.0 5 votes vote down vote up
@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 #4
Source File: D8Converter.java    From jadx with Apache License 2.0 5 votes vote down vote up
public static void run(Path path, Path tempDirectory) throws CompilationFailedException {
	D8Command d8Command = D8Command.builder(new LogHandler())
			.addProgramFiles(path)
			.setOutput(tempDirectory, OutputMode.DexIndexed)
			.setMode(CompilationMode.DEBUG)
			.setMinApiLevel(30)
			.setIntermediate(true)
			.setEnableDesugaring(false)
			.build();
	D8.run(d8Command);
}