当前位置:网站首页>Master this promotion path and share interview materials

Master this promotion path and share interview materials

2022-07-07 16:45:00 InfoQ

It is said that programmer is a youth rice , And I've been in the industry for more than seven years , I am about to enter the age of thirty . All say 30 Age is a hurdle that every programmer must go through , And I'm going to face it , I often wonder if I can cross this barrier .

![](https://upload-images.j 
《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》 Free open source Hui Xin search official account 【 Advanced programming 】
 ianshu.io/upload_images/22861276-b18c6975e70c7541.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

So I asked for some advice 30 And a well-developed predecessor . Let me be right about " The middle age crisis of programmers " With a further view .

In fact, we should see the essence of the Internet , It is an industry with a short life cycle , Fast iterations 、 Agile development 、 The coming and going of the tuyere , This industry is full of opportunities and a lot of impetuosity , How to learn , How to look at the industry , How to deal with your career planning , Elegant and smooth through the bottleneck period , Namely " Programmer middle age crisis " Root cause of .

** So how do programmers survive the midlife crisis ?** Now let's share some of the face-to-face methods I summarized , Hope to be helpful to the programmer friends at this stage .

Android Advanced extension point

1、 How to do unit testing , How to ensure App Stable  ?
Reference answer :

To test Android Applications , The following types of automatic unit tests are usually created

  • Local testing
    : Local machine only JVM Up operation , To minimize execution time , This unit test does not depend on Android frame , Or even if there is a dependency , It is also convenient to use simulation framework to simulate dependency , To achieve isolation Android Purpose of dependence , The simulation frame is as follows Google The recommended Mockito;
  • Testing
    : Unit tests running on a real machine or Simulator , Because of the need to run to the equipment , It's slow , These tests have access to the instrument (Android System ) Information , For example, the context of the application under test , In a general way , It's not very convenient to use this method when simulating through the simulation framework ;

Be careful : Unit tests are not suitable for testing complex UI Interactive events

App The stability mainly depends on the overall system architecture design , It is also important not to ignore the detailed specification of code programming , As the saying goes “ A thousand Li dike , Collapse in ant nest ”, If you don't think about it , Seemingly unimportant code snippets may cause the overall software system to crash , So before going online, in addition to its own localization test, it also needs to carry out Monkey Pressure test

A few interviewers may extend , Such as Gradle automated testing 、 Model adaptation test, etc
2.Android How to view the recycling status of an object in  ?
Reference answer :

  • First of all, understand Java Four reference types of scenarios and uses ( Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference )
  • Take a scenario :SoftReference Object is used to hold soft references , But it's also a Java object , So when the soft reference object is recycled , Although the SoftReference Object's get Method returns null, but SoftReference The object itself is not null, And now this SoftReference The object no longer has the value of being , An appropriate clearance mechanism is needed , Avoid large quantities SoftReference Memory leaks from objects
  • therefore ,Java Provide ReferenceQueue To handle the recycling of reference objects . When SoftReference The referenced object is GC after ,JVM Will be will be softReference Object added to ReferenceQueue In this line . When we call ReferenceQueue Of poll() Method , If this queue is not empty , Then you will return and remove the one you added earlier Reference object .
    null
3.Apk How to compress the size of  ?
Reference answer :

3.1 A complete APK Contains the following directories ( take APK Drag files to Android Studio):

  • META-INF/: contain CERT.SF and CERT.RSA Signed documents and MANIFEST.MF  Inventory file .
  • assets/: Include applications that can use AssetManager Object retrieval application resources .
  • res/: Contains uncompiled resources  resources.arsc.
  • lib/: Contains compiled code specific to the processor software layer . This directory contains subdirectories of each platform , image armeabi,armeabi-v7a, arm64-v8a,x86,x86_64, and mips
  • resources.arsc: Contains compiled resources . The file contains res/values/  Folder in all configurations XML Content . The packing tool extracts this XML Content , Compile it in binary format , And file the content . This includes language strings and styles , And directly contained in *resources.arsc8 Content path in file  , For example, layout files and images .
  • classes.dex: Include with Dalvik / ART The virtual machine can understand DEX Class compiled in file format .
  • AndroidManifest.xml: Including the core Android Inventory file . This file lists the name of the application , edition , Access permissions and referenced library files . This file uses Android Binary system XML Format .

null
  • lib、class.dex and res Occupied more than 90% Space , So these three pieces are optimization Apk The point of size ( The reality is not unique )

3.2. Reduce res, Compress graphic files

Image file compression is for jpg and png Format picture . We usually place multiple sets of pictures with different resolutions to fit different screens , It can be deleted here . In practical use , It's enough to keep only one or two sets ( Keep a set of words and suggest to keep xxhdpi, Two sets of words add hdpi), Then compress the remaining pictures (jpg Use the best picture compression ,png Try to adopt pngquant Compress )

3.3. Reduce dex file size

  • Add resource obfuscation
  • null
  • shrinkResources by true Remove unreferenced resources , Working with code compression .
  • minifyEnabled by true Said by ProGuard Enable code compression , coordination proguardFiles Configuration to obfuscate code and remove unused code .
  • Code obfuscation is compressing apk At the same time , It also improves security .

3.4. Reduce lib file size

  • Because there are a lot of third-party libraries ,lib Folders usually take up a lot of space , Especially with so In the case of the library . quite a lot so The library will introduce armeabi、armeabi-v7a and x86 There are several types , You can only keep armeabi or armeabi-v7a One of them is enough , In fact, wechat and other mainstream app It's all done .
  • Just in build.gradle Just configure it directly ,NDK Configuration is the same

null
4. How to use Gradle Configure multichannel package ?
Reference answer :

First of all, understand the reasons for setting up multi-channel . Add a different identity to the installation package , Cooperate with automatic burying point , The application carries the channel information when requesting the network , Convenient for background operation statistics , For example, statistics of our app downloads in different app markets, etc

  • Take the statistics of the friendship League as an example
  • First, in the manifest.xml Set dynamic channel variables in the file :
    null
  • And then app In the catalog build.gradle Middle configuration productFlavors, That is, to configure the channel of packaging :
    null
  • At the bottom of the editor Teminal Output command line
  • perform ./gradlew assembleRelease , It's going to be all channels release package ;
  • perform ./gradlew assembleVIVO, Will play VIVO Channel release and debug Version of the bag ;
  • perform ./gradlew assembleVIVORelease Will generate VIVO Of release package .
5. Analysis of plug-in principle
Reference answer :

Plug in refers to the combination of  APK  Divided into host and plug-in parts . Take the modules or functions to be implemented as an independent extraction , stay  APP  Runtime , We can dynamically load or replace the plug-in part , Reduce the size of the host

  • host :  Is currently running APP.
  • plug-in unit :  Compared to plug-in technology , Is to load the running apk Class file .

And thermal repair is from repair bug From the angle of , It's important to fix the known... Without the need for a second install application bug. can

null
Class loading mechanism
Android Two kinds of loaders commonly used in ,DexClassLoader and PathClassLoader, They all inherit from BaseDexClassLoader, The difference is PathClassLoader Only internal storage directories can be loaded dex/jar/apk file .DexClassLoader Supports loading the specified directory ( Not limited to the interior ) Of dex/jar/apk file

Plug in communication : By giving plugins apk Generate corresponding DexClassLoader Then you can access the classes , Can be divided into single DexClassLoader And many DexClassLoader Two structures .

  • If you use more ClassLoader Mechanism , The main project references the classes in the plug-in through the plug-in ClassLoader Load the class and call its methods through reflection . The plug-in framework will generally manage the access to the classes in each plug-in through a unified entry , And make certain restrictions .
  • If you use single ClassLoader Mechanism , The main project can directly access the class in the plug-in through the class name . There is a drawback to this approach , If two different plug-in projects refer to different versions of a library , Then the program may go wrong

Resource loading

  • The principle is to reflect the plug-in apk Path join of AssetManager And create Resource Object load resource , There are two ways to do this : Combined :addAssetPath Add the path of all plug-ins and main projects ; because AssetManager Add the path of all plug-ins and main projects , So generated Resource You can access both plug-in and main project resources . But because the main project and each plug-in are compiled independently , Generated resources id There will be the same thing , Resource conflicts arise during access .
  • Stand alone : Each plug-in only adds itself apk route , The resources of each plug-in are isolated from each other , But if you want to share resources , You have to get the corresponding Resource object .
6. The principle of componentization
Reference answer :

The reason for introducing componentization : Projects are getting larger and larger as demand increases , The increase of scale leads to the complex interweaving of various business errors ,  Between each business module , The code has no constraints , It brings a blurring of code boundaries , Code conflicts happen from time to time ,  Changing a small problem can cause some new problems ,  Pull one hair and move the whole body , Add a new demand , Need to be familiar with the relevant code logic , Increase development time

  • Avoid making wheels repeatedly , Can save development and maintenance costs .
  • You can use components and modules to align your workforce with your business benchmarks , Improve development efficiency .
  • Different projects can share one component or module , To ensure the unity of the overall technical solution .
  • Prepare for the future plug-in sharing the same underlying model .

Component development process is to complete a function App Or the module is divided into multiple sub modules (Module), Each sub module can be compiled and run independently , It can also be combined into another new  App Or module , Each module is independent but can interact with each other , But the final release is to merge these components into one apk, In some special cases, you can even upgrade or downgrade

Take a simple model

null
null
App It is the Lord. application,ModuleA and ModuleB It's two business modules ( Relatively independent , They don't influence each other ),Library It's the basic module , Contains the dependency libraries required by all modules , And some tool classes : Such as network access 、 Time tools, etc

Be careful : The basic components provided to each business module , It needs to be divided into  aar  perhaps  library, Like logging in , The basic network layer is such a stable component , It's usually packed directly into  aar, Reduce compilation time . And like custom  View  Components , Because there will be more changes with version iteration , Directly in the form of source code  Library
7. Cross component communication
Reference answer :

7.1. Cross component communication scenario :

  • The first is the page jump between components  (Activity  To  Activity, Fragment  To  Fragment, Activity  To  Fragment, Fragment  To  Activity)  As well as data transfer during jump  ( Basic data types and serializable custom class types ).
  • The second is the call of custom classes and methods between components ( Components provide services to the outside ).

7.2. Analysis of cross component communication scheme :

  • The first kind of page jump between components is easy to realize , When you jump, you want to pass different types of data to provide corresponding  API that will do
  • The second kind of call between custom classes and custom methods is a little more complicated , need  ARouter  Cooperate with   Public service (CommonService)  Realization :
  • Business modules that provide services :
  • In public service (CommonService)  In a statement  Service  Interface  ( Contains custom methods that need to be called ),  Then implement this in your own module  Service  Interface ,  Re pass  ARouter API  Expose implementation classes .
  • Use the business module of the service : adopt  ARouter  Of  API  Get this  Service  Interface ( Polymorphism holds ,  The actual holding implementation class ),  You can call  Service  Custom methods declared in the interface ,  In this way, the interaction between modules can be achieved .
  • Besides , have access to  AndroidEventBus  It's unique  Tag,  It's easier to locate code that sends and receives events at development time ,  If you use the component name as  Tag  The prefixes are grouped ,  It can also better manage and view events of each component in a unified way ,  Of course, it is not recommended to use too much  EventBus.

7.3. How to manage too many routing tables ?

  • RouterHub  It's in the base library ,  It can be regarded as a communication protocol to be followed by all components ,  It can not only put routing address constants ,  You can also put a variety of names when transferring data across components  Key  value ,  With proper notes ,  Any component developer does not need to communicate in advance as long as they rely on this Protocol ,  I know how to work together ,  It improves efficiency and reduces the risk of error ,  It's natural that what you agree on is better than what you say
  • Tips:  If you feel like writing every routing address in the basic database  RouterHub  in ,  It's too troublesome ,  You can also create a private within each component  RouterHub,  Put routing addresses that do not need to cross components into private  RouterHub  In the management ,  Only put the routing address that needs to cross components into the public of the basic library  RouterHub  In the management ,  If you don't need to centrally manage all routing addresses ,  This is also a recommended way .

7.4.ARouter Routing principle :

ARouter Maintains a routing table Warehouse, All the module jump relationships are saved ,ARouter Route jump is actually called startActivity The jump , Using native Framework Mechanism , Only by apt The form of annotation creates jump rules , And artificially intercept jump and set jump conditions

Common componentization schemes are as follows

null

Conclusion

Due to space limitation , The detailed information of the document is too comprehensive , There are too many details , Therefore, only a partial screenshot of the knowledge points is provided for a rough introduction , Each small node has more detailed content ! Here's a screenshot of the catalog :

null
Because the whole document is comprehensive , More content , Space is not allowed , Here is a screenshot of  .

Another part is attached Android Structure interview video explanation :

null
原网站

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