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.
Yes, we can define multiple methods in a class with the same name but with different types of parameters.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With