Device Compatibility
Android isdesigned to run on many different types of devices, from phones to tablets andtelevisions. As a developer, the range of devices provides a huge potentialaudience for your app. In order for your app to be successful on all thesedevices, it should tolerate(容忍) some featurevariability(变化) and provide aflexible(灵活的) user interfacethat adapts to different screen configurations.
To facilitate(方便)your effort toward that goal, Android provides a dynamicapp framework in which you can provide configuration-specific app resources in static files (such asdifferent XML layouts for different screen sizes). Android then loads theappropriate resources based on the current device configuration. So with someforethought(远见) to your appdesign and some additional app resources, you can publish asingle application package (APK) that provides an optimized user experience ona variety of devices.
If necessary,however, you can specify your app's feature requirements and control which typesof devices can install your app from Google Play Store. This pageexplains how you can control which devices have access to your apps, and how toprepare your apps to make sure they reach the right audience. For moreinformation about how you can make your app adapt to different devices,read Supporting Different Devices.
What Does "Compatibility" Mean?
As you read moreabout Android development, you'll probably encounter the term"compatibility" in various situations. There are two types ofcompatibility: device compatibility and appcompatibility.
Because Androidis an open source project,any hardware manufacturer can build a devicethat runs the Android operating system. Yet, a device is "Androidcompatible" only if it can correctly run apps written forthe Android execution environment. The exact details of the Androidexecution environment are defined by the Android compatibility program and eachdevice must pass the Compatibility Test Suite (CTS) in order to be consideredcompatible.
因为Android是一个开源项目,任何一个硬件厂商都可以构建一个设备来运行Android操作系统.然后,一个设备被称作是“Android兼容的”,只是因为那些在Android执行环境下编写的app能在设备上运行.每一个设备为了考虑兼容性必须通过兼容性测试套件的测试
As an appdeveloper, you don't need to worry about whether a device is Androidcompatible, because only devices that are Android compatible include GooglePlay Store. So you canrest assured that users whoinstall your app from Google Play Store are using an Android compatible device.
而作为一个app开发者,你不需要考虑设备是否是Android兼容的,因为只要包含Google Play Store的设备就是Android兼容的.所以你可以放心,用户一定会使用一个Android兼容的设备从Google Play Store上安装你的app.
However, you doneed to consider whether your app is compatible with eachpotential device configuration. Because Android runs on a wide range of deviceconfigurations, some features are not available on all devices. For example,some devices may not include a compass sensor. If your app's core functionalityrequires the use of a compass sensor, then your app is compatible only withdevices that include a compass sensor.
不过你需要考虑的是:你的app是否在潜在的不同配置的设备上的保持了兼容性.因为Android系统运行在一个广泛不同配置的设备上,一些特性不是在所有设备上都存在.例如,一些设备可能不会包含指南针传感器.如果你的app的一些核心功能需要用到指南针传感器,而你的app只能在那些包含了指南针传感器的设备上运行才能是兼容的.
Controlling Your App's Availability to Devices
Android supportsa variety of features your app can leverage through platform APIs. Somefeatures are hardware-based (such as a compass sensor), some are software-based(such as app widgets), and some are dependent on the platform version. Notevery device supports every feature, so you may need to control your app'savailability to devices based on your app's required features.
Android提供了很多很丰富的功能,让你的app可以通过平台API来利用.一些功能是基于硬件的,而另一些功能则基于软件的,还有一些功能则是依赖于平台的版本.不是每一个设备都支持每一个功能,所以你可以根据你app的功能需求来控制一些设备上你APP的可用性
To achieve thelargest user-base possible for your app, you should strive to support as manydevice configurations as possible using a single APK. In most situations, youcan do so by disabling optional features at runtime and providing app resources with alternativesfor different configurations (such as different layouts for different screensizes). If necessary, however, you can restrict your app's availability todevices through Google Play Store based on the following devicecharacteristics:
为了尽可能让你的app实现最大的用户群,你需要努力尽可能的用一个APK就能支持很多不同配置的设备.在大多数情况下,你可以在运行时禁用掉一些可选的功能和提供不同配置设备的app资源。当有必要时,你可以通过Google Play Store的下列设备属性来限制你的app在一些设备上的可用性
Device features
In order for youto manage your app’s availability based on device features, Androiddefines feature IDs for any hardware or software feature thatmay not be available on all devices. For instance, the feature ID for thecompass sensor is FEATURE_SENSOR_COMPASS and thefeature ID for app widgets is FEATURE_APP_WIDGETS.
为了能让你管理app在一些设备功能上的可用性,Android给一些硬件或软件的功能定义了一些功能id,但不会在所有设备上都存在.
If necessary,you can prevent users from installing your app when their devices don't providea given feature by declaring it with a
如果必要的话,你可以在manifest文件的
For example, if your app does notmake sense on a device that lacks a compass sensor, you candeclare the compass sensor as required with thefollowing manifest tag:
android:required="true"/>
...
Google PlayStore compares the features your app requires to the features available on eachuser's device to determine whether your app is compatible with each device. Ifthe device does not provide all the features your app requires, the user cannotinstall your app.
Google Play Store会根据你app上申请的功能与设备上所拥有的功能做对比来决定你app是否兼容(是否可以安装在)该设备.
However, if yourapp's primary functionality does not require a devicefeature, you should set the required attributeto "false" andcheck for the devicefeature at runtime. If the app feature is not available onthe current device, gracefully degrade the corresponding app feature. Forexample, you can query whether a feature is available by calling hasSystemFeature() like this:
如果你的app核心功能上对一些设备功能是非必须的,那你可以设置required属性为"false"并且在运行时来检查设备的这项功能是否存在.如果当前设备不存在该功能,就可以适当的禁用掉该app相关的功能.
PackageManager pm = getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)){
// This device does not have a compass, turn off the compass feature
disableCompassFeature();
}
For informationabout all the filters you can use to control the availability of your app tousers through Google Play Store, see the Filters on Google Play document.
Note: Some system permissions implicitly require theavailability of a device feature. For example, if your app requests permissionto access to BLUETOOTH, thisimplicitly requires the FEATURE_BLUETOOTH devicefeature. You can disable filtering based on this feature and make your appavailable to devices without Bluetooth by setting the required attributeto "false" in the
注意:一些系统的权限也会隐式的需要设备上的一些功能的存在.例如,如果你的app申请了进入蓝牙的权限,这样隐式的需要设备上的蓝牙功能.你可以设置required属性为"false"来禁用掉一些功能,让app还能够在该设备上继续使用
Platform version
Differentdevices may run different versions of the Android platform, such as Android 4.0or Android 4.4. Each successive platform version often adds new APIs not availablein the previous version. To indicate which set of APIs are available, eachplatform version specifies an API level. For instance, Android 1.0 is APIlevel 1 and Android 4.4 is API level 19.
不同的设备可以能运行着不同版本的Android平台.每一个连续性版本的平台都经常在新版本中增加前一个版本中没有的一些新API.为了指出哪个API是可用的,每一个平台版本都指定一个API等级.
The API levelallows you to declare the minimum version with which your app is compatible,using the
For example,the Calendar Provider APIs were added inAndroid 4.0 (API level 14). If your app cannot function without these APIs, youshould declare API level 14 as your app's minimum supported version like this:
例如: Calendar Provider的API在Android4.0(API等级为14)的时候加入的.那么如果你的app不能使用或实现这些功能时,你需要声明一下你的API等级为14来座位你app的最小sdk版本.
...
The minSdkVersion attributedeclares the minimum version with which your app is compatible and the targetSdkVersion attributedeclares the highest version on which you've optimized your app.
minSdkVersion 属性声明了你app兼容性的最小API等级,而targetSdkVersion 属性则声明了你app使用的最高API等级
Each successiveversion of Android provides compatibility for apps that were built using theAPIs from previous platform versions, so your app should always be compitiblewith future versions of Android while using the documented Android APIs.
每一个连续性版本的中Android都使用前一个版本的平台API为app提供了兼容性,所以你的app应该当让兼容在Android文档中规定的未来版本的API.
Note: The targetSdkVersion attributedoes not prevent your app from being installed on platform versions that arehigher than the specified value, but it is important because it indicates tothe system whether your app should inherit behavior changes in newer versions.If you don't update the targetSdkVersion tothe latest version, the system assumes that your app requires somebackward-compatibility behaviors when running on the latest version. Forexample, among the behavior changes in Android 4.4, alarmscreated with the AlarmManager APIs arenow inexact by default so the system can batch app alarms and preservesystem power,but the system will retain the previous API behavior for your app if yourtarget API level is lower than "19".
注意:targetSdkVersion 属性不会阻止你的app安装在比你指定的平台版本还要高的平台上,但是这个属性很重要,因为它向系统表名了你的app在一些新版本上是否可以继承一些行为变化.如果你不更新targetSdkVersion 属性到最新版本,系统会假设你的app在最新的版本中需要一些向后兼容性的行为.例如,在 behavior changes in Android 4.4之中,alarms会使用AlarmManager的API来创建而现在已经作为了默认,这样系统可以批量的管理alarms来位系统保存电量,但是系统将会为你的APP保留前一个版本的API行为.
However, if yourapp uses APIs added in a more recent platform version, but does not requirethem for its primary functionality, you should check the API level at runtimeand gracefully degrade the corresponding features when the API level is toolow. In this case, set the minSdkVersion tothe lowest value possible for your app's primary functionality, then compare thecurrent system's version, SDK_INT, to one thecodename constants in Build.VERSION_CODES thatcorresponds to the API level you want to check. For example:
但是,如果你的app用到的最新的API但是核心功能中又不需要他们时,你应该在运行时来检测当前API的等级并且当API等级比较低的时候适当的降低相关的功能.在这样的案例中,设置minSdkVersion 属性到你app核心功能所需要的最低版本,然后与系统版本对比.
if (Build.VERSION.SDK_INT< Build.VERSION_CODES.HONEYCOMB){
// Running on something older than API level 11, so disable
// the drag/drop features that use ClipboardManagerAPIs
disableDragAndDrop();
}
Screenconfiguration
Android runs ondevices of various sizes, from phones to tablets and TVs. In order to categorizedevices by their screen type, Android defines two characteristics for eachdevice:screen size (the physical size of the screen) andscreen density (thephysical density of the pixels on the screen, known as DPI). To simplifythe different configurations, Android generalizes these variants into groupsthat make them easier to target:
屏幕尺寸;屏幕密度:每像素上的密度,DPI
· Four generalized sizes: small, normal,large, and xlarge.
· And several generalized densities: mdpi(medium), hdpi (hdpi), xhdpi (extra high), xxhdpi (extra-extra high), and others.
By default, yourapp is compatible with all screen sizes and densities,because the systemmakes the appropriate adjustments to your UI layout and image resources asnecessary for each screen. However, you should optimize the userexperience for each screen configuration by adding specialized layouts fordifferent screen sizes and optimized bitmap images for common screen densities.
默认的情况下,你的app会兼容各种屏幕尺寸和屏幕密度的,因为系统会为你的UI布局做一些适当的调整以及会为你的图片资源做一些适当的调整.但是,你应该自己去为每一种配置的屏幕去优化用户的体验,使用不同布局来适应不同尺寸的屏幕,为通用的屏幕密度优化bitmap
For informationabout how to create alternative resources for different screens and how torestrict your app to certain screen sizes when necessary, read Supporting Different Screens.
Controlling Your App's Availability for Business Reasons
In addition torestricting your app's availability based on device characteristics, it’spossible you may need to restrict your app’s availability for business or legalreasons. For instance, an app that displays train schedules for the LondonUnderground is unlikely to be useful to users outside the United Kingdom. Forthis type of situation, Google Play Store provides filtering options in thedeveloper console that allow you to control your app’s availability fornon-technical reasons such as the user's locale or wireless carrier.
为了限制你的app在不同设备上的可使用性,可以限制你app的商用或者法律原因的可用性.对于这一类型的情况Google Play Store提供了开发者控制台提供一些过滤选项来允许你在一些为非技术原因上来控制该app的可用性.
Filtering fortechnical compatibility (such as required hardware components) is always basedon information contained within your APK file. But filtering for non-technicalreasons (such as geographic locale) is always handled in the Google Playdeveloper console.
而一些技术性的兼容性过滤器总是存在与APK里的文件信息里.但是一些非技术原因的过滤器就只能在Google Play Store上的一些开发者控制台里了