By Jiang Yi (Qinghan)
In 2019, we completed the research, infrastructure construction, and smooth implementation of Swift for the Taobao Mobile for iOS app.
As a mothership-type app, Taobao Mobile has an organizational structure and engineering structure far more complex than those of ordinary apps. Hence, it is difficult to develop new practices. Therefore, to develop a clear guide to Swift implementation, many Swift enthusiasts in the group and middleware owners had to work together.
In 2018, Swift announced that its priority is to achieve the application binary interface (ABI) stability. Although the syntax has remained basically unchanged since Swift 4.x, Taobao Mobile has been limited by the fact that it is a mothership-type app and project delivery is primarily based on binary componentization. Due to Swift's ABI instability, the Swift Toolchain version had to be fixed, which added about 3 MB to the package size. Therefore, it was inappropriate to introduce Swift at that time.
On 25 Mar2019, Apple released Swift 5.0 and announced its ABI stability achievement. This gave the team new confidence in Swift. As the architecture team controls the introduction of new technology, they decided to divide the research work concerning Swift to different team members in their spare time. In non-work hours, we completed the research work and reached two important conclusions.
We used crawlers to analyze the top 1,000 apps in the App Store rankings in China and abroad, and obtained results through file scanning and analysis.
Apps that use Swift account for about 22% of apps in China and 78% in the US. Most of the apps that do not use Swift in China and the US are products made in China, such as TikTok and Kuaishou. This tells us that Swift apps are still a minority in China, but they are the status quo abroad.
In the GitHub and Stack Overflow communities, Objective-C open-source libraries and problem discussions have stagnated. In the future, we may not choose Objective-C when implementing new technologies. Since WWDC 17, Apple no longer provides any Objective-C examples, and our team members have repeatedly encountered Objective-C bugs. Moreover, when we try to figure out these bugs through the help of the community, there is no apt response. Apple released four Pure Swift frameworks during WWDC 19, which, however, cannot be directly mixed and matched with Objective-C.
In the future, we are likely to see technology failures due to Apple's forced promotion style and backward community culture. We may not even be able to recruit engineers who use Objective-C.
Swift has been an advanced language since its inception. It offers three main features.
|New features||Tuples, closures, generics, namespaces|
|Advancements||Function improvement, higher-order functions, more powerful and flexible instruct, and enum, more advanced control flow (such as defer, range operators, and switch case for any type)|
|Security||Type security, no implicit conversion, easy problem identification during compilation, guard, and optional protection|
|Package size||10% less in size after compilation (after being stripped)|
|Encoding efficiency||Low-footprint code, coding volume reduced by 20% to 30%|
|Performance||No dynamic distribution and faster function calls|
|Programming paradigm||Protocol-oriented programming, functional programming, SwiftUI, and Combine|
The following figure shows the code reduction of a redeveloped business module:
Although Swift provides ABI stability in version 5.0, it is imperfect in earlier operating systems (iOS 12.2 and earlier).
For operating systems earlier than iOS 12.2, we must increase the package size by about 3 MB. Fortunately, Apple now allows downloads of up to 200 MB through the cellular data network. Also, iOS 13 supports downloads greater than 200 MB.
It takes another 100 to 200 milliseconds to start on an operating system earlier than iOS 12.2. However, the team worked hard to perform binary layout optimization, which significantly improves startup performance.
For a more detailed analysis, see: New Practices of the Taobao Mobile Architecture Team | Improve iOS Startup with Binary Layout Optimization Based on Static Library Instrumentation.
After fully considering the advantages and future of Swift, we made up our minds to use Swift. After all, all iOS developers know that Swift is the future. However, we needed to proceed in an organized and productive manner so that this comes to reality faster.
At the basic meeting, we discussed some plans and core tasks for Swift implementation. This covered Swift infrastructure, base library adaptation, Xcode upgrade, and new frameworks such as SwiftUI, Combine, and containerized architecture. We gradually disassembled the project into separate tasks for implementation.
Just as we were studying Swift, Apple announced the launch of the next-generation declarative UI layout framework "SwiftUI" across the Apple platform at WWDC 19. I was among the first to fully study SwiftUI's features. Its simple, clear DSL and expressive data stream management can reduce the volume of code in traditional Cocoa imperative programming by 80% and greatly improve R&D efficiency. A code sample is shown below:
At the same time, we always needed someone to monitor the data dashboard. Sometimes, we even had to check it beyond working hours. To do this, we had to carry our work computers everywhere. Obviously, we desperately wanted a mobile app to take over this job, and the new SwiftUI technology helped us quickly implement a prototype.
However, there was no backend support at that time. The GAIA platform, which is similar to the Function-as-a-Service (FaaS) platform in style, came into our sight. Plus, Swift is a cross-platform language. IBM long ago introduced a serverless service. Therefore, we used Swift to implement a set of FaaS services in compliance with GAIA specifications to provide backend support for our data dashboard.
Many articles that analyze the core implementations of SwiftUI and FaaS are already available, so I will not go into this topic here. For more information, click one of the links below.
Through our research and usage analysis, we found that, although the performance of SwiftUI in initial UIKit or APPKit was slightly worse than Flutter, the DSL and other features of SwiftUI allowed more concise programming. We believe that Apple will continue to make improvements in future versions. Our current technical know-how will allow us to easily cope with the implementation of SwiftUI in the future.
However, after years of iteration, Taobao Mobile accumulated some legacy issues, such as the non-standard use of Cocoapods, non-unified management of header files, and outdated engineering templates. To verify whether potential engineering problems prevent Swift implementation, we tried to develop an SDK with a moderate amount of code to test Swift's R&D efficiency.
After the preliminary SDK redevelopment, we found that a large number of problems remained in the existing project. As a result, we were not able to use Swift in this project at all.
The main problems were as follows:
To solve the problems in the Swift implementation project, we leveraged the power of the Group's horizontal Swift organization and called on Swift enthusiasts to investigate adaptation technologies, compile adaptation documents, and provide automation tools. We then combed through a large number of Swift adaptation documents and had the middleware personnel get rid of the legacy issues without affecting the business iteration. This allowed us to meet the requirements for Swift.
To date, we have adapted more than 100 base library modules and started the forced packaging of bayonets. This is an effective long-term solution to legacy issues. While resolving historical issues in some modules, we also improved the packaging and R&D efficiency.
The development model used by Taobao Mobile is different from that of other small and medium-sized apps. We have a complex development team, and the delivery method mainly involves packaged SDK binary integrations. However, only Swift 5.1 supports module stability. Therefore, when Apple officially launched Swift 5.1 in September, we started upgrading to Xcode11.
For apps like Taobao Mobile, upgrading Xcode on a yearly basis produces a lot of Break Change Futures. We studied the adaptation solutions in the official documentation and had business teams fix a large number of bugs and adapt to a great deal of Futures. Anyway, we managed to complete the adaptation of Xcode11 in early November.
We upgraded Cocoapods to version 1.7.5 because only version 1.5.x supports the Swift static library. This also significantly improves the packaging efficiency of the SDK.
|Xcode 10 + pod 1.2.0||Xcode 11 + pod 1.7.5|
|Swift 5.1||Cannot support the Swift environment||Implement multiple business applications and achieve forward-compatibility|
|DarkMode||Xcode10 cannot use the DarkMode API||DarkMode is available for business adaptation|
|Minimum packaging time||About 8 minutes||5.5 minutes|
|Average packaging time (for debugging)||14.5 minutes||8 minutes|
|Average packaging time (for release)||14.2 minutes||9.7 minutes|
|Construction time of the source-code environment (for initial pod installation)||3 minutes||1 minute|
|Construction time of the source-code environment (for incremental construction)||1 minute||20 seconds|
Swift is a relatively static language. Unlike Objective-C, which uses
msg_send for dynamic scheduling, a static language needs to access the static layout information of other data structures that depend on SDKs. In addition, the Taobao Mobile R&D methodology involves the delivery of binary dependencies, which may cause lower-layer SDK upgrades without notifying upper-layer SDKs to perform upgrade and recompilation. This means binary incompatibility problems may occur in online versions. Swift 5.1's library evolution function helps avoid binary incompatibility.
The following API changes can remove the binary incompatibility:
After completing the aforementioned basic work, we officially released the new Taobao Mobile version with the redeveloped SDK after regression testing. After release, the number of service calls was equivalent to the Taobao Mobile Unique Visitors (UVs) and no crashes occurred. Our developers were greatly encouraged by this because they had successfully implemented a Swift business application on their own initiative.
During the initial implementation, I reviewed a large amount of code and found that Objective-C influenced everyone's coding style. They even introduced syntax such as forced unpacking because the developers were not used to the optional types. Such syntax may pose security risks and lead to crashes. We have learned that many people can understand the Swift syntax but cannot adapt to the Swift style and mix-and-match environment. To help developers adapt to the Swift development environment, we have brought together some Swift experts and designated a series of courses. In the future, we will consider offering these courses to other developers in the industry.
The Taobao Mobile team produced a large number of adaptation documents, tools, tutorials, and methodologies during the Swift implementation process. We are pleased to see that we achieved the goal we set for ourselves in early 2019!
In the six years since Swift was born, developers have been constantly touting it as the language of the future. However, the efforts of the Taobao Mobile architecture team show that Swift is no longer the future, but the present.
Next, we plan to provide a progressive SDK migration solution to gradually migrate the legacy SDKs to Swift. Swift's R&D efficiency and security may help facilitate rapid business development.
Alibaba Clouder - February 11, 2019
Aliware - May 20, 2019
淘系技术 - October 28, 2020
淘系技术 - April 14, 2020
XianYu Tech - August 10, 2021
Alibaba Cloud Community - September 26, 2022
Alibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.Learn More
Help enterprises build high-quality, stable mobile appsLearn More
Provides comprehensive quality assurance for the release of your apps.Learn More
Accelerate software development and delivery by integrating DevOps with the cloudLearn More
More Posts by 淘系技术