Different import dependencies for different enviro

2019-08-26 23:43发布

问题:

I have a JAR which needs some of the classes provided by the java.time package, which are available in Java 8 and up.

However, the JAR also needs to be usable on Android, where java.time is only available on API 26 and up (I need to support lower APIs as well).

I could simply use threetenbp, which is most portable and provides a compatible implementation with the same class and member names (it is literally just a matter of providing the JAR and changing the import packages).

However, this is not very efficient, especially on Android, where an adapted version of threetenbp is recommended instead.

Now this leaves me with at least two different choices of packages to import, depending on the environment I am building for: on JDK use java.time.*, on Android use org.threetenb.bp.*.

Is there a way I can do this without having to maintain two versions of my classes?

标签: java import