iOS9AppThinning(应⽤瘦⾝)功能介绍
iOS9 发布后,产⽣了⼀个使 App Thinning ⽆法正常运⾏的 bug。在iOS9.0.2 版本中,这个 bug 已经被修复,App Thinning 已经可以正常使⽤。当你从应⽤商店(App Store)下载应⽤时,请注意这点。
iOS9 推出之后,。仅仅数周,已经有超过半数的 iOS 设备安装了这⼀新系统。这是 iOS 历代版本中采纳速度最快的—甚⾄打破了 2013 年iOS7 创下的记录。
关于 iOS9,笔者最近发表了⼀篇名为的⽂章,今天,笔者将继续探讨 iOS9 上另⼀个令⼈兴奋的功能——App Thinning(应⽤瘦⾝)。在这篇教程⾥,我们将探讨 App Thinning 之所以重要的原因,以及如何在你⾃⼰的 App 上使⽤这⼀令⼈兴奋的功能。
根据 WWDC(苹果全球开发者⼤会)的发布,App Thinning 是⼀项可以改变整个下载进程的新技术。许多⽤户反应,流量费⽤过⾼、iOS 设备的存储空间有限,⽽下载速度却不断提升,为了适应这些问题,App Thinning 变得⼗分值得学习。此外,App Thinning 的发布已经推迟了,现在正是学习关于这⼀新技术的最好时机。
前提条件
在本教程中,笔者假设你有 Xcode 实践经验⽽且知道如何使⽤ IDE。如果你对此不甚了解或者不确定 IDE
是什么,也许你会想看看与此相关的。
此外,笔者也假设你知道如何在 App Store 上发布 app,也知道如何使⽤苹果的Beta 应⽤测试服务 testflight。笔者不会具体说明如何使⽤testflight,所以如果你对 testflight 不熟悉或者不清楚上传发布 app 的⼤体流程,笔者推荐你先看⼀看这篇 AppCoda 上的除了这些和你学习的意愿之外,就没有任何前提条件了。
以此为前提,让我们开始吧。
App Thinning 的介绍
现今,市⾯上的 iOS 设备多种多样,屏幕尺⼨和分辨率更是五花⼋门,这导致⼀个app 想在多种屏幕上都能视觉效果良好,就需要⼤量的优化资源(即 PNG 格式,JPEG 格式以及⽮量 PDF ⽂件等)以匹配不同尺⼨的屏幕。更糟糕的是,这导致⽤户的下载量⼤幅增加。(之前的iOS 版本强制要求⽤户下载整个 app ⽂件,即使⽤户使⽤的是 iPhone,也需要下载他们绝不会使⽤到的 ipad 图像⽂件)16g 的iPhone 还是切实存在的(短时间内也不会消失),因此,让你的 App 变得精简⽽且迅速下载可以确保⽤户留有⾜够的空间,并且优化⽤户体验。App Thinning 让这⼀切变得可能。
现今,App 不再是由简单的代码和图像组成了。现代应⽤不仅仅包括可执⾏的代码,还有不同的 32 位,app开发者需要更新此app以在此ios上正常使用
64 位版本(针对如 arm64, arm7s,和 arm7 等多种架构的优化),3D 图形技术(如 OpenGL, Metal 等等),还有⾳频以及其他不同的⽂件。总之,如今 App 功能极其复杂。这就是 App Thinning 发挥作⽤的地⽅。
App Thinning 会⾃动检测⽤户的设备类型(即型号名称)并且只下载当前设备所适⽤的内容。换句话说,如果你使⽤的是 iPad Mini 1(1x 分辨率且⾮ retina 显⽰屏)那么只会下载 1x分辨率(下⽂会有更多介绍)所使⽤的⽂件。更强⼤和更⾼分辨率的 ipad(如iPad Mini 3或4)所使⽤的资源将不会被下载。因为⽤户仅需下载他/她⾃⼰当前使⽤的特定设备所需的内容,这不仅加快了下载速度,还节约了设备的存储空间。
尽管⼀开始听起来可能有点复杂,但是笔者会深⼊了解细节。幸运的是,这项⼯作的⼤部分会由 Xcode 和 App Store 负责完成,这使得开发者的⽣活轻松了许多。因此,本教程的重点在于对 App Thinning 的深刻理解以及其实现技术,并不会有⼤量的代码。
App Thinning 主要有以下三个⽅⾯:App Slicing(应⽤程序的划分),Bitcode以及按需加载资源。在本教程中,笔者会⼀⼀详述每个⽅⾯。
App Slicing
关于 App Thinning,本⽂探讨的第⼀个⽅⾯就是划分问题。根据苹果官⽅⽂献的描述「Slicing 是为应⽤捆绑包创建、分发不同变体以适应不同⽬标设备的过程。⼀个变体只包含针对某个⽬标设备的可执⾏架构与资源。」换句话说,App Slicing仅向设备传送与之相关的资源(取决于屏幕分辨率,架构等等)。事实上,App Slicing 负责处理 App Thinning 的主要流程。
当你准备好提交 app 时,通常会(但必须使⽤ Xcode7,因为它包含⽀持 App Thinning 的 iOS9 SDK)向 iTtunes Connect 上传 .IPA 或
.App ⽂件。然后,应⽤商店分割该应⽤,创建特定的变体以适应性能不同设备。
On Demand Resources
按需加载资源
想要充分理解 App Thinning,你必须理解按需加载资源(On Demand Resources , ODR)。按需加载资源是在 app 第⼀次安装后可下载的⽂件。举例说明,当玩家解锁游戏的特定关卡后可以下载新关卡(和这个关卡相关的特定内容)。此外,玩家已经通过的关卡可以被移除以便节约设备上的存储空间。
开启按需加载资源功能涉及改变 Xcode 中的设置(在编译设置(Build Settings)下),将「启⽤按需加载资源”选中为是」。
Bitcode
App Thinning 的最后⼀个⽅⾯是 bitcode。Bitcode 有些抽象,但在本质上它也是苹果在⽤户下载前优化app的新⽅式。Bitcode 使得 app ⽆论在何设备上都能快速⾼效地运⾏。Bitcode 使⽤最新的编译器⾃动编译app并且针对特定架构进⾏优化。(例如,针对 iPhone 6s和 iPad Air 2等 64 位处理器的 arm64)
Bitcode 不会下载应⽤针对不同架构的优化,⽽仅下载与特定设备相关的优化,使得下载量更⼩,同时与前⽂所述的 App Thinning 技术紧密合作。
Bitcode 是 iOS 上较新的功能,对于新的项⽬需要⼿动开启。这可以通过选择Build Settings(编译设置)下的项⽬设置,将 bitcode 设为YES 来完成。
在⾃⼰的项⽬中应⽤ App Thinning
虽然 Xcode 和 App Thinning 负责处理 App Thinning 的主要流程,但是你还是要采取⼀定的预防措施以确保你的 app ⽀持这项新技术。⾸
先,你必须使⽤资产⽬录。资产⽬录在⼤部分 app 上是默认启⽤的。如果你尚未启⽤资产⽬录,现存的许多⽂件都可以通过下图的⽅式点击Xcode 项⽬设置中“启⽤资产⽬录”转移到⽬录下。
Sprite Atlases 是 Xcode7 中的新特性。Sprite Atlases 本质上就是资产⽬录的组合,也是 SpriteKit(Xcode⽤来创建2D游戏的技术)的核⼼。所以说,如果你使⽤ SpriteKit,那么 app thinning 就是必须的。
App Thinning 的测试
正如上⽂所述,Xcode 和 App Store 负责处理⼤部分的 App Thinning 进程,让这项技术变得真正易于在你的 app 上应⽤。但是,假如你想测试⾃⼰的 app 以确定它已经为 app thinning 做好准备,应该怎么办呢?幸运的是,苹果的 TestFlight 为我们提供了完美的解决⽅案。
在本教程的后半段,笔者会探讨如何在 TestFlight 上使⽤ App Thinning。
⾸先,下载⼏乎空⽩的项⽬,将它解压缩,并在 Xcode 中运⾏。你会发现该项⽬中除了⼏张资产⽬录下的图像(⽽没有⼤量的代码),并⽆太多内容。该资产⽬录也包含了该应⽤图标的 1x、2x、3x 版本。
⾸先,在模拟器或设备上运⾏该应⽤。打开设置应⽤程序,单击「存储空间与 iCloud ⽤量」(在⾮ iOS 的 9 设备上只是「存储」),然后选择「管理存储空间」。向下滚动到刚刚编译的应⽤程序,然后点击它。你会发现它⼤约只有 17.0 MB ⼤⼩(当上传到 iTunes Connect 时⼤⼩可能略有不同)。
aa
当你使⽤ Xcode 编译和运⾏⼀个应⽤时,Xcode 不会⾃动处理应⽤程序变种和 App Thinning。这样,整个应⽤程序⽂件还是在你的设备上。
然后点击 Xcode 的产品选项(Product tab)并且选择存档(Archive)。
注:你有可能需要先修改应⽤程序的 Bundle Identifier 去匹配你⾃⼰创建的程序。否则,该应⽤程序将⽆法上传到 iTunes
Connect 上。
确保你在选择「提交」之前选择了「包含 bitcode」。如果⼀切顺利,你会看到⼀个绿⾊的对号提⽰你项⽬已经成功更新了。
现在登录 iTunes Connect ,创建⼀个新的应⽤程序(填⼊适当的捆绑包 ID,应⽤程序名称等)。如果你不确定怎么做,请参考。
将⾃⼰添加为内部测试员。要注意,在建⽴项⽬时数个⼩时都处于「处理中状态并不寻常。⼀旦应⽤程序处理完毕,就选中它并按下开始测试」。
你将会收到⼀封电⼦邮件。请确保使⽤你想测试的 iOS 设备,并接受邮件邀请。你会被带到 TestFlight 应⽤。
安装这个版本。完成之后,返回到应⽤程序设置,浏览存储,到我们之前的应⽤程序。发现这个应⽤程序现在仅有 5.4MB 了。这就是App Thinning 的杰作!
哇!你的⼀个⾮常精简的应⽤程序都节省了 12.4MB 的存储空间。那么,包含多个不同资源的应⽤程序将会得到更加显着的⼤⼩变化!
参考链接:
2. 官⽅⽹站链接:
官⽹⽂档
App thinning overview (iOS, tvOS, watchOS)
The App Store and operating system optimize the installation of iOS, tvOS, and watchOS apps by tailoring app delivery to the capabilities of the user’s particular device, with minimal footprint. This optimization, called app thinning, lets you create apps that use the most device features, occupy minimum disk space, and accommodate future updates that can be applied by Apple. Faster downloads and more space for other apps and content provides a better user experience.
Slicing (iOS, tvOS)
Slicing is the process of creating and delivering variants of the app bundle for different target devices. A variant contains only the executable architecture and resources that are needed for the target device. You continue to develop and upload full versions of your app to iTunes Connect. The App Store will create and deliver different variants based on the devices your app supports. Use asset catalogs so that the App Store can select images, GPU resources, and other data appropriate for each device variant. When the user installs an app, a variant for the user’s device is downloaded and installed.
Xcode simulates slicing during development so you can create and test variants locally. Xcode slices your app when you build and run your app on a device or in Simulator. When you create an archive, Xcode includes the full version of your app but allows you to export variants from the archive.
Note: Sliced apps are supported on devices running 9.0 and later; otherwise, the App Store delivers universal apps to customers.
Bitcode
Bitcode is an intermediate representation of a compiled program. Apps you upload to iTunes Connect that contain bitcode will be compiled and linked on the App Store. Including bitcode will allow Apple to re-optimize your in the future without the need to submit a new version of your app to the App Store.
For iOS apps, bitcode is the default, but optional. For watchOS and tvOS apps, bitcode is required. If you provide bitcode, all apps and frameworks in the app bundle (all targets in the project) need to include bitcode.
Xcode hides your app’s symbols by default, so they are not readable by Apple. When you , you have the option of including symbols. Including symbols allows Apple to when you or . If you’d like to collect and crash reports yourself, you don’t have to upload symbols. Instead, you can after you distribute your app.
On-Demand Resources (iOS, tvOS)
On-demand resources are resources—such as images and sounds—that you can tag with keywords and request in groups, by tag. The App Store hosts the resources on Apple servers and manages the d
ownloads for you. The App Store also slices on-demand resources, further optimizing variants of the app.
On-demand resources provide a better user experience:
App sizes are smaller so apps download faster, improving the first-time launch experience.
On-demand resources download in the background, as needed, while the user explores your app.
The operating system purges on-demand resources when they are no longer needed and disk space is low.
For example, a master-detail app may divide resources into levels and request the next level of resources only when the app anticipates that the user will move to that level. Similarly, the app can request In-App Purchase resources only when the user makes the corresponding in-app purchase.
Note: If you (outside of the App Store), you must host the on-demand resources yourself.
To set up on-demand resources in your app, read and .
Was this help page useful?