Content Source:On May 25, 2017, Aurora Senior Android engineer Kewei Wang delivered a speech titled “Characteristics and Experience Sharing of Mobile TERMINAL SDK Optimization” at aurora Developer Salon. IT big said as the exclusive video partner, by the organizers and speakers review authorized release.



Read the word
Reading: 2098 | 4 minutes



Abstract

Based on aurora’s business and development experience, aurora senior Android engineer briefly introduced the differences between mobile SDK and APP, as well as some experience in architectural design and performance optimization.

Guests share video address

t.cn/RK1X3gj

The difference between SDK and APP

Repeat the wheel

We have a saying in APP development: “Don’t reinvent the wheel.” It means you want to iterate quickly, and each APP has its own unique functionality. The basic business can be imported directly using frameworks already made by others or third-party packages, but the business code must be unique to you.

There is a significant difference between SDK and APP in size. The core code of APP only needs to do the above small piece, but the result is that the size of an APP itself is tens of megabytes or even more than a hundred megabytes, which users can accept. The SDK needs to be small, within a few hundred K, to be acceptable to developers.

So the SDK can’t just use someone else’s “wheel”. For basic services such as web and database, native Android apis can be used to keep the volume within a smaller range, so APP developers can use smaller packages to do their business. It has the advantage of keeping code lean and internally visible, making it easy to debug and modify.

We had to “reinvent the wheel” for size, as well as for architecture and performance.

configuration

The APP simply requires the developer to write a list of configurations based on their own needs.

The permission of SDK is given to developers. We have no control over what permission or version developers use. We can only tell them how to configure it in the form of instructions.

So we have to think about configuration to make it as easy, friendly, flexible as possible.

upgrade

If an APP needs to be upgraded, develop a new version and put it on the APP store to replace it, or push update notification to ensure that users can use the new version quickly. At the same time, the online version range is relatively small, and the share of the old version slowly decreases.

The SDK doesn’t go directly to the market, but requires developers to take the initiative to get the files, integrate them into their own projects, and then put them on the market. Some developers don’t update for months, or they don’t update at all, which leads to the existence of many old versions at the same time. This problem requires a lot of compatibility considerations, so reliability and performance considerations are more important than new features.

Architecture optimization for aurora SDK

The old architecture

Before 2016, Aurora mainly promoted two SDKS, one is Jpush, the other is JMessage.

Push and IM in the old architecture are two separate SDKS with a lot of redundant code. The disadvantages are large footprint, repeated operations, channel and thread resources, and the upgrade management of redundant code is very troublesome.

The new architecture

After expanding our business, we also added statistics and sharing. Considering multiple business lines, we made structural adjustment and stratified business and core.

JCore is responsible for the common functions of the core, and the upper SDK runs its own business on JCore, which makes the structure clearer, helps to expand shared resources, reduces repetitive actions, and makes targeted basic optimization more convenient.



Need to be considered in actual development

When communicating with the server, the protocol should be made compatible with different components and different versions online at the same time. The policy class involved in code design should adopt the policy pattern, which is convenient to replace the better policy.

The core component and the upper component are divided into two, and more interfaces are needed to solve the problem of upgrade asynchronism, which should be concise and adapt to changes.

And about the communication between various components, through the command mode, the action is abstracted into outgoing objects, to achieve distribution and buffering.

In the engineering design of Android development, some interfaces are inevitably exposed because they are split into two packages, but not fully exposed to protect the code. So there’s a trade-off between convenience and protection.

Because many components are introduced, Jenkins+Gradle can be used when packaging and compiling scripts, scripting and integration, and multiple packages can be automatically printed.

Aurora SDK performance optimization

Multiprocess versus multithreading

Multithreading is a basic skill of language. Normally, business is executed in the main thread. However, heavy tasks in the main thread of mobile terminal will lag and affect user experience, so it should be restrained as much as possible. Therefore, in the case of occupying more resources and time consuming, it is necessary to open another thread.

Android applications are designed with processes as a valuable resource, and it tries not to leave process management to the developer, but to the system.

If a single-process application has a large number of tasks and high memory usage, multiple processes can share some task resources with other processes to avoid resource reclamation. Another benefit is that the main process running in the background is reclaimed by the system for some unpredictable reason, and the tasks running in the main process can continue to execute.

After multiple processes are enabled, memory space is independent and may be initialized on the main process, but not on the child process. You don’t realize it when you write the code, and you don’t expect it when you run it. That’s the problem with data being out of sync.

Data synchronization is a problem that both the main process and the line process can encounter. How to skillfully use its performance without error? I personally often use double-checked locking, which looks more complex code, but is conducive to better performance and less prone to data synchronization problems.

Variables are not synchronized in multi-processes. Therefore, variables shared across processes need to be read and written in the same process through inter-process communication mechanism. Although some performance loss is caused, data correctness can be ensured in this way.

storage

The same process can be read once and used multiple times.

Write operations can be committed in batches.

Memory level storage is used for faster response.

Batch reads and commits across processes.

Split the storage.

Long link optimization

We do aurora push, push the main tasks are in the long link. To communicate with the server, the client must first access the service.

We have an SIS service, which is to open up another server to find the device at that time. We will send a list to the clients for them to try on their own.



To optimize performance, you can first cache the list locally, and then use the address delivered by SIS if this fails. Then write a selection of policies, prioritizing what is available and eliminating what is not.

In the access service part, the server will be reported to the current situation, so that the server can make adjustments according to the reported data feedback.

This is the end of my sharing, thank you!

Upcoming events

2017 Big Data and Artificial Intelligence Technology Conference is hosted by netease Artificial Intelligence, netease Cloud, IT Big Jia Said, Erudite Education, and co-organized by Qiuniuyun, Hadoop123, Caiyun and DBAPlus. IT has become a widely watched industry technology conference. Conference will be gathered many industry leaders, experts, Daniel, well-known media, etc., continue to focus on large data exploration and application of artificial intelligence, displaying the latest big data and artificial intelligence technology, innovation and achievement for the technical practitioners and enthusiasts to provide valuable communication platform, to build a large data feast with the technology of artificial intelligence.

Offline Address:

Report Hall, Netease Building, No. 599 wangshang Road, Binjiang District, Hangzhou

Live broadcast Address:

Scan the code to sign up, or click t.cn/R9mqzEF to watch the live stream!