您的位置:首页 > 移动开发 > Android开发

Android应用在不同版本间兼容性处理

2017-11-23 20:16 549 查看
在Android系统中向下兼容性比较差,但是一个应用APP经过处理还是可以在各个版本间运行的。向下兼容性不好,不同版本的系统其API版本也不同,自然有些接口也不同,新的平台不能使用旧的API,旧的平台也使用不了新的API。

为了应用APP有更好的兼容性,咱们可以利用高版本的SDK开发应用,并在程序运行时(Runtime)对应用所运行的平台判断,旧平台使用旧的API,而新平台可使用新的API,这样可以较好的提高软件兼容性。

那么,如何在软件运行时做出这样的判断呢?答案下边揭晓:

在Android SDK开发文档中有段话这样的话:

Check System Version at Runtime(在软件运行时检查判断系统版本)

Android provides a unique code for each platform version in the
Build
constants class. Use these codes within your app to build conditions that ensure the code thatdepends on higher API levels is executed only when those APIs are available on the system.

private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}


Note: When parsing XML resources, Android ignores XML attributes that aren’t supported by the current device. So you can safely use XML attributes thatare only supported by newer versions without worrying about older versions breaking when theyencounter that code. For example, if you set the
targetSdkVersion="11"
, your app includes the
ActionBar
by defaulton Android 3.0 and higher. To then add menu items to the action bar, you need to set
android:showAsAction="ifRoom"
in your menu resource XML. It's safe to do this in a cross-version XML file, because the older versions of Android simply ignore the
showAsAction
attribute (that is, you do not need a separate version in
res/menu-v11/
).

从上面可以知道Android为我们提供了一个常量类Build,其中最主要是Build中的两个内部类VERSION和VERSION_CODES,

VERSION表示当前系统版本的信息,其中就包括SDK的版本信息,用于成员SDK_INT表示;

对于VERSION_CODES在SDK开发文档中时这样描述的,Enumeration of the currently known SDK version codes. These are the values that can be found in
SDK
. Version numbers increment monotonically with each official platform release.

其成员就是一些从最早版本开始到当前运行的系统的一些版本号常量。

在我们自己开发应用过程中,常常使用如下的代码形式判断运行新API还是旧的API:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
// 包含新API的代码块
}
else
{
// 包含旧的API的代码块
}


OK,大家都知道原理了吧! 需要实例的百度蛮多的,这里就不提供了。

android 10(2.2.3/2.2.4)及以下的版本是没有fragment的,从 11(3.0.x) 就有了,这就是新特性,诸如此类的还有很多呢。

不明白题主的“向下兼容”具体指哪方面,就我理解的来说吧:

为了使老版本的sdk能用上新版本的特性和功能,官方都会给出额外的jar包,还是以 fragment 为例,如果我开发的app必须要能在 2.3的系统上运行,但同时要使用 fragment 怎么办呢?此时就可以用引入android.support.v4.jar包,这就是官方给的兼容性解决方案了。

可以发现,随着 SDK 版本的不断升级,官方给出的jar包也越来越多,android.support.v7.jar,v13......

如果你想详细了解下某些版本的升级带来了哪些新特性,欢迎访问Android 5.0 Behavior Changes,当然,感兴趣的话也可以找到历史版本的升级记录,在这里就不多说了。。。

Android 版本更替,新的版本带来新的特性,新的方法。

新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版本系统上,运行时将会crash。

本文以一个具体的例子说明如何在使用高API level的方法时处理好兼容性问题。

例子:根据给出路径,获取此路径所在分区的总空间大小。

安卓中的文件存储使用参考中提到:

获取文件系统用量情况,在API level 9及其以上的系统,可直接调用File对象的相关方法,以下需自行计算


一般实现
就此需求而言,API level 9及其以上,调用
File.getTotalSpace()
即可, 但是在API level 8 以下系统
File
对象并不存在此方法。

如以下方法:

/**
* Returns the total size in bytes of the partition containing this path.
* Returns 0 if this path does not exist.
*
* @param path
* @return -1 means path is null, 0 means path is not exist.
*/
public static long getTotalSpace(File path) {
if (path == null) {
return -1;
}
return path.getTotalSpace();
}


处理无法编译通过
如果
minSdkVersion
设置为8,那么build时候会报以下错误:

Call requires API level 9 (current min is 8)


为了编译可以通过,可以添加
@SuppressLint("NewApi")
或者
@TargeApi(9)


用@TargeApi($API_LEVEL)显式表明方法的API level要求,而不是@SuppressLint("NewApi");


但是这样只是能编译通过,到了API level8的系统运行,将会引发
java.lang.NoSuchMethodError


正确的做法
为了运行时不报错, 需要:

判断运行时版本,在低版本系统不调用此方法

同时为了保证功能的完整性,需要提供低版本功能实现

如下:

/**
* Returns the total size in bytes of the partition containing this path.
* Returns 0 if this path does not exist.
*
* @param path
* @return -1 means path is null, 0 means path is not exist.
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
// using @TargeApi instead of @SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static long getTotalSpace(File path) {
if (path == null) {
return -1;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
return path.getTotalSpace();
}
// implements getTotalSpace() in API lower than GINGERBREAD
else {
if (!path.exists()) {
return 0;
} else {
final StatFs stats = new StatFs(path.getPath());
// Using deprecated method in low API level system,
// add @SuppressWarnings("description") to suppress the warning
return (long) stats.getBlockSize() * (long) stats.getBlockCount();
}
}
}


总结

在使用高于
minSdkVersion
API level的方法需要:

用@TargeApi($API_LEVEL) 使可以编译通过, 不建议使用@SuppressLint("NewApi");
运行时判断API level; 仅在足够高,有此方法的API level系统中,调用此方法;
保证功能完整性,保证低API版本通过其他方法提供功能实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: