简体   繁体   中英

Which way does the Android minimum SDK work?

If I have set the minimum SDK version to 16 (Jelly Bean 4.1) and I have coded most of my project in such a way that is compatible with this API level, but I have a small method which requires SDK version 25, will this stop the entire app working on a device with the Jelly Bean platform? Or will it allow them to install it and only that specific part won't work?

You can use annotations to remove these problems in the method.

@TargetApi(25)

1.You need to create a abstract class with the abstract method that you are talking about.

    public abstract class YourClass {
        public abstract void doSomething();
    }

2.Two implementation. One for a legacy version and other for a new version, in the new version you put @TargetApi.

  • Legacy Version:

     public class YourClassLegacy extends YourClass { public void doSomething(){ //Legacy method } } 
  • Implementation 25 version:

     @TargetApi(25) public class YourClassNew extends YourClass { public void doSomething(){ //25 version method } } 

3.In the method that you call a this method ( doSomething() ), you need implement this.

    public static void methodFoo(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= 25) {
            //call to YourClassNew.doSomething();
        }else{ 
            //call to YourClassLegacy.doSomething();
        } 
     }

then you need to code yourself to version 25 and re-code the old practice to keep up with the trends. Gradle WILL and MAY have issues higher API don't support certain methods and most particularly libraries where people use in their own methods

You should provide an alternative for alder versions by using a check.

if(android.os.Build.VERSION.SDK_INT >= 25) {
    // use new method
} else {
    // use alternative method
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM