project background:
With the progress of the times, the placement of mobile ads has become more and more diversified. In order to get closer to the market, many companies have developed their own SDK to collect some user information and behavior for analysis, and use custom ads based on the analysis results ( Custom View) continues to be displayed to users in order to increase the display rate and click-through rate.
Regarding the current choices of advertisers, domestic advertising monetization is generally low, and the first choice should be to access Google advertising. With the development of the business, after a period of time, the company began to transform into an advertising receiver, and rely on its own SDK for advertising and optimization.
Take the way of getting ads by targeting as an example:
· First use the user's location and other permissions to get the latitude and longitude
· Upload the latitude and longitude to a domestic positioning SDK to obtain specific information.
· Finally, the advertisement is obtained according to the positioning information and pre-loaded to prepare for the advertisement display.
encountered by
A series of tests will be conducted on the project before it is ready to be put on the shelf after the project function is completed, but the ANR problem is difficult to reproduce during the test process, and there is almost no ANR problem record during the test using multiple models. However, in the actual use process of the user, due to the serious fragmentation of Android and some operating habits of the user, the problem of ANR will occur. In the face of some large manufacturers, when ANR appears, a pop-up window will guide users to close the software, which will lead to a bad user experience and loss of users.
steps to solve the problem:
During the abnormal collection of the project for more than a month, there were several ANR problems.
· Analyze the ANR log in the abnormal collection and lock the problem on SharedPreferences. The troubleshooting process is more troublesome. After the problem is locked, the problem is analyzed.
· View Android documentation: In the project, the team uses SharedPreferences to read and write configuration files. They all adopt the official recommended practice. Call apply to submit. When calling this method, write it to the memory first, and then add the task to the queue. The operation of placing an order in an asynchronous thread is theoretically no problem, and it is also officially recommended by Google.
· Read the source code:
official Google note was found during this process:
If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.
Translation: If another editor on SharedPreferences executes a regular commit(), and apply() is still incomplete, then commit() will block until all asynchronous commits and the commit itself are completed.
· Locking problem: the main thread calls QueuedWork.waitToFinish()
, there is no task to be executed, directly executes the finisher, performs blocking and waits, until the file is successfully written to resume execution, at this time, if the waiting time is too long, the performance is poor in some markets. ANR will easily appear on the end model. (Below 8.0)
Problem solving
Optimization at the time:
- Reduce the size of the file corresponding to the sp, and read and write the corresponding sp file according to the classification.
- SP reads and writes lightweight, small configuration information, and transfers JSON-like data to other methods for storage.
- When the Put series methods need to be called multiple times, and when the logic confirms that it does not need to be read immediately, just call commit or apply at the last time.
Recently, my friend posted a blog about Byte (the following text and pictures are from the Toutiao team of Byte Today).
· Idea: If you can make sPendingWorkFinishers.poll() return to null, the waiting behavior here will skip over. sPendingWorkFinishers is a ConcurrentLinkedQueue collection, which can directly proxy this collection dynamically, override the poll method, and let it return null forever, this Sometimes the UI will never wait for the child thread to finish writing to the file, and it turns out that this method is simple and effective.
· In response to this kind of write-waiting ANR problem, there is also a global replacement write method, which replaces all API implementations through instrumentation, and uses other storage methods. This method has a higher repair cost and risk, but The storage method can be randomly replaced in the later period, and the use is more flexible.
platform related SDK first experience:
Since ANR is difficult to reproduce, I wrote a method to repeatedly operate SharedPreferences to achieve the recurrence of similar situations.
If there is a problem, locate it through the U-Meng U-APM platform:
After finding the problem, just follow the instructions in the article.
Summarize
In the scenario, because Android is too fragmented and users of lower versions cannot be abandoned directly, it is indispensable to use a method similar to the U-Meng U-APM platform to solve the problem faster. However, ANR problems may still occur for the low version of some small mobile phones. For scenarios similar to collecting user behavior, various methods can be adopted to collect, for example, for low version systems, reducing the integrity of the collected data, etc.
Author: Ji Meng does not eat fish
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。