Focus on Finddreams, learn together, progress together: http://blog.csdn.net/finddreams/article/details/45504133
Now developers of mobile developers, both Android and iOS, tend to prefer people with independent development skills because the app project is relatively small, and sometimes a person can be uniquely qualified for a project. So if we want to be able to have independent development capabilities, rapid Agile development is what we have to think about, after all, a person to do the app is a lot of things to do, for the project to go online, we need to use a variety of third-party libraries and frameworks, so that you can make a beautiful UI interface, so that the code is neat, Improve the efficiency of our development and so on.
1. What is Dependency injection (DI)?
Dependency Injection is a software design pattern that allows you to delete and change hard-coded dependencies both at run time and at compile time.
Let's just say here. One of the three frameworks for Java EE development is called ioc--control inversion: It's about creating an instance of an object. Control is stripped from code control to IOC container control, in effect we control it in an XML file, such as a spring control. In layman's terms, the previous creation of an instance object requires the programmer to invoke it proactively to execute it, but the control reversal is given to the container to invoke execution automatically. In fact, Android itself is an IOC-based mechanism.
Now that you know the concept, let's take a look at what several DI frameworks are used today.
It can be seen that there are three kinds of dependency injection frameworks, one is class injection, like Dagger, one is view injection, very famous butter knife, and one is more comprehensive, there is roboguice,androidannotations. Dragger This framework is relatively troublesome to use, here for the time being do not introduce, the following to compare the other three frames, see which one you fit?
Let's take a look at the wording before we use any Dependency injection framework:
class exampleactivityExtendsActivity{TextViewTitleTextViewSubtitleTextViewFooter @Overridepublic void OnCreate(Bundle savedinstancestate){Super.oncreate(savedinstancestate); Setcontentview(R. Layout. simple_activity); title =(TextView)Findviewbyid(R. ID. title); Subtitle =(TextView)Findviewbyid(R. ID. subtitle); Footer =(TextView)Findviewbyid(R. ID. footer); //TODO UseViews ...}
After reading the most native writing, we take a look at the wording of the dependency injection framework and see if it's a lot simpler and faster.
2.ButterKnife
This is a view injection framework developed by Daniel Jake Wharton on GitHub to allow developers to write as little code as possible.
Website address: http://jakewharton.github.io/butterknife/
After using the Butterknife framework we write the code above to see if it is a lot simpler, and we don't have to repeat the Findviewbyid () method.
class exampleactivity extends activity { @InjectView (r.id.title) TextView title; @InjectView (r.id.subtitle) TextView subtitle; @InjectView (r.id.footer) TextView footer; @Override public void onCreate (Bundle savedinstancestate) {super . OnCreate (savedinstancestate); Setcontentview (r.layout.simple_activity); Butterknife.inject (this ); //TODO use "injected" views ... }}
However, there is a place to note when using the Butterknife.jar package, you need to configure annotation procession, or you may be able to report a null pointer exception.
Methods: 1, mouse alignment needs annotated items, right-click Poperties–>java compiler–>
2, Annotation procession–> hook to Enable project specific settings The others will automatically hook up
3, –> Factory Path (Hook enable project specific settings) –> last add .... JARs the jar package that was just downloaded. This allows the Butterknife configuration to be used.
There may be some students of Eclipse Click Project Right Poperties,javacompiler there is no annotation procession this option, then you need to configure the plug-in, install:/http Download.eclipse.org/releases/juno
Butterknife also has a lot of other useful features that can be used to learn more about the website.
3.AndroidAnnotations
Androidannotations is an open-source framework that allows you to quickly develop Android, which makes your code leaner and easier to maintain, with the goal of "fast Android Development.easy Maintainance ".
Using this framework can simplify at least half the amount of code compared to native.
GitHub Address: Https://github.com/excilys/androidannotations
Characteristics:
1. Using Dependency Injection (Dependency injection)
2. Simplified threading model (simplified threading models)
3. Event Binding
4. REST Client
5, no magic [do not know why this is called, literal translation comes from: no magic, it means: Androidannotations in the compilation
will produce a subclass (next you will understand), you see this subclass, you can see how it works]
The code is as follows:
Import Android. App. Activity; Import Android. Widgets. EditText; Import Android. Widgets. TextView; Importcom. Googlecode. Androidannotations. Annotations. Click; Importcom. Googlecode. Androidannotations. Annotations. Eactivity; Importcom. Googlecode. Androidannotations. Annotations. Viewbyid; @EActivity (R. Layout. Main) public class MyActivity extends Activity {@ViewById (R. ID. Myinput) EditText Myinput; @ViewById (R. ID. Mytextview) TextView TextView; @Click void MyButton () {String name = Myinput. GetText(). toString(); TextView. SetText("Finddreams"+name); } }
Note: Use Androidannotations to remember, compile time will generate a subclass, the name of this subclass is after the original class with an underscore "", for example, this example produces a subclass named "MyActivity", This requires you to register this activity, in the Androidmanifest.xml to change myactivity to Myactivity_, when used is also used myactivity_ to represent this class.
If you jump from another activity to startactivity (new Intent (This,myactivity_.class)), note that Myactivity_.class is not myactivity.class;
Androidannotations is a very powerful rapid development framework, and it is very useful to know how to use it to improve our development efficiency. This is not a detailed introduction.
4.Roboguice
Roboguice is a dependency injection framework for Android apps, and using Google's official Guice location greatly simplifies Android's dependency injection. Make your Android app development smoother, easier and more fun to program.
By using Roboguice, you can inject a view view control, a resource, a system service, or any other object. Roboguice can help you streamline your application's code. Fewer code means fewer problems or bugs, so you can devote more effort to the parts of your project that need to be written or modified, making it easier to read the code.
Using the Roboguice library:
1. Control injection: Initializes the control with the @injectviews method, for example: @InjectView (r.id.textview1) TextView textView1.
2. Resource injection: Initializes the resource with the @injectresources method, for example: @InjectResource (r.string.app_name) string name.
3. System Service Injection: Initializes and obtains system services using the @inject method, for example: @Inject layoutinflater inflater.
4.POJO Object injection: Use the @inject method to inject and initialize the POJO object, for example: @Inject foo foo.
Using Roboguice
You have to inherit roboactivity or robofragment before you can use the Roboguice dependency injection function.
Public class testactivity extends roboactivity{ @InjectView(r.id.textview1) TextView textView1;@InjectView(R.ID.TEXTVIEW2) TextView textView2;@InjectView(r.id.imageview1) ImageView imageView1;@InjectResource(R.string.app_name) String name;@InjectResource(R.drawable.ic_launcher) Drawable Iclauncher;@InjectLocationmanager Locmanager;@InjectLayoutinflater Inflater;@InjectNotificationmanager Notifymanager;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.layout_test); Textview1.settext (name); }}
Benefits of using Roboguice
You do not need to initialize the control and use @injectviews if necessary.
There is no need to initialize the system service and use @inject if necessary.
There is no need to initialize like drawable,string and other resources, use @injectresource if necessary.
These practices can help you streamline your code.
The less code, the less problems and bugs.
A small amount of code makes it easier for Android developers to focus on the actual business logic.
Just as we programmers often say that C and C + + run fast, java,php run slowly, because these languages have sacrificed some performance to help developers learn and maintain, but these sacrifices are worth it.
Best of all, I would say learning to use any of the dependency Injection class libraries is a good choice, because it will greatly improve your development speed and really give you the ability to develop apps quickly and independently.
Android Rapid Development Essentials--Dependency Injection (DI) class Library selection Butterknife,androidannotations,roboguice