preface

++ The speed at which an App starts can affect the user’s first experience, and users expect the App to be responsive and fast to load. Apps that take too long to launch do not meet this expectation and may disappoint users. This kind of poor experience can result in users giving your app low ratings in the App Store, or even abandoning your app altogether. ++

== The main contents of this session include: ==

What have we done to optimize App launch?

1. Optimization direction of App startup: optimization of visual experience

2. App startup optimization direction: code logic optimization

I. Optimization direction of App startup: optimization of visual experience

The App starts with a blank screen

App startup stage:

  1. Load and start the application.
  2. A blank startup window that displays the application immediately after startup.
  3. Create the application process.

The problem with launching a white screen is in phases 1 or 2, because apps start with a SplashActivity to display App information. We can optimize the visual blank screen by setting the theme of the launch window.

1. Default theme

By default, no processing is done for App. Once the default theme is set, a blank screen will be displayed during App startup and initialization, as shown in the figure below:

2. Transparent themes

In order to solve the problem of white during launch window, subject to the solution, by setting the start page for transparent, although did not have white, but our App seems to be dull, look carefully and click on start the App icon, App appears to be paused, then loaded our welcome page, a bit like ANR, just very short, so the user experience is not beautiful, The phenomenon is shown as follows:

<style name="NormalSplash" parent="AppTheme">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
Copy the code

3. Set the theme of the flash screen picture
<style name="NormalSplash" parent="AppTheme"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowBackground">@drawable/welcome_layler_drawable</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowFullscreen">true</item> <! - display virtual buttons and make room -- > < item name = "android: windowDrawsSystemBarBackgrounds" > false < / item > < / style >Copy the code

Welcome_layler_drawable. XML source code:

<? The XML version = "1.0" encoding = "utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/welcome_background" android:drawable="@drawable/icon_splash_bg" /> <item android:bottom="@dimen/dp_16" android:gravity="center"> <bitmap android:gravity="center_horizontal" android:src="@drawable/icon_splash_word" /> </item> <item android:bottom="@dimen/dp_41" android:gravity="bottom"> <bitmap android:gravity="center_horizontal|bottom" android:src="@drawable/icon_splash" /> </item> </layer-list>Copy the code

2. App startup optimization direction: code logic optimization

1. Application optimization:

As the entry of the entire initialization configuration of an Application, many third-party components (including the App itself) perform initialization operations in the Application. Therefore, completing various initialization operations and complex logic in the Application will affect the startup performance of the Application

Too many initialization tasks, consider the following optimizations:

  1. Consider asynchronously initializing three-party components without blocking the main thread;
  2. Delay initialization of partial-party components;

The optimization scheme is as follows:

The component is initialized in a child thread:

new Thread(new Runnable() {
            @Override
            public void run() {
                setThreadPriority(THREAD_PRIORITY_BACKGROUND);
                initARouter();
                CacheManager.getInstance().initialize(getInstance());
                ConnectionManager.getInstance().initialize();
                initImageFactory();
                initBJY();
                initGrowingIO();
                initUmeng();
                initBugly();
                initOkHttp();
                initSobot();
                setRxJavaErrorHandler();
            }
        }).start();
Copy the code

Lazy loading controls that need to be initialized in the main thread but can be used independently:

Handler. PostDelayed (new Runnable() {@override public void run() {// Delay initialization component}}, 3000);Copy the code

== Note: == Not every component initialization and operation can be asynchronous or deferred; It depends on the calling relationship of components and the specific business needs of your project. Guarantee one rule: asynchrony if you can, and delay if you can’t. Let the application start first, then operate.

// The child thread initializes the third party component // It is recommended to delay the initialization, you can find out whether other functions are affected, or crash! Thread.sleep(5000);Copy the code

2, flash screen Activity optimization:

Ui-level optimization for Activity:

UI layout before optimization:

<? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/icon_splash_bg"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_splash_word" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:paddingBottom="160dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@mipmap/icon_splash" android:layout_alignParentBottom="true" android:layout_marginBottom="@dimen/dp_41" /> <com.pxwx.student.modulecore.widget.TouchRelativeLayout android:id="@+id/rl_adsRl" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal|top" android:orientation="vertical" > <ImageView  android:id="@+id/iv_SplashAd" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:contentDescription="@null" android:scaleType="fitXY" android:visibility="gone" /> </com.pxwx.student.modulecore.widget.TouchRelativeLayout> <TextView android:id="@+id/tv_adjump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ad_jump_selector"  android:gravity="center_vertical|center_horizontal" android:layout_alignParentRight="true" android:layout_marginRight="@dimen/dp_18" android:layout_marginTop="@dimen/dp_30" android:paddingBottom="@dimen/dp_5" Android :paddingLeft="@dimen/dp_11" Android :paddingRight="@dimen/dp_11" Android :paddingTop="@dimen/dp_5" Android :text=" skip  3" android:textColor="@color/white" android:textSize="@dimen/font_15" android:visibility="gone" /> </RelativeLayout>Copy the code

Simplified:

<? The XML version = "1.0" encoding = "utf-8"? > <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/welcome_layler_drawable"> <ViewStub android:id="@+id/vs" android:layout_width="match_parent" android:layout_height="match_parent" android:layout="@layout/layout_stub_avd" /> </FrameLayout>Copy the code

ViewStub initialization delay

For splash ads in a project, delay their initialization through the ViewStub and display the real view through the ViewStub’s inflate when needed, optimized as follows

<ViewStub
    android:id="@+id/vs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout="@layout/layout_stub_avd" />
Copy the code

Open screen advertising business layout extraction

layout_stub_avd.xml

<? The XML version = "1.0" encoding = "utf-8"? > <! - qi advertising screen page view - > <. Com. PXWX. Student modulecore. Widget. TouchRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_adsRl" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_SplashAd" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:contentDescription="@null" android:scaleType="fitXY" /> <TextView android:id="@+id/tv_adjump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginTop="@dimen/dp_30" android:layout_marginRight="@dimen/dp_18" android:background="@drawable/ad_jump_selector" android:gravity="center" android:paddingLeft="@dimen/dp_11" android:paddingTop="@dimen/dp_5" android:paddingRight="@dimen/dp_11" android:paddingBottom="@dimen/dp_5" Android :textColor="@color/white" Android :textSize="@dimen/font_15" /> </com.pxwx.student.modulecore.widget.TouchRelativeLayout>Copy the code

You then inflate when you need to display the WebView in your code:

/** * private void showAvd() {viewStub = findViewById(R.I.S); if (viewStub ! = null) { viewStub.inflate(); mAdRl = findViewById(R.id.rl_adsRl); mAdImage = findViewById(R.id.iv_SplashAd); mAdJump = findViewById(R.id.tv_adjump); }}Copy the code

Point of optimization:

  1. Discard the previous start page UI layout and use the previously customized Welcome_layler_drawable as the start page background
  2. Extract and separate the screen advertising Ui
  3. Lazy loading of AD views

OnCreate business logic optimization:

  1. Reduce advertising and other business logic time here belongs to business logic optimization.
  2. OnCreate for advertising business initialization business optimization, asynchronous download pictures, such as the next startup control display

conclusion

Universal application start acceleration routines

  1. Use theme quick display interface;
  2. Initialize components asynchronously;
  3. Combing business logic, delaying initialization of components and operations;
  4. Proper use of threads;
  5. Get rid of useless code, repetitive logic, etc.

Question:

1. How to calculate the starting time?

2, why there will be a white screen?

3. Why is this optimization effective?