背景:
个人使用的设备越来越多,处了手机之外,使用的设备包括IOT,穿戴式(手表),PAD,TV,HandBook等。应用开发者需要为各种各样的设备开发程序。对于OS操作系统厂家来说,提供一套统一的API或者一种清晰组织的API非常重要。我们调查一下IOS,Android,Windows是如何组织API,供大家开发多设备应用程序的。
分析:
1,IOS/TvOs/iPadOS/WatchOS/MacOS
苹果根据不同的设备类型,从品牌上对OS进行区分。目前对外的OS名称包括iPhoneOs,TvOs,IPadOs,WatchOS,MacOs。具体这些OS在技术实现上差异有多大,不得而知。
- iPadOS is the operating system specifically designed to take full advantage of everything that makes iPad iPad.
- TvOS:Leverage many of the same frameworks, technologies, and concepts that are similar to iOS. TVMLKit templates using XML and Javascript are available as a framework for tvOS apps that let you use predefined layouts and Javascript APIs to quickly create beautiful apps ideal for streaming media.
- iOS(formerlyiPhone OS): including theiPhone, andiPod Touch;
- watchOS:based on iOS, the operating system of theiPhone, and has many similar features
我目前还没有理解背后区分OS品牌的逻辑,但是苹果为开发者提供的SDK API仍旧是统一组织的。
Apple Developer Documentationdeveloper.apple.com- 针对每个OS都有的API(例如UIKit,SwiftUI)会在右侧展示可以使用的IOS版本
- 针对差异化的API不同部分,会在Kit名字上命名区分:
例如TVUIKit,WatchKit进行区分。
对于Apple API开发人员来说,他们是如何定义API区别不同的OS的?
Mark Availability:
- ObjectC:
API_AVAILABLE(ios(11), macosx(10.13))
- Swift:
available(iOS 11, macOS 10.13, *)
Check Availability
Use the @available()
keyword to check availability information in a conditional statement in Objective-C
2,Windows 10/Universal Windows Platform (UWP) app
windows10操作系统是多设备操作系统(a desktop PC, Xbox, Mixed-reality headset)
编程API分为两个部分:Universal Device Family APIs和Extension SDKs APIS。例如如果你的应用只应用于IOT设备,你可以导入IoT extension SDK。
1,Universal device family API contracts
These API contracts are implemented by all Windows 10 operating system editions.
Universal device family API contracts - Windows UWP applicationsdocs.microsoft.com2,Extension SDKS API
例如Mobile扩展SDK
Windows Mobile Extension SDK API contracts for UWP - Windows UWP applicationsdocs.microsoft.com微软通过这种分层组织API方式可以获取如下好处:
- 对于希望部署尽可能的设备上,只允许调用Universal Device API
- 对于只允许在单个设备上的应用,可以调用Extension API
- 提供机制,可以Writing adaptive code with the Api Information class
bool isWindows_Devices_Scanners_ScannerDeviceContract_1_0Present =
Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent
("Windows.Devices.Scanners.ScannerDeviceContract", 1, 0);
3,Android,Wear OS、Android TV、Android Auto,CHROME
对于我们来说,Android是比较熟悉的,Android目前应用设备包括手机,TV,手表等。Android本身为了支持多设备编程也做了不少事情,基本思路跟IOS,微软也是类似的,都有一个适合所有的设备的基础API框架集+扩展API框架集。
- 基础API集合
Android Platform API,AndroidX,
- 设备类特殊API
androidx.leanback:Android TV
androidx.wear: Android Wear
android.car: Android Auto
- Android 提供的适配多设备UI适配机制
布局应该从容应对不同的屏幕尺寸和方向,而不是为布局定义刚性尺寸,假定屏幕尺寸和宽高比是一定的。
https://developer.android.com/training/multiscreen/screensizesdeveloper.android.com- 应用商店支持为每个不同的设备发布不同的APP的能力
4,AliOS
AliOS为多端而生,结合云的能力,实现云端一体化、智能化。支持车机类(Automotive)、电视类(TV)、平板类(Tablet)以及轻量化设备类(Lite)等几类
其为开发者提供的API按照设备类别进行分类。API Level 是对 AliOS 操作系统平台所提供的应用开发框架(Cloud Application Framework,简称CAF)API 的唯一自然数版本标识。API Level 机制严格遵循“向后兼容”原则,即 API Level n(其中 n 是自然数)可以确保完全兼容 API Level 1 至 API Level n-1。
结论:
1,采用基础API + 设备类API的方式组织,相对比较清晰,但是按照设备扩展性后续值得讨论。设备类API通过Kit名称/包名的方式区分,如Apple,微软,Android等。
2,在多设备OS下API本身附带了一些单机OS不具备的属性,是否在API定义的时候显式指定适配的属性,如苹果引入annotation方式进行说明。如Java中引入annotation标记API使用的<设备,OS,编程语言>
针对多设备OS,如何组织API,大家有什么好的意见。