In this article I would like to briefly tell you about the latest best practices from Google. I tried to highlight the most basic points so that the reader could immediately understand what exactly any feature gives the developer. Do not be surprised if I repeat somewhere. I outlined + added from myself as I watched the video at
www.youtube.com/channel/UCVHFbqXqoYvEWM1Ddxl0QDgAlso, each item contains all the necessary links for more detailed acquaintance with the specific best practice.
Best practices. Ui
1. Use hardware accelaration. Allows you to improve the smoothness of ui, animation due to pre-rendering and storing these drawings in memory (instead of dynamic constant drawing and redrawing)
2. For custom View or animations (transparency, etc.), it is recommended to use hardware accelaration. Example:
')
View.setLayerType (View.LAYER_TYPE_HARDWARE, null);
ObjectAnimator animator = ObjectAnimator.ofFloat (view, "rotationY", 180);
animator.addListener (new AnimatorListenerAdapter () {
@Override
public void onAnimationEnd (Animator animation) {
view.setLayerType (View.LAYER_TYPE_NONE, null);
}
});
animator.start ();
3. In the custom view, if the method overrides the hasOverlappingRendering method and returns false, then the view cannot be set to the alpha other, but the performance will increase by a factor of two.
Link:
www.youtube.com/watch?v=wIy8g8yNhNk&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=9Follow this link -
developer.android.com/guide/topics/graphics/hardware-accel.html - more UI optimization tips
4. In onDraw it is not recommended to create objects (such as Paint, etc.). These objects can be made static - it saves memory + pre-creation + no permanent call to gc.
Link:
www.youtube.com/watch?v=HAK5acHQ53E&index=10&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE5. Tools: Strict mode. This tool is configured in the developer settings on the device. It allows you to catch all potential bottlenecks in the UI (something slows down, somewhere the UI is blocked - ThreadPolicy). You can also catch memoty leaks (VMPolicy). You can configure the mode for the application through the corresponding class -
developer.android.com/reference/android/os/StrictMode.html6. When creating a custom view.
View.invalidate () - causes the element to be redrawn. This evil. If something has changed, then you need to redraw not the whole view, but the specific Rect (if it is visible to the user).
Do not draw too much. What is not visible, do not draw. Trite, but often neglected. Canvas.clipRect () to help. Also for quick determination that a layer is not visible - Canvas.quickReject (...)
Do not overload too much for another CPU and GPU.
Link: https: //www.youtube.com/watch? V = zK2i7ivzK7M & list = PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE & index = 12
7. Bitmap. It is advisable to load pre-compressed images using BitmapFactory.Options
developer.android.com/training/displaying-bitmaps/load-bitmap.html8. Bitmap. When creating a Bitmap from a picture (jpeg, png), the default format is ARGB_8888 (32 bits per pixel). If the memory issue is critical, other formats can be used (RGB_565 - 16 bits and others). Setting this parameter occurs through BitmapOptions. Conversion, of course, affects performance.
Link:
www.youtube.com/watch?v=1WqcEHXRWpM&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=149.
www.youtube.com/watch?v=2TUvmlGoDrw&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=15 - different image formats
New Google format (the quality is the same, the size is smaller) -
developers.google.com/speed/webp/?csw=110. Scaling to BitmapOptions to save memory when downloading high-resolution images
www.youtube.com/watch?v=HY9aaXHx8yA&index=16&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCEdeveloper.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap11. Overdraw. It makes the GPU idle (drawing twisted elements). To determine overdraw, you need to enable the Show GPU Overdraw flag in DeveloperOptions. Also helps tulza ViewHierarchy.
Link:
www.youtube.com/watch?v=T52v50r-JfE&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=2212. TraceView is also well suited for determining the correctness of a drawing. All drawings should be placed in 16 ms (60 frames per second).
Link:
www.youtube.com/watch?v=HXQhu6qfTVU&index=21&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE13. VSYNC. What it is.
Refresh rate (60 hz) - this is the refresh rate of the screen, a constant. Frame rate - frame refresh rate per second. The unit of measure for the GPU.
Both speeds must be synchronized for correct display. To solve this problem, double buffering is used. With the GPU, the pictures go to the back buffer, and then to the frame buffer, and from there to the screen. Between the back buffer and the frame buffer is VSYNC, which ensures that the data from the back buffer goes into the frame buffer synchronously with the screen refresh rate. This eliminates possible collisions of pictures on the screen.
The speed of the GPU (frame rate) must be higher than the refresh rate of the screen in order for ui to be smooth.
www.youtube.com/watch?v=1iaHxmfZGGc&index=23&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE14. Tool - Profile GPU Rendering
You can connect to DeveloperOptions. We are interested in the graph for the application. There is also a navigation and notification bar. The graph itself is a collection of times required to draw a frame. The schedule should not exceed the green vertical line - 16ms (screen refresh rate).
The composition of the graphics is well described in the video below. Everything is pretty clear:
www.youtube.com/watch?v=VzYkVL1n4M8&index=24&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE15. Work of CPU and GPU in Russian is well written in
www.youtube.com/watch?v=zVK6TKSx1lU&feature=iv&src_vid=VzYkVL1n4M8&annotation_id=annotation_3064896115Critical for games)
16. Why do we have to stick to 60 fps -
www.youtube.com/watch?v=CaMTIgxCSqU&index=25&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE17. Android UI and the GPU
GPU is needed for rasterization of polygons, textures. The CPU gives the GPU the necessary graphic elements and, using OpenGL, these elements are converted into polygons and textures and stored in the GPU (need to be clarified?). Plus other GPU optimizations: rendering not the entire screen, but changing parts, combining elements into polygons, etc .:
www.youtube.com/watch?v=WH9AFhgwmDw&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=2618. Show GPU view updates in DevOps - shows which elements are invalidate ()
Best practices. Collections
1. By default, it is recommended to use Iterator to iterate through all elements of the collection.
But in critical situations, the question arises of choosing the fastest type of search. The best indicators belong to ArrayList with a search on an index.
Links:
developer .
Best practices. Ways to sync. Energy saving
1. JobScheduler +
github.com/evant/JobSchedulerCompat (for versions <21). A tool that forms a queue for background operations (especially working with the network), and organizing their execution in equal chunks for efficient battery energy saving (the radio stays active and waiting for a small amount of time). For the tasks, we set the conditions when, with what frequency, at which connection, etc. they should run.
2. SyncAdapter. The old solution. A lot of code + own ContentProvider.
3. GCM. As a replacement for JobScheduler -
developers.google.com/cloud-messaging/network-manager . It allows you to synchronize not by periodically polling the service, but by receiving a message from the GSM about a change in something in the service, in fact, the client also needs to update. Plus, only GSM messages will reach the device in deep sleep mode (M version)
4. Firebase Offline. Offline sync.
5. AlarmManager. Two types of start / repeat time are set: inexact (time is correlated with the start time of other applications to save battery, recommended), exact (exact time, not recommended)
Best practices. Battery
1. To monitor the battery in the application, you can use:
- Application Settings
- battery-historian (https://github.com/google/battery-historian)
To save battery in cases where it is necessary to make any periodic requests, it is recommended - JobScheduler
Where the battery goes -
research.microsoft.com/en-us/people/mzh/eurosys-2012.pdf Good research:
www.youtube.com/watch?v=9i1_PnPpd3g&index=29&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCEBest practices. Memory
1. Object pools (
www.youtube.com/watch?v=bSOREVMEFnM&index=5&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE ). For independent control over the memory allocated for heavy objects (such as Bitmap, etc.). It is necessary in order to control when the GC should trigger - the speed and smoothness of the application.
Possible solutions: LRUCache, Pools (https://developer.android.com/reference/android/support/v4/util/Pools.html), FlatBuffers (https://google.imtqy.com/flatbuffers/)
2. I think the usual LRUCache should be enough
Best practices. GC
1. Comparison of Dalvik and ART -
source.android.com/devices/tech/dalvikAs an example, ART has improved GC work due, among other things, to the parallelization of GC work:
www.youtube.com/watch?v=pzfzz50W5Uo&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=34Google play services 7.5
1. Cloud messaging. Sending messages with specific topics, NetworkManager, repeating in principle JobScheduler
developers.google.com/cloud-messaging2. App invites. Invitation from a friend
developers.google.com/app-invites3. Smart lock for passwords. If the user, under his account on the mobile, and, for example, he went to the application from the browser and saved the password, then the application authenticates automatically:
developers.google.com/identity/smartlock-passwords/androidAbout these innovations and the rest here -
developers.google.com/app-invitesVoice interaction
Management of the application through voice commands. In the course only with the M version.
io2015codelabs.appspot.com/codelabs/voice-interaction#1developers.google.com/voice-actionsNew and interesting
1. Fingerprint API (fingertip) and Confirm credential (cryptographically)
www.youtube.com/watch?v=VOn7VrTRlA4&index=11&list=PLOU2XLYxmsIJDPXCTt5TLDu67271PruEk2. In the M version in Settings, you can see how much the application consumes traffic, etc.
3. Data binding in the future (setting the list and date directly in xml)
4. RecyclerView ItemTouchHelper - Swipe-to-dismiss, Drag & Drop
5. When setAlpha hardware will connect automatically
Just interesting
1. External storage - no hard code! Never use when specifying the path to the file lines like - "/ sdcard /" (that is, do not specify the path to the file manually). To do this, there are special methods such as “Environment.getExternalStorageDirectory (). GetAbsolutePath ()”, which will always return you the correct path to the file, which is not guaranteed when you manually set it.
Android studio
1. Support SVG format
2. New Jack Compiler
3. PNG Cruncher?
4. Coming: aapt
5. Studio 1.3 + Gradle 2.4 = a big increase in build speed
6. Data binding is experimentally available now.
7. Coming soon: Cloud test Lab, Google play testing
8. NDK support
9. New annotations (WorkThread, etc.)
10. Android Typed Integer - now you can see that any flag means
11. Capture view: Heap snapshot, Allocation tracking, etc. - allows you to view memory leaks and more
12. Simple connection of Google services (analytics, cloud, etc.)
13. Theme editor
www.youtube.com/watch?v=f7ihSQ44WO0Interesting libraries
1. Libraries for developers. Many interesting UI solutions
Conclusion
I hope you enjoy the article and it will be interesting! This is my first article, so if you get positive emotions and knowledge, I am waiting for invite. If you have something to add, or fix, wait for comments.