当前位置:网站首页>Master this promotion path and share interview materials
Master this promotion path and share interview materials
2022-07-07 16:45:00 【InfoQ】
Android Advanced extension point
1、 How to do unit testing , How to ensure App Stable ?
- 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 ;
2.Android How to view the recycling status of an object in ?
- 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 .
3.Apk How to compress the size of ?
- 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 .
- 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 )
- Add resource obfuscation
- 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 .
- 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
4. How to use Gradle Configure multichannel package ?
- Take the statistics of the friendship League as an example
- First, in the manifest.xml Set dynamic channel variables in the file :
- And then app In the catalog build.gradle Middle configuration productFlavors, That is, to configure the channel of packaging :
- 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
- host : Is currently running APP.
- plug-in unit : Compared to plug-in technology , Is to load the running apk Class file .
- 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
- 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
- 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 .
7. Cross component communication
- 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 ).
- 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.
- 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 .
Conclusion
边栏推荐
猜你喜欢
随机推荐
字节跳动高工面试,轻松入门flutter
二叉搜索树(特性篇)
Opencv configuration 2019vs
node:504报错
logback. XML configure logs of different levels and set color output
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
Detailed explanation of several ideas for implementing timed tasks in PHP
pycharm 终端部启用虚拟环境
[designmode] flyweight pattern
Personal notes of graphics (4)
thinkphp3.2.3中设置路由,优化url
掌握这套精编Android高级面试题解析,oppoAndroid面试题
打造All-in-One应用开发平台,轻流树立无代码行业标杆
time标准库
Binary search tree (features)
DNS 系列(一):为什么更新了 DNS 记录不生效?
两类更新丢失及解决办法
ByteDance Android gold, silver and four analysis, Android interview question app
Ray and OBB intersection detection
Personal notes of graphics (3)