当前位置:网站首页>Android advanced interview question record in 2022

Android advanced interview question record in 2022

2022-07-05 02:05:00 Manon paradise

Apply stability optimization

1、 What stability improvements have you made ?

The crowd of the project is gradually maturing , A large increase in the base ,DAU Keep rising , We have encountered many problems in user stability , Or often use our App Users with abnormal Caton function , Therefore, we will focus on the key optimization projects that we continue to pay attention to , We mainly optimized three :

  • Crash special optimization (=>2)

  • Performance stability optimization (=>2)

  • Business stability optimization (=>3)

With these three aspects of optimization, we have achieved a truly high availability platform for mobile terminals . meanwhile , There are also many measures to make the mobile terminal achieve high quality .

、2、 How does performance stability work ?

  • Overall performance optimization : Speed up 、 performance optimization 、 Startup optimization

  • Problems found offline 、 Optimize

  • Online monitoring

  • Crash special optimization

We start for memory 、 Layout 、 Slimming 、 Traffic 、 customized 、 Multi dimensional and multi-faceted start .

Our optimization is mainly divided into two levels , For offline , That is, our online and offline , That is, we find problems online , Direct solution , The goal is to solve the problem before it goes online . Let's aim at , In order to get the performance to our monitoring in advance ?

, for example ,Crash The most serious simultaneous problems , We made some specific information , Then optimized Crash Display of , And it also involves Crash Details of , Then the optimized combination , Alarm and other functions , It is convenient for us to locate the problem quickly .

3、 How to ensure stability ?

  • Data collection + Call the police

  • It is necessary to monitor the main process and core path of the project ,

  • We also need to know how many abnormal rates occur , such , We know the conversion rate of all businesses , And the conversion rate of the corresponding interface

  • , If the conversion rate is combined with the market , Call the police

  • Abnormal monitoring + Single point tracing

  • Out strategy

The high service of mobile terminal provides users with abnormal functions , But it is mainly to solve the problem that some complete functions lead to its failure in some cases , No performance , Well , It's simply not available , The situation requires our understanding of the main process of the project 、 Buried point monitoring of core path , To calculate the real conversion rate of each step , At the same time , You also need to know how many exceptions occur in each step . In this way, we know the conversion rate of all business processes . And the corresponding displayed conversion rate , There are a lot of data , We already know the online value of the conversion rate disk or the success rate of some monitoring , If that's possible, it's abnormal , There are corresponding possible alarm functions , We don't need this to wait for user feedback , It is the basis of business stability guarantee .

Well , Code , at that time , There are some special cases in the development process or some special cases in some function blocks , Let the program not happen unexpectedly , This anomaly is actually abnormal , Although the program did not fail , But such users of the program have these unknown problems , So we also need such exceptions , So we need to report an exception in a problem , We're on it , There are still some problems , When the user clicks ? Never go , This situation belongs to the nature of the problems we need to understand and other problems , So , Be sure to find its details .

Last , If something goes wrong , We also made a quick stop loss on the table .(=>4)

4、 If something goes wrong , How to stop loss quickly ?

  • Function switch

  • Jump system center

  • Dynamic repair : Hot repair 、 Resource pack update

  • Autonomous repair : safe mode

First , Need make App It has some advanced functions , We decided to display new features to be enabled for any , To add a function switch , Functions distributed through the configuration center , To see if you want new functions . There are abnormal , Access to emergency functions , Then you can turn off the state controlled by the application .

then , You need to jump all routes , All interfaces need to be distributed through us or through applications , If we need to jump to such a new function with errors , Then we will no longer create route jumps , Jump to the unified exception handling interface . Add the ability of hot function repair to our project , Of course , If some are made of RN or WeeX It's better to realize , Then, if the dynamic update is realized by updating the resource package . Then you can consider adding a self repairing state , If the application starts on a large scale , Then consider emptying all cached data , Reset the application to the installed state , To the most advanced position , It can prevent users from hot repairing , Be sure to wait App Only after success can we enter .

5、 Native crash

  • The collapse process : When the machine crashes, the operation will send a signal to the process , Crash information is written to data / Under the tombstone , And in logcat Output crash log

  • location : So if the location library can debug information , Only the relative position has no specific line number , Use NDK Provided addr2line or ndk-stack Come on

  • addr2line: According to the debugging information so And relative position to locate the actual code

  • ndk-stack: Tombstone files can be analyzed , The actual code call stack

For a more comprehensive and in-depth understanding, please see the exploration Android Stability optimization

2、App Start speed optimization

1、 How to start optimization ?

  • Analyze the phenomenon 、 Identify the problem

  • Before arriving , Guide it to

  • Keep the best effect for a long time

After a certain version , We will find that the startup speed is very fast , At the same time, users give us more and more feedback , therefore , We began to consider optimizing the startup speed of the application . then , Let's optimize the starting speed . The code is confirmed by a series of code tools , As a result, we have implemented our discovery process , Then we have performed a series of operations by executing multiple operations in multiple main threads in the task .

Then we found it through detailed code , Found the task of applying the main thread , He came up with a solution for us , That is, the first similar discovery . A problem , You can also create an initialized optimization , It's in the code we execute , Because some priorities are high , So it can not execute the application , And can be executed immediately , And can be executed immediately . We initialized these codes , We also made an excellent late combination for the processing program , It can be in the recent event of the main thread , Plan to start it in a more effective way . After that , Our start-up is very fast .

Last , Let me simply say how we can start the optimization effect for a long time . First , We made our starter , our CI, And many aspects of monitoring have been added online .( guide => 4 topic )

2、 By chance , There are no problems encountered by chance ?

  • Evolution process

  • Introduce the starter in detail

We initially adopted a common asynchronous solution , namely new Thread + The priority of background thread is the way of our background thread in Application Of onCreate Initiate initialization in , Using thread pool 、IntentService Of , later , In the evolution of our application , No matter what , Finding code can be very difficult to handle in some cases , And there are multiple initialization tasks , An initialization task needs to be completed in a specific time period , These are all things that we cannot use to solve problems ,IntentService The implementation of the .

This is the startup scheme currently used , In the concept of starter , We combine each initialization code into a task , then , Make an example of , According to the relationship between them, there is a directed graph , then , Use a task to delegate our main thread to execute , It and CPU The core of is powerful and relevant , It ensures that both the processing and the thread can execute our , That is, almost everyone can finish it at the same time .

3、 What are the easy points to pay attention to when starting optimization ?

  • cpu Time and wall time

  • Pay attention to the optimization of initialization in advance

  • Introduce black Technology

First , stay CPU Profiler and Systrace There are two important indicators in , namely cpu time and wall time, We have to be clear about cpu time and wall time The difference between ,wall time It refers to the time of code execution , and cpu time It refers to code occupation CPU Time for , Lock interruption will cause a large time gap . We need to cpu Time as a direction of our optimization .

We are only looking for an improvement in starting speed , It also needs a recent optimization , For later initialization , But my habit is to display on the interface , Usually, if you need to display and slide with the user at this time, you can load this outstanding person with the user , It seems to have a very obvious effect , So our sudden occurrence of a sudden event , And run users at this point in time , It greatly improves the efficiency of users , And promote accidents caused by downtime . Page jam phenomenon .

Last , Startup optimization , Some black Technology , First , That is, we adopt the method of class preloading , We are Multiex.install Then there is a thread , And then use Class.forName Method to trigger class loading , Then when our class is really used , There is no need to run the class loading process . nucleus , But these give us 4 The nuclear situation , then , Some applications just do some black Technology , It's just its cpu The kernel and its frequency were violently increased at startup .

4、 Is there a good solution to the slow startup caused by version ?

  • starter

  • combination CI

  • Perfect monitoring

In fact, we have met before , This problem is very solvable , But we thought and tried again and again later , Finally found a comparison .

First , Use the manager to manage one task of each task , And our execution in each task is automatically deployed , Our deployment of these tasks automatically started by us will ensure the goal of starting each process of its deployment when we arrange , This is different from our ordinary accidents , It can well slow down the startup changes of our applications .

after , We also combined CI, So we now limit some modifications , For example, applications , If someone gives it , We will not merge some of its code into the trunk Department , Or there are some internal tools , For example, if internal tools are sent voluntarily ,,, I can consider whether the final result of his code , If she can apply , If I want to , I can consider being late too long , Load lazily , Use it later... Etc .

then , The problems we have monitored are also exposed before the environment goes online . For example, applications and AttachBaseContext Create a monitoring method at a time interval , And the interval between these two life cycles , We all had one , Let's talk about our slow startup , We can find out which one slows down , We will compare it with the previous one , After the comparison , We can find this new code .

5、 Open questions : If you increase the starting speed , Design a delayed loading framework or sdk The method and the problem of attention

For a more comprehensive and in-depth understanding, please check   Accelerate exploration Android Accelerate optimization ( On )  Began to explore Android Startup optimization ( Next )

3、 Apply memory optimization

1、 How do you do the process of your memory optimization project ?

1、 Analyze the reality 、 Identify the problem

our APP There may be a big problem in memory , The first reason is online OOM The rate is relatively high . The second point is , We will always be in our Android Studio Of Profiler In the tool, one of my organizations finally found a major event . Then in a project organized by us , After we found this project , We found problems in this project , We have conducted in-depth research on us , And conducted in-depth research , Use a very big problem , say : payment 、 payment 、 payment , And our bitmap is rough .

2、 Optimization objectives

The solution of memory story > Use of memory profiler ( Display teeth appear > Analyze the logs in the presentation ) Or overflow solution .

3、 Capacity improvement

In order not to increase the workload of business students , We use some classes or ARTHook Such a big plan , There is no intrusiveness of any of these tools , meanwhile , We will teach you Technology , Then let us work together to improve efficiency .

Our memory analyzer for the entire memory optimization tool 、MAT Usage situation , So for different situations , We have written outstanding solutions to the problem , Share with you . Such documents , Our team members' awareness of memory optimization has become stronger .

2、 What is your biggest feeling about optimization ?

1、 Sharpening a knife never misses a woodcutter

We didn't analyze where the learning memory problem existed in the code at the beginning , But go directly first Google Some official documents , For learning projects Memory Profiler Use 、 Use of learning tools , After we orientate tool learning , When we encounter memory problems in our project , We can easily solve the problem of land arrangement .

2、 Technical optimization must be combined with business code

Start writing APP Operation phase , then , We report a memory in the running phase , then , We found some memory modules, and later found and monitored that some relevant business code was not running in our memory , It shows that in these monitoring, we have completed a monitoring project , We found that using multiple images exists , It is found that the memory cache in multiple image libraries is definitely not a public item , So our memory usage is very high . Therefore, the whole project must be combined with our business code during technical optimization .

3、 Systematic and perfect solutions

Whether we are running memory , Then it's putting Android Put aside the work of the end , We put Android Send some data from the client to our server , such , Convenient for our backstage , such , For our convenience Bug Tracking or crash tracking personnel solve major problems .

3、 How to detect all unreasonable places ?

Develop large image detection , At first we inherited ImageView The method found a similar scheme , But because of its implementation , In the process of promoting it , A lot of people don't accept , quite a lot ImageView It has been done before , Now you let him replace , The cost of work is relatively high . So , And then we thought , Is there a way to avoid replacement , Finally, we will ARTHook So I found one Hook Of .

How to calculate memory ?( Code considerations )

The memory occupation is because a large number of objects enter the new area , There is cpu Resources GC,gc It will cause the application to jam completely .

Avoid any suggested suggestions :

  • Avoid creating objects in loops , The object should be created into the loop of the object .

  • Pay attention to the custom view () Methods will be captured , So this should not be a real creation object .

  • When it takes a lot of use Bitmap When , Try to reuse them in or containers .

  • For objects that can be reused , Similarly, they can be cached using pools .

For a more comprehensive and in-depth understanding, please check Android Memory optimization for performance optimization 、 Explore Android Memory optimization 4、App Download optimization

1、 What optimization tools have you made in the process ?

I am in the process of arranging different , Every different situation , I have different tools and their usage , The following is my analysis from different perspectives of online and online ..

Words , I want to count online FPS, What I use is Choreographer This kind of , It has the following characteristics :

  • 1、 Can get the overall frame rate .

  • 2、 Can be used online .

  • 3、 Its frame rate is almost real-time , To meet our needs .

yes , Offline , If you want to perform loading, it will lead to time consumption , So check every different layout , This is the development I need , At the same time, there is no need to use the way , There is no need to use if this access point , You can easily detect a deployed method to obtain .

Besides ,LayoutInspector and Systrace This tool ,Systrace You can see a good layout in every frame of , And you can easily use each of its layouts in this framework . Interface hierarchy , Help us optimize the hierarchy .

2、 Why does it cause Caton , How do you optimize

After analyzing the loading process of the layout , We have four points that may lead to the discovery of layout jam :

  • 1、 First , The process system generates our Xml File by IO The mapping method is loaded into our memory , and IO May trigger our agreement .

  • 2、 Suddenly, it is a reaction process , And the reaction process may lead to card .

  • 3、 The terrain is deep , Then compare layers during the process of crossing at the same time .

  • 4、elativeLayout The layout is redrawn too many times .

This , Our optimization methods are as follows :

  • 1、 Load for layout Xml File optimization , Asynchronous Inflater The way , namely LayoutInflater. Its principle is to load through its in subsequent sub threads , After loading, we synchronously send its layout to the main thread to use our main thread , The loading time is all timed out in similar threads . And this is another way to alleviate .

  • 2、 The back is , We found a way to fundamentally solve the above pain points , That is to use X2C frame . One of its principles is in the development process or our process XML When writing layouts , But when compiling, it will use APT The way to XML The arrangement is converted to Java Arrange in a way , Write the layout like this , It has advantages :1、 It eliminates the use of XML The arrangement is used in the following ways Java Sequence code for .2、 Its new object control , So it also goes to the creation method without reflection . This directly and fundamentally brings performance problems in the process of layout loading .

  • it 、3, We can reduce the use of deep layout levels , If the layout level , You can skip levels more and more . many .

  • 4、 Last , We can use AspecJ frame ( namely AOP)LayoutInflaterCompat.setFactory2 To establish the loading speed and control monitoring system of offline loading level respectively .

  • 3、 What is the effect of layout optimization ?

  • 1、 First , We have established a standardized monitoring means , The system here also refers to an integrated scheme of online and offline , For offline , We AOP perhaps ARTHook, It is easy to get various usage patterns through the sending box and through such a control to C Call to get . on-line , We can go through Callback The way to know when we call .

  • 2、, For layout , We established FPS、 Layout time 、 Layout level monitoring and other indicators .

  • 3、 Last , Before a version goes online , Each of our departments will review our core path once , Make sure our FPS、 Layout time 、 The layout level reaches a reasonable state .

    4、 How do you do Karton optimization ?

From now to the big time , And then to the end of the period, it is carried out according to different processing methods . The tasks completed on time for each meal are as follows: :

  • 1、 System tool positioning 、 solve

  • 2、 Automation Caton scheme and optimization

  • 3、 Construction of online monitoring and offline monitoring tools

My Caton optimization is in the initial stage , We have some later modules , I do system tracking , I use Systrace, Over a period of time CPU Under the circumstances , Combining with the code , This module has been transformed , The part is delayed and delayed , The problem was solved at the beginning of the project . Well , With the expansion of the project , There are more and more codes for offline Caton , meanwhile , In the online , The news also has Caton's feedback , But online feedback stuck , We reappear offline , So we began to look for automated Caton monitoring solutions , The idea comes from Android Its processing mechanism , Execution thread code unit Looper.Looper. Method automatically records any method we use , There is a loop object , It will be called at the main execution schedule of the message , The timing of execution of this thread is monitored at the same time , At this stage , We have also completed online ANR The report of , The way we take is to monitor ANR Information about , Combined with ANR-WatchDog, As a supplementary scheme of high no file permission . Doing this Caton test , We have also done the construction of online monitoring and subsequent online detection , Finally, a complete set of improvement , Multidimensional tools ?

5、 You often get Caton information automatically ?

Our idea comes from the message processing mechanism , The main thread performs any code management Love process , The processing method of this function is mlogging it , It is called in every thread , When the main thread is called, a thread will execute a scheduled trigger event at a certain point in time , We can execute a delayed message at that time , We can trigger an event at this delayed time , This delayed message , We can trigger an event threshold at this delayed time , Tasks completed by the main thread within this value , Then cancel the task of this sub thread , If the task of the main thread is completed within its value , You can execute , It will get the one that our current protagonist performs , You can know what happened there .

already , Found the right place for this scheme , Because we found a real place , We will make some plans for this plan , We have adopted an optimization scheme collected recently , We will also trigger the relevant information of the Lord for a period of time , If carton , We will compress these information and give it to APM backstage , These messages are repeated messages , Then a huge place happened repeatedly , This is a huge place where the Caton information occurs .

6、 How does Carlton's whole set of solutions work ?

First , The way for online media to jam , We used offline tools to expose tool problems in advance , For online , We combine , Comprehensive sex 、 Automation and abnormal results .

There are many mistakes , See your Caton's mission . therefore , We predicted the next normal result online in some way Hook, We can analyze the data obtained in , And know the time and frequency of these data, and then , It is carried out in many cases below . A time interval of a cycle , The modification of specific problems , We use compile time annotation to solve the problem of the parent class of all handlers in this project , There are two processing methods , We can mainly process messages, their execution time and diols .

Carton , We have to calculate App The Caton rate 、ANR In addition to conventional indicators such as rate , We also calculated the second opening rate of the page 、 Cycle execution time, etc . and , At the moment of katon , We also saved more of the current scene , This is for us to reproduce or reproduce the information left by Caton .

7、 The main scene of Caton ?

There are many pictures in Caton , There are four types by scene :UI、 The scenario application is startup 、 Switch event response , Among them, you can add :

1、 The user interface

  • painting

  • Refresh

2、 start-up

  • Installing the

  • Cold start

  • Hot start

3、 Jump

  • Jump between pages

  • Front and rear station switching

4、 Respond to

  • A single one

  • system event

  • slide

The root causes can be divided into the following

1、 Interface drawing

  • The level of painting is deep

  • Complex pages

  • Unreasonable refresh

2、 Data processing

  • Data processing in UI Threads

  • CPU Take up high , As a result, the main thread cannot get the time slice

  • Memory increase trigger GC, Therefore, the card is introduced

Need a deeper understanding of view Android Rendering optimization of performance optimization 、  Explore Android Layout optimization ( On )  Explore in depth Android Layout ( in )、 Explore Android Layout optimization ( Next )、 Explore Android Carton optimization ( On ) )、 Explore Android Carton optimization ( Next )

5、App Slimming

Slimming optimization is a help to optimize performance, although it doesn't seem to be very important , But our problems will be more stable and operational . Here are some common questions .

1、 How to reduce Apk Bag size ?

We should pay attention to some operable dry goods when answering , At the same time, combine with your project cycle . We can answer from the following three points :

  • 1)、 Code :Proguard、 Unified third-party library 、 Useless code deletion .

  • 2)、 Delete with resources 、 Resource reconfiguration : nothing .

  • 3)、 therefore : Only keep Armeabi、 Better plan .

At the beginning of the project , We have been in the field , Added a lot of code 、 resources , At the same time , There is no corresponding specification , So ,UI Students give us a lot UI In the picture , There is no continuous compression of functional images , For a long time , Our bags will get bigger and bigger . Optimize , Adopt yes Android Studio Self contained Analyze APK Let's do our package volume analysis , The main thing is to make code 、 resources 、 So wait for the key optimization in three aspects .

First , For code simplification , We use it for the first time Proguard Realized the first function , Tool code conversion to the first function , But the tool code is converted to a format that is not easy to understand . At the same time, convert a very long class into identity ., There is also a job of security , Let the project use some waiting libraries 、 database 、 database 、 Safety project , We will have different functions , But a different library . The third point , We cut down the useless code in the project , We used AOP In this way, it counts which activities and clips are not used by users in real situations , For those who are not Activity Or is it Fragment Class , We can count the constructor of the class , You can make statistics like this so that some classes don't really need to be called . however , For the simplification effect of code , It's not obvious .

In limine , We took out some resources . We give our resources , We are sure to encounter such pictures , Then we make resource image compression , UI Resource pictures , It's a compressed picture , At the same time, for our bottom strategy , When we do the packing , Uncompressed pictures , Then we need to continue until now , At this time, the effect is very obvious . Resources we also have a good name for resources , Resource is to replace the resource name with more compression utility codes , The effect of streamlining .

first , We only keep the arm , It can finally realize another CPU framework , Click the very directory . Removed the high performance of it , therefore , We also deal with another , The arm project in order to use its video , So it has very high performance , So we adopted another way , Put all the directories of this module under this , Make decisions in code , If it is another CPU framework , Then we can load CPU The file of the architecture . In this way, the final performance can be achieved . The effect of is generally the best ‍.

2b85d7807e35c16c3e5ef96c7140ddb3.png

原网站

版权声明
本文为[Manon paradise]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140956569924.html