Can I download only certain lines of code according to the version of Android OS?

Is there a simple line of code that only downloads code if the OS version meets the requirements?

Suppose I have a target OS like 2.2, but min sdk is 3 for android 1.5, so even if I have code in my project that is not compatible with 1.5, it will still compile, since the target OS is 2.2 . Anyway, I want to declare a function that requires a code that is not in the 1.5 SDK and will fail if it is downloaded to 1.5 phones. Is there such a simple thing that I can do? So I don’t need the whole application not to be accessible to 1.5 users?

if (Android OS == >2.1){ //Insert code here that requires 2.1 and up} else{ //insert code that would appear is OS is <2.1} 
+7
java android android-1.5-cupcake
source share
4 answers

Yes you can do it. There are actually more than one way. (Note: the only specific part of this answer for Android is how you find out the version of the platform.)

Suppose class X has the void y() method in version 2.0, but not before.

One way to call this method without introducing any compile-time dependencies is to use reflection to find the method and invoke on it. For example:

 X x = ... if (BUILD.VERSION.RELEASE.compareTo("2.0") >= 0) { // (exception handling omitted ...) Method m = c.getClass().getDeclaredMethod("y"); m.invoke(x); } 

Another way is to create the version compatibility adapter API for your application as follows:

 /** Version compatibility adapter API */ interface Compat { void doY(); } /** Adapter class for version 1 */ class CompatV1 { public void y(X x) { // do nothing } } /** Adapter class for version 2 */ class CompatV2 { public void y(X x) { xy(); } } // // Code to instantiate the relevant adapter for the current platform. // Class<?> compatClass; // (Exception handling omitted) if (BUILD.VERSION.RELEASE.compareTo("2.0") < 0) { compatClass = Class.forName("...CompatV1"); } else { compatClass = Class.forName("...CompatV2"); } // (Exception handling omitted) Compat compat = (Compat) compatClass.newInstance(); // The adapter object can be passed around as a parameter, wrapped // as a singleton or injected using dependency injection. // Invoke Xy() as follows: X x = ... compat.y(x); 

The second version looks a little heavyweight, but it has the advantage that the dynamic (slow, insecure) code is executed only once and that the specific version code is isolated from the rest of the code. In real life, you would probably put a number of methods in the adapter interface.

This approach requires a bit more understanding in order to develop how to develop a compatibility API so that it completely isolates version dependencies from the rest of the code. You may also need to revise the adapter API and create new adapter classes for each new (incompatible) major version.

Finally, if the platform API changes, you need to adapt to using classes or methods in the old version that are removed in the newer version, then you will need to compile various adapter classes (for example, CompatV* classes) using different Android SDKs. This will make your build processes more complex.


For others, β€œtake” this problem, read the following Android blog articles:

+11
source share
+2
source share
0
source share

You can check with Build.VERSION.RELEASE , it gives you the current version of your Android system (1.5,1.6,2.1,2.2)

There is more at Build.VERSION

0
source share

All Articles