current position:Home>What are the differences among the three Android annotation frameworks dagger, hilt and Koin?, Retrofit benefits

What are the differences among the three Android annotation frameworks dagger, hilt and Koin?, Retrofit benefits

2022-02-04 16:47:56 mb61ab4e6f736e7

[]( )Dagger

==================================================================

If we want to Dagger Provide an instance of a class , All we have to do is add... To the constructor [email protected]? annotation .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

After adding this annotation ,Dagger? A... Will be generated for this class at build time ?Factory. Under this use case , Because its class name is ?CompositeAdapter, It generates a file called ?CompositeAdapter**_**Factory Class .

This class contains the creation of ?CompositeAdapter? Class .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

As you can see, the factory class implements ?get()? And returned a new ?CompositeAdapter? example . This is actually an implementation of this kind ?Provider ? Interface . Other classes can use ?Provider? Interface to get an instance of a class .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

[]( ) If we use Hilt Instead of Dagger Well ?

===================================================================================

In this case , It doesn't make any difference .Hilt? Is an internal use ?Dagger? The library of , The class I show you is composed of ?Dagger? Generated . If you use ?Hilt, It does generate some additional classes for us , These classes simplify ?Dagger? Use , And reduce the amount of boilerplate code we need to write . But the core remains the same .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

[]( )Koin

================================================================

Koin? And ?Dagger? as well as ?Hilt? comparison , The approach to managing dependencies is completely different . To be in Koin Register dependencies in , We won't use any annotations , because Koin No code will be generated . contrary , We must provide factories for modules , These modules will be used to create instances of each class required in the project .

Koin? Add references to these factory classes to ?InstancesRegistry? Class , This class contains references to all the factories we write .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

The ?map? Medium ?key? Is the full name of the class or the name provided when using named parameters . The corresponding value is the factory we write , Will be used to create an instance of the class .

To get dependencies , We need to call ?get()?( For example, in a factory class ) Or through ?activities? or ?fragments? Call in ?inject()? Delegate properties , So as to load ?get().get() Method will find the factory registered for the class of the given type , And inject it into it .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

[]( ) What's the impact? ?

==================================================================

Dagger Generate code to provide dependencies , and Koin No code generation , This actually has some impact .

1. Error handling

because Dagger? Is a compile time dependency injection framework , If we forget to provide some dependencies , We will know our mistakes almost immediately , Because our project will fail to build .

for example , If we forget to write to the constructor ?CompositeAdapter? Add [email protected]? annotation , And try to inject it into fragment in , The build will fail , And display the appropriate error , Tell us exactly what's wrong .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

stay Koin The situation is different in , Because it doesn't generate any code . If we forget to be ?CompositeAdapter Class add factory , The application will be successfully built , But it will throw out ?RuntimeException? Once we request an instance of this class . It may happen when the application starts , So we may notice it immediately , But it may also happen later on other screens or when the user performs certain actions .

Android Annotate three frames Dagger、Hilt and Koin What's the difference? ?,retrofit advantage

2. Impact on build time

Koin The advantage of not generating any code is : It has a much smaller impact on our build time .Dagger? You need to use an annotation processor to scan the code and generate the appropriate classes . This may take some time , It may slow down our construction .

summary

Finally, in order to help you understand Android The principle of relevant knowledge points and interview related knowledge , Here's what I've collected 24 set tencent 、 Bytes to beat 、 Ali 、 Baidu 2019-2021 Analysis of the real interview questions , I organized the technical points into Video and PDF( In fact, it took a lot more energy than expected ), package The context of knowledge + A lot of details .

also ? Advanced architecture technology advanced brain map 、Android Develop interview materials ? To help you learn and upgrade , It also saves time for people to search information on the Internet to learn , You can also share it with your friends and learn together .

 First line internet interview topic

379 page Android Advanced knowledge

379 page Android Advanced knowledge

Online learning Android A lot of information , But if the knowledge learned is not systematic , When you encounter a problem, you just have a taste of it , No further study , So it's hard to really improve the technology . I hope this systematic technical system has a direction reference for you .

2021 In spite of the bumpy road , It's all about Android To fall , however , Don't panic , Make your own plan , Learn from yourself , Competition is everywhere , This is true of every industry . Believe in yourself , Nothing is impossible , Only the unexpected . Wish you all 2021 Everything is going well in the new year .

This article has been  CODING Open source project :《Android Summary of learning notes + Mobile architecture video + The real interview question of Dachang + Project source code 》 Included

copyright notice
author[mb61ab4e6f736e7],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041647552093.html

Random recommended