Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move desugar to a standalone project #2975

Closed
balazs-zsoldos opened this issue May 9, 2017 · 9 comments
Closed

Move desugar to a standalone project #2975

balazs-zsoldos opened this issue May 9, 2017 · 9 comments
Assignees

Comments

@balazs-zsoldos
Copy link

If you could, please, move Desugar out to a separate project, it would be great. Reasons:

  • Bazel might be the future, but it is not the case yet. Desugar would be a popular tool used by maven, gradle, or a command-line script if it was possible
  • Modularizing software is the future. It is better to have more tiny projects with separate release plan and bug tracking than having one monolith pyramid

If it is possible, please upload the Desugar jar with a descriptive pom (containing its dependencies) to maven-central.

Btw.: We moved out the code of Desugar and recompiled it as a JAR file. The only thing we had to do is removing the dependency on the com.google.devtools.build.android.Converters class and all dependencies could be downloaded from maven-central. The tool works great (although it has some bugs, fits our needs much better than the retrolambda project). Please do this step before it is too late, before the Desugar source code is wired too much together with the remaining part of Bazel code.

@aj-michael
Copy link
Contributor

Hi @balazs-zsoldos , there is a standalone repo containing the desugar within the Android platform's code base: https://android.googlesource.com/platform/external/desugar. It is being integrated with Gradle in an upcoming Android Studio release: https://developer.android.com/studio/preview/features/java8-support.html.

Does this fit your use case?

I'll leave it up to @kevin1e100 , author of desugar, for comment on if there are any plans to publish a jar to maven central.

@aj-michael aj-michael assigned kevin1e100 and unassigned aj-michael May 9, 2017
@balazs-zsoldos
Copy link
Author

Hi @aj-michael,

we already downloaded the Android Studio Preview. However, our use-case a bit special :-).

We want to repack an OSGi based application to be used on an Android tablet. We add each dependencies to the project as an asset. So in the gradle script:

  • we iterate through the dependencies of the project
  • we call desugar on every dependency jar
  • we call dx on every dependency jar and aapt
  • we add the jar to the assets of the project

What we could not do is to call Desugar on a JAR file from the gradle script. We could do it only in the way that we re-packaged Desugar and created an executable JAR from it.

Is there a way to call Desugar as a library from the Gradle script? Also in the long term, I think people would really appreciate it if Desugar could be used as a standalone lib or command-line tool. It would be very useful not only in other projects (not only in Android studio and/or Basel). That is why I asked if it could be uploaded to Maven Central. In that case, it could be accessed by everyone from any tool.

@aj-michael
Copy link
Contributor

I am not very familiar with Gradle scripts, so I can't advise on how to invoke Desugar from a Gradle script.

To partially answer your question, Desugar already is a standalone command-line tool. Bazel treats it as a standalone command-line tool. It just so happens that command-line tool is bundled inside of the standard Bazel distribution.

Here are the steps to build and use the command-line desugar tool:

$ git clone https://github.com/bazelbuild/bazel

$ bazel build //src/tools/android/java/com/google/devtools/build/android/desugar:desugar_bin_deploy.jar
INFO: Found 1 target...
Target //src/tools/android/java/com/google/devtools/build/android/desugar:desugar_bin_deploy.jar up-to-date:
  bazel-bin/src/tools/android/java/com/google/devtools/build/android/desugar/desugar_bin_deploy.jar
INFO: Elapsed time: 3.336s, Critical Path: 2.87s

$ desugar=bazel-bin/src/tools/android/java/com/google/devtools/build/android/desugar/desugar_bin_deploy.jar

$ java -cp $desugar com.google.devtools.build.android.desugar.Desugar --help
Options category 'input':
  --bootclasspath_entry (a valid filesystem path; may be used multiple times)
    Bootclasspath that was used to compile the --input Jar with, like javac's -
    bootclasspath flag (required).
  --classpath_entry (a valid filesystem path; may be used multiple times)
    Ordered classpath (Jar or directory) to resolve symbols in the --input Jar,
    like javac's -cp flag.
  --input [-i] (a valid filesystem path; may be used multiple times)
    Input Jar or directory with classes to desugar (required, the n-th input is
    paired withthe n-th output).

Options category 'misc':
  --[no]copy_bridges_from_classpath (a boolean; default: "false")
    Copy bridges from classpath to desugared classes.
  --[no]desugar_interface_method_bodies_if_needed (a boolean; default: "true")
    Rewrites default and static methods in interfaces if --min_sdk_version <
    24. This only works correctly if subclasses of rewritten interfaces as well
    as uses of static interface methods are run through this tool as well.
  --[no]desugar_try_with_resources_if_needed (a boolean; default: "false")
    Rewrites try-with-resources statements if --min_sdk_version < 19.
  --[no]desugar_try_with_resources_omit_runtime_classes (a boolean; default: "false")
    Omits the runtime classes necessary to support try-with-resources from the
    output. This property has effect only if --
    desugar_try_with_resources_if_needed is used.
  --[no]rewrite_calls_to_long_compare (a boolean; default: "true")
    rewrite calls to Long.compare(long, long) to the JVM instruction lcmp
  --min_sdk_version (an integer; default: "1")
    Minimum targeted sdk version.  If >= 24, enables default methods in
    interfaces.
  --[no]verbose [-v] (a boolean; default: "false")
    Enables verbose debugging output.

Options category 'output':
  --output [-o] (a valid filesystem path; may be used multiple times)
    Output Jar or directory to write desugared classes into (required, the n-th
    output is paired with the n-th input, output must be a Jar if input is a
    Jar).

@balazs-zsoldos
Copy link
Author

@aj-michael Thanks a lot for the answer! It will be easier for us in the future to build Desugar in the standard way. The company computer is a Windows machine and there I could neither install or compile Bazel due to missing reqs (e.g.: windows SDK or it could not find msys64), but I guess time will solve this issue, and there will be an easily downloadable binary that works out-of-the-box.

You can close this issue if you want, or leave it until you decide if you upload desugar to maven-central with a pom that has the dependencies (which I think would be useful for others who would use maven to desugar java 8 code).

bazel-io pushed a commit that referenced this issue May 10, 2017
This will make it easier to build the desugarer tool from the Bazel source tree as an executable (deploy) jar and then use it for something else.

For example, with this CL, you can do the following steps to get a standalone
desugarer binary:

1. git clone https://github.com/bazelbuild/bazel
2. bazel build //src/tools/android/java/com/google/devtools/build/android/desugar:desugar_bin_deploy.jar
3. cp bazel-bin/src/tools/android/java/com/google/devtools/build/android/desugar/desugar_bin_deploy.jar ~/bin/desugar.jar

And then you can invoke desugar without bazel with `java -jar ~/bin/desugar.jar`.

#2975

RELNOTES: None
PiperOrigin-RevId: 155629711
@eaftan
Copy link

eaftan commented May 10, 2017

(standing in for @kevin1e100, who is OOO)

We're sorry, but we have no plans to publish Desugar as a separate tool. Our team is lightly staffed for this, and our priority is Bazel and Android Studio customers. There are some workarounds in this thread, and I hope they are sufficient for your use case.

@aj-michael
Copy link
Contributor

Marking this as closed per @eaftan's comment. @kevin1e100 can reopen if plans change.

@Hitexriod
Copy link

Hitexriod commented Mar 2, 2021

@aj-michael thank you sir, can you send your desugar_bin_deploy.jar ?

i used your command :
bazel build //src/tools/android/java/com/google/devtools/build/android/desugar:Desugar_deploy.jar

result :

Starting local Bazel server and connecting to it...
ERROR: While resolving toolchains for target //src/tools/android/java/com/google/devtools/build/android/desugar:Desugar: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.
ERROR: Analysis of target '//src/tools/android/java/com/google/devtools/build/android/desugar:Desugar_deploy.jar' failed; build aborted: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.
INFO: Elapsed time: 11.709s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (11 packages loaded, 19 targets configured)

@bubbleguuum
Copy link

bubbleguuum commented Mar 13, 2021

@Hitexriod

Correct build/run instructions are:

bazel build //src/tools/android/java/com/google/devtools/build/android/desugar:Desugar_deploy.jar
java -cp bazel-bin/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar_deploy.jar <options>

@CypherpunkSamurai
Copy link

CypherpunkSamurai commented May 24, 2022

The code seems to have been updated and the build commands no longer work.

I had to reset to the commit f78ffb5fb2f37d20acd1320ed37080c26b32e607 (according to the last comment here) and then run the commands.

git reset --hard f78ffb5fb2f37d20acd1320ed37080c26b32e607
bazel build //src/tools/android/java/com/google/devtools/build/android/desugar:Desugar_deploy.jar
cp bazel-bin/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar_deploy.jar desugar_deploy.jar
java -jar bazel-bin/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar_deploy.jar

but the problem is, it's giving me errors when i run it on my machine.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.devtools.build.android.desugar.Desugar (file:/E:/BikiniAtol/android-ide/build_r8/desuga/Desugar_deploy.jar) to field java.lang.invoke.InnerClassLambdaMetafactory.dumper
WARNING: Please consider reporting this to the maintainers of com.google.devtools.build.android.desugar.Desugar
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.NullPointerException: Failed to register lambda dump directory 'C:\Users\RAKESH~1\AppData\Local\Temp\lambdas13320091041861902585'
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:987)
        at com.google.devtools.build.android.desugar.Desugar.verifyLambdaDumpDirectoryRegistered(Desugar.java:1028)
        at com.google.devtools.build.android.desugar.Desugar.main(Desugar.java:949)

I also found this reddit post that suggests using rt.jar, should i try downloading rt.jar from jdk8?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants