The latest practice of Android development (translation)

This article is translated from Amit Shekhar (MindOrks Company)'s blog post Android Development Best Practices for your reference.
Original link:
https://blog.mindorks.com/android-development-best-practices-83c94b027fd3

Let's examine some best practices for Android application development.

For starters, when you develop an Android application, you need to take into account a variety of devices and operating environments.

Every Android device is a whole in itself, and if there is a problem with any module, other parts will also be affected.

When an application requests memory, if there is no free memory available for allocation at this time, the system will kill some processes. When the user returns to the application of these killed processes, the application will be restarted.

If an application takes up too much memory, CPU, GPU, or consumes too much power, other applications will also suffer from it, resulting in a poor user experience. Therefore, performance matters. If you want to learn about Android performance, please click here .

Beware of little expenses. A small leak will sink a great ship.  — Benjamin Franklin

Here are some best practices you should follow when building Android apps.

1. Use the recommended Android framework

If you don't follow the code guidelines and don't use any framework at the beginning, as the application functions continue to increase, you will face the following problems:

2. Constantly maintain code quality

Code quality is very important. Not only involves frameworks such as MVP/MVVM/MVC, but also involves all aspects of the entire application code. Please follow the link about Android coding style and guidelines.

3. Find and fix memory leaks

Check resources and find and fix memory leaks:

Practical Guide To Solve OutOfMemoryError in Android Application
Detecting and fixing memory leaks in Android

4. The release version should use code obfuscation

After using code obfuscation, useless code will be removed, which can reduce the size of APK. Android code obfuscation click here .

5. Use debugging tools

I especially recommend using the Android Debug Database . The Android Debug Database library is used to debug and directly browse and view the Android application database and shared preferences. It is easy to use and powerful. The Build Analyzer Tool is also worth looking at.

6. Use strings.xml

String resources are more useful, especially when the application needs to be maintained for a long time, especially when there are multiple language requirements.

7. Try to reuse the layout

Create a separate layout file and use the include tag to reference it in the xml file. Another handy tag is merge , which removes unnecessary containers.

8. Place the launcher icon in the mipmap directory

9. Use shape and selector more

Try to use shape and selector, use less image, and use it to reduce the APK size.

10. Avoid too many layout levels

The deeper the layout level, it will not only make the layout difficult to control, but also cause the UI to slow down. Using the correct ViewGroup can reduce the layout level, such as Constraint Layout (Constraint Layout), related learning resources are as follows:

10. Use the HTTP library

Depending on your specific situation, use an HTTP third-party library, such as Retrofit or Fast Android Networking.

11. Try to use Parcelable instead of Serializable

When using Intent or Bundle to transfer data, try to use Parcelable instead of Serializable. Serialization by implementing the Parcelable interface is much faster than Java's default serialization. Objects of classes that implement the Serializable interface are serializable, while Java's implementation uses reflection (making it slower). When the Parcelable interface is used, the entire object will not be automatically serialized, but the user can selectively add object data, and the added object data can be taken out when deserializing. Similarly, Parcelable is also available in Kotlin.

12. Do not perform IO operations on the UI thread

File operations should always run in a background thread, such as using RxJava or Kotlin coroutines . File operations are time-consuming, and if executed on the main thread, the application will feel unresponsive. If it takes more than 5 seconds in the main thread, the application will cause ANR and a warning window will pop up for the user.

12. Fully understand Bitmap

Because Bitmap occupies a lot of memory, it may cause OOM. Users love content, especially content that is organized and looks great. For example, a picture is worth a thousand words. But images consume a lot of memory. Click here to learn more. Learn how to load images in Glide or Fresco framework in Android .

13. Fully understand Context

Understand what the Context in Android is. Correct use of Context can avoid memory leaks. Click here to learn more.

14. Use styles

In the layout, to prevent repeated setting of attributes, you can use style.

15. Treat the Activity life cycle correctly

Learning how to use the Activity life cycle correctly can solve most of the problems encountered in Android development. Click here to learn more.

16. Unit testing

The most important thing in application development is unit testing. I recommend running unit tests on the JVM, which is much faster than running them on an Android device or emulator. If you need to mock some objects during testing, please use Mockito . Using a dependency injection framework (Dagger) can make testing easier.

17. UI functional testing

Functional testing is to test the functionality of the application from the user's point of view. If your application does not interact with other applications, you can use Android Instrumentation ; otherwise, use UIAutomator .

18. Optimize build speed

Slow build projects slow down your development. Click here to learn how to optimize build speed.

19. Focus on application security

Applications can be decompiled and reverse engineered. It is every developer's responsibility to do their best to make the application secure. Only when the application is safe can it gain the trust of users and ensure the integrity of the device. Click here to learn how to make your app safe.

20. Make the app bug-free

A crash reporting library (such as Firebase Crashlytics) should always be integrated into the app. Have the ability to analyze crash reports.

21. Use mature frameworks

In general, use mature frameworks rather than your own solutions.

22. Adapt to all devices

Don't just adapt to a certain device.

23. Test on different system versions

Test your app on different system versions.
And again, finally, keep testing on various OS versions.

The so-called gift of roses has a lingering fragrance . If you like this blog post, you can share it with other partners.
insert image description here

Guess you like

Origin blog.csdn.net/wudexiaoade2008/article/details/121457252