Search code examples
androidbazelandroid-d8android-r8

How do you set command line flags for d8 in a Bazel project?


For example if I wanted to set --min-api to 26: https://r8.googlesource.com/r8/+/b9cb6ae34047f19320885d9e2c594f81364fa896/src/main/java/com/android/tools/r8/D8CommandParser.java#120

I've tried various .bazelrc combinations with Bazel's -s flag on to see the subcommands called with their flags and I don't see anything being added as I'd would like:

  • build:d8 --define=min-api=26
  • build:d8 --min-api=26
  • build:d8 --min-api 26

Ideally though this would be set somewhere in a BUILD file so it's configurable per target.


Solution

  • Unfortunately this doesn't seem possible. Bazel uses CompatDexBuilder while we remove the remaining dex support code:

    https://github.com/bazelbuild/bazel/blob/d1ff8c2d15454444094f94765d4b57ce0232bb51/tools/android/BUILD.tools#L42

    and CompatDexBuilder appears to hardcode the min api level:

    https://r8.googlesource.com/r8/+/41d2ea5adff39b579b297219310199fd566e778b/src/main/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilder.java#169

    H_MR2 corresponds to 13:

    https://r8.googlesource.com/r8/+/60e493f814cb2a37e3b02a30c09023c56b6d1bf9/src/main/java/com/android/tools/r8/utils/AndroidApiLevel.java#32

    I filed a github issue here: https://github.com/bazelbuild/bazel/issues/10556