Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling different API versions in same Java source

I'm sure this is a dumb question, but.. We have the same Java source files and we want to use a different version of a Java API (jar file) depending on the client we are building our app for.

The newer version of the API has the methods setAAA() and setBBB() which we reference in our Java source:

if (...) {
  api.setAAA(a);
  api.setBBB(b);
} 

This code will fail if compiled with the old API has the old API doesn't have these setters. Is there any way to conditionalize this code to only compile the setter lines if we are using the new API?

Thanks.

like image 407
Marcus Leon Avatar asked Apr 06 '09 22:04

Marcus Leon


People also ask

Can a class have multiple versions of the same method?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

How do I change my API version?

Step 1: Open your project in Android mode then go to Gradle Scripts > build. gradle(Module: app) as shown in the following image. Step 2: Refer to the below image and here you have to change the minSdkVersion and targetSdkVersion as per the requirement.

What is API version control?

API versioning is the practice of transparently managing changes to your API. Managing an API boils down to defining and evolving data contracts and dealing with breaking changes. The most effective way to evolve your API without breaking changes is to follow effective API change management principles.


1 Answers

The safest approach is to fall back to the lowest version you need to support. That assumes all versions are backwards compatible which isn't necessarily the case.

If that solution isn't appropriate or desirable then I fall back to dependency injection. The Spring framework is by far the most popular and common DI framework but no means the only one. Guice is another one. You can even roll your own if it's undesirable to add a complete framework for this.

But I have problems envisioning a Java application--particularly a Web/J2EE application--that I do without using Spring. It's simply too useful.

Let's say there are 4 versions of a relevant jar. The API has changed twice in that time so you have 3 different API versions. You need to abstract use of that jar to an API of your own that is consistent across all these versions and then create three implementations of it: one for each different API version.

In Spring you create an application context, which defines all your beans and how they're injected into other beans. There is no reason you can't choose or build and application context as part of a build process. Often properties are used for this but you could also include part of the application context this way too.

The key point here is that even though the APIs are different you need to abstract away those differences as far as your code is concerned. If you don't you're just asking for trouble and it just gets messier.

like image 133
cletus Avatar answered Sep 25 '22 03:09

cletus