简体   繁体   English

Android最低SDK的工作方式是什么?

[英]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? 如果我已将最低SDK版本设置为16(Jelly Bean 4.1),并且以与该API级别兼容的方式对大部分项目进行了编码,但是我有一个小的方法需要SDK版本25,那么该方法会停止吗?整个应用程序在具有Jelly Bean平台的设备上运行? 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. 1.您需要使用您正在谈论的抽象方法创建一个抽象类。

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

2.Two implementation. 2.二次执行。 One for a legacy version and other for a new version, in the new version you put @TargetApi. 一个用于旧版本,另一个用于新版本,在新版本中,您放置了@TargetApi。

  • Legacy Version: 旧版:

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

     @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. 3.在调用this方法的方法( doSomething() )中,您需要实现此方法。

    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. 那么您需要将自己编码为版本25,然后重新编码旧的方法以跟上趋势。 Gradle WILL and MAY have issues higher API don't support certain methods and most particularly libraries where people use in their own methods Gradle WILL和MAY可能存在问题,高级API不支持某些方法,尤其是人们在自己的方法中使用的库

You should provide an alternative for alder versions by using a check. 您应该使用支票为provide木版本提供替代方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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