com.android.tools.r8.CompilationFailedException Java Examples

The following examples show how to use com.android.tools.r8.CompilationFailedException. 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: D8DexMerger.java    From bundletool with Apache License 2.0 6 votes vote down vote up
private static CommandExecutionException translateD8Exception(
    CompilationFailedException d8Exception) {
  // DexOverflowException is thrown when the merged result doesn't fit into a single dex file and
  // `mainDexClasses` is empty. Detection of the exception in the stacktrace is non-trivial and at
  // the time of writing this code it is suppressed exception of the root cause.
  if (ThrowableUtils.anyInCausalChainOrSuppressedMatches(
      d8Exception, t -> t.getMessage() != null && t.getMessage().contains(DEX_OVERFLOW_MSG))) {
    return CommandExecutionException.builder()
        .withInternalMessage(
            "Dex merging failed because the result does not fit into a single dex file and"
                + " multidex is not supported by the input.")
        .withCause(d8Exception)
        .build();
  } else {
    return CommandExecutionException.builder()
        .withInternalMessage("Dex merging failed.")
        .withCause(d8Exception)
        .build();
  }
}
 
Example #2
Source File: Desugar.java    From bazel with Apache License 2.0 6 votes vote down vote up
private void desugar(
    List<ClassFileResourceProvider> bootclasspathProviders,
    ClassFileResourceProvider classpath,
    Path input,
    Path output)
    throws CompilationFailedException {
  checkArgument(!Files.isDirectory(input), "Input must be a jar (%s is a directory)", input);
  DependencyCollector dependencyCollector = createDependencyCollector();
  OutputConsumer consumer = new OutputConsumer(output, dependencyCollector);
  D8Command.Builder builder =
      D8Command.builder(new DesugarDiagnosticsHandler(consumer))
          .addClasspathResourceProvider(classpath)
          .addProgramFiles(input)
          .setMinApiLevel(options.minSdkVersion)
          .setProgramConsumer(consumer);
  bootclasspathProviders.forEach(builder::addLibraryResourceProvider);
  D8.run(builder.build());
}
 
Example #3
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 #4
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 #5
Source File: DexFileMerger.java    From bazel with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws CompilationFailedException {
  try {
    if (PRINT_ARGS) {
      printArgs(args);
    }
    run(args);
  } catch (CompilationFailedException | IOException e) {
    System.err.println("Merge failed: " + e.getMessage());
    throw new CompilationFailedException("Merge failed: " + e.getMessage());
  }
}
 
Example #6
Source File: DexFileMergerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private Path compileTwoClasses(OutputMode outputMode, boolean addMarker)
    throws CompilationFailedException, IOException {
  // Compile Class1 and Class2.
  Path output = temp.newFolder().toPath().resolve("compiled.zip");
  D8Command command =
      D8Command.builder()
          .setOutput(output, outputMode)
          .addProgramFiles(Paths.get(class2Class))
          .addProgramFiles(Paths.get(class1Class))
          .build();

  DexFileMergerHelper.runD8ForTesting(command, !addMarker);

  return output;
}
 
Example #7
Source File: DexFileMergerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void mergeTwoFiles() throws CompilationFailedException, IOException {
  Path mergerInputZip = compileTwoClasses(OutputMode.DexFilePerClassFile, false);

  Path mergerOutputZip = temp.getRoot().toPath().resolve("merger-out.zip");
  DexFileMerger.main(
      new String[] {
        "--input", mergerInputZip.toString(), "--output", mergerOutputZip.toString()
      });

  // TODO(sgjesse): Validate by running methods of Class1 and Class2.
  // https://r8.googlesource.com/r8/+/5ee92486c896b918efb62e69bff5dfa79f30e7c2/src/test/java/com/android/tools/r8/dexfilemerger/DexFileMergerTests.java#103
}
 
Example #8
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);
}
 
Example #9
Source File: DxStep.java    From buck with Apache License 2.0 4 votes vote down vote up
private boolean isOverloadedDexException(CompilationFailedException e) {
  return e.getCause() instanceof AbortException
      && e.getCause().getMessage().contains("Cannot fit requested classes in a single dex file");
}