Apps are optimized to make them faster, more stable, and smaller. There are a lot of articles on the web, but many of them only record the most recent ones because the time spent is not proportional to the level of optimization, or many have been avoided in the development process.

faster

Start the white

<! < span style = "color: RGB (51, 51, 51); font-size: 14px! Important;"SplashStyle" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item> <! If you want a more rounded effect, you can use the background image of the launch page --> <item name="android:windowBackground">@color/theme_color</item> <! -- With this property will remove fringe --> <item name="android:windowFullscreen">true</item>
    </style>
Copy the code

Change the theme of the startup page to

   <activity
            android:name=".SplashActivity"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:theme="@style/SplashStyle"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Copy the code

Pay attention to

  • Some people may change the MainActivity theme directly, which can cause problems, and the background needs to be changed after the startup. And can not be set to remove the bangs of the property, some phones do not match, bangs are white. Maybe I didn’t find a way to control the bangs myself. Hope big guy gives directions
  • When jumping to MainActivity, do not drop the start page finish directly, otherwise there will be a blank screen for a moment. Need to delay a few seconds finish
    // Delay destruction to avoid white screen
   new Handler().postDelayed(() -> {
       finish();
   }, 3000);
Copy the code
  • It is best to change the default system jump animation when jumping, the effect will be more smooth
  ARouter.getInstance().build(RouterPath.MAIN)
                                .withTransition(R.anim.common_popup_fade_in, R.anim.common_popup_fade_out)
                                .navigation(this);
Copy the code
The < alpha XMLNS: android = "http://schemas.android.com/apk/res/android" android: duration = "300" android: fromAlpha = "1.0" Android: interpolator = "@ android: anim/decelerate_interpolator" android: toAlpha = "0.0" / >Copy the code
The < alpha XMLNS: android = "http://schemas.android.com/apk/res/android" android: duration = "300" android: fromAlpha = "0.0" Android: interpolator = "@ android: anim/decelerate_interpolator" android: toAlpha = "1.0" / >Copy the code

Lazy loading

/** * Off-screen information latency */ private voidinitDelayAddIdleHandler (() -> {// return MessageQueue looper.myQueue (). AddIdleHandler () -> {// return MessageQueue LooperfalseNo more listening in the future.return faslse;
        });
    }
Copy the code

The addIdleHandler method is executed when the Handle thread is idle. Basically, it is executed after the interface is displayed. Specific principles can see mp.weixin.qq.com/s/KpeBqIEYe…

Asynchronous loading

For some code that does not need to be executed in the main thread, such as SP, database code can be initialized in the child thread.

Layout optimization

See if there is too much drawing and too many levels of nesting. This usually saves a lot of work when you’re writing code by paying attention later.

  • LinearLayout and FrameLayout perform better than RelativeLayout at the same level.
  • When writing a layout, avoid setting background as much as possible
  • Flexible use of the ViewStub, instantiations are loaded only and only once.

Some of the other

Click event optimization

Many apps do not do click error processing, so sometimes there will be two interfaces when you click twice. Although the impact is not significant, in order to make the user experience better. It’s better to do it right from the start

  1. Use RxBind to see github specifically
  2. Using utility classes
public class ViewOnClickUtils { private static final int MIN_DELAY_TIME= 1000; Private static long lastClickTime; /** * Determine whether to quickly click * @return
     */
    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        returnflag; } /** * Determine whether to quickly click * @param time to set the time * @return
     */
    public static boolean isFastClick(int time) {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= time) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        returnflag; }}Copy the code
     tv.setOnClickListener(view -> {
        if (ViewOnClickUtils.isFastClick()) {
            return;
        }
        // Logical operation. }Copy the code

Image compression

Smaller images mean faster loading and faster uploading.

  • In the past, when outsourcing companies do, THE UI to the picture is very large, not to do the maximum compression, a background picture hundreds of K, when loading back to memory jitter, so it is best to call UI or go to the Internet to find their own compression.
  • Image uploads should also use compression, a great open source framework for image compression github.com/Curzibn/Lub…

A more stable

Avoid memory leakage and memory jitter. I personally think it’s related to writing code

Pay attention to

  • Where you need to pass in the Context, you need to pay attention to whether the lifecycle is consistent. For example, singletons should use ApplicationContext
  • Unregister listeners when they need to be destroyed, or stop writing custom views when looping animations are destroyed.
  • Avoid creating a large number of objects in a short period of time, such as when writing lists, and use singletons instead or make a judgment call.

We need some tools to help troubleshoot the problem

  • LeakCanary is often used to see if there is a memory leak. It’s usually plugged in from the start. Access can see blog www.jianshu.com/p/70b8c87ea…
  • Some other tool is introduced, for example as own monnitors and MAT (memoryAnalyzer: official eclipse tools) www.jianshu.com/p/d71b51a0e…

Corruption problems

Although this was filtered out during the testing phase, it could crash in real use for reasons such as data errors. At this time, we’d better access Bugly. The access method can be found on the official website. Bugly will record every crash. But Bugly will only push it once a day. If we need to do some early warning, we can monitor the crash of the app and send a notice to the nail robot or our own background so that we can do hot repair.

Nail group screenshots are as follows, the specific implementation can see the corresponding official documents and blogs. It’s relatively simple with no technical content, just a solution.

smaller

Can eliminate useless layout, resources, and library plus code confusion. Companies probably don’t use plug-ins and I’m not very familiar with them.

Code confusion

I don’t want to talk too much about it, but the most important thing to learn about obfuscation is to know what should not be obfuscated.

Confusing reference blogs

  1. Android Studio comes with its own confusion
  • An article on the principle of confusion
  • A practical application article
  • There are some confusion rules in there
  1. Third party confusion
  • Slimming launched by Tencent

Recommended reading

Check out his series of articles on performance optimization design patterns and more.