1

NB: Please suggest a better title for this question.

Suppose I have two classes with the same name and I want to import one or the other before I compile depending on the use-case/app version. Is there a way to do it ?

Why do I need this ?, you may ask.

I need to use an important java library that uses java.time, but I also need to support min sdk 23 (Android 6.0). I could use threeThenABP in my Android app to be able to use all of the java.time APIs, but I need to import them from org.threeten.bp.

That important library is actually private. So, the idea is to make it use either java.time imports or org.threeten.bp imports in order to compile 2 versions that would be compatible with both Android and desktop Java applications.

I hope my explanations are clear enough. Thank you for your help.

Ariles
  • 333
  • 1
  • 14
  • A library using `java.time` is not available in Android below level 26. So no chance to use that library on older Android versions. The only way would be the option (if you have) to hold two different versions of that library - one of them using threetenbp instead. I don't know if you can create it yourself or build it from available sources which you might be able to edit (based on a fork?). – Meno Hochschild May 15 '19 at 18:04
  • @MenoHochschild yes, that's exactly what I'm trying to explain above ("...the idea is to make it use either `java.time` imports or `org.threeten.bp` imports in order to compile 2 versions"). Now, the question is how can I do that in a clean way ? – Ariles May 16 '19 at 08:57
  • 1
    I suggest you simply use the backport always (until one day you can switch to using the built-in java.time always). While the backport was developed for Java 6 and 7, it works with later Java versions too. – Ole V.V. May 17 '19 at 11:55

1 Answers1

1

I ended up using threetenbp in the library project, as suggested by @Ole V.V.
Then, in my android project, I used threetenAbp (the android specific version) and I imported it the library using gradle. Finally, excluding the packages used in the library means that threetenAbp is used instead.

implementation ('my-library') {
    exclude group: 'org.threeten'
}

NB: notice the difference (A) in threetenbp and threetenAbp.

Ariles
  • 333
  • 1
  • 14