The ultimate solution for Android components

Source: Internet
Author: User
Tags jcenter

Android Modular Project address: Android Modular Project Androidmodulepattern

      • How fragment or view supports component
      • How to manage components
How fragment or view supports component

The release of Android component Solutions has been more than half a year, although the program has been able to solve the needs of some projects, but still not perfect. Many developers also comment on blogs and GitHub or even email me and ask me, what about fragment? Currently on the market, the style of the app is similar to the interface of a lot more, several fragment in the main interface, and then click on the Navigationbar on the icon to show the different fragment. But it's obvious that Android is a modular solution
is not suitable for this situation. At first I thought of a less elegant compromise, which was to move our app's mainactivity to the app Shell project, because the app Shell project itself was tasked with managing and assembling the business components, So this mainactivity naturally gets the fragment that is scattered among the other business components, like this:

Although this scheme is feasible, it is clearly not the result we expect, our ideal "app Shell project" should not be related to the business, he should be responsible for the management and assembly of other components, and packaging these business components into a market can be posted to the app, that is to say we want "app Shell project" Do not relate to any business, do not coupling the code in other components, I think I can arbitrarily replace the shell project, without affecting my app packaging, obviously this lazy solution is not to do this. So the question I have to solve is: How does a business component get fragment or other view in these business components without relying on other business components?

Suppose little a receives an invitation to attend an Internet technology conference, which is held in a "xx hotel", but little a has not heard of this hotel before, then how can he find this hotel and attend the meeting? Most students will habitually open Baidu map, and then enter "XX Grand Hotel", Baidu Map will help me find this hotel. But have you ever wondered why Baidu Maps can find this hotel? This time certainly someone will say: this is not nonsense, Baidu Map do not know who else know? It reminds me of the 08 when there was no smartphone, I wanted to go to a building in Lanzhou, but I asked a lot of passers-by that nobody knew where the building was. And now we go to a place from ask passers-by to ask Baidu Map, then back to which words, Baidu Map is how to know these places? There are two possibilities: one is to tell Baidu map a place where (that's what those small stores do), and the other is to send people to the city to keep track of all the notable landmarks in the city. Their relationship is like that:

In fact, in the Android component solution already has the similar function the component: Common component, and another one is arouter. But given that Arouter is an open source repository and we don't have the convenience to modify it, we're fiddling with the common component. What do we need to do if I want the common component to know the dfragment in the D component? First add the cfragment and dfragment to the common component, and the B component wants to get the dfragment, and go directly to the common component to find the line.

At this time you must be very excited, as if to find out what the secret, you can not wait to write a demo test this program. When you roll up your sleeves and start to dry, what do you find? How can I add dfragment to baseapplication? We all know that application will callback the OnCreate () method when it starts, it seems we can add fragment to baseapplication in the OnCreate method when application is started. At this time your mind will certainly pay the black question mark expression, can not let D components to rely on common it? This relationship is too special to mess up.

But after the front of the bedding, in fact, we have found something, that is: as long as we can know in the business components application life cycle, then we can be in application onCreate Add the fragment in the business component to the common component! so this time we need to solve: how can we let the business components know the application life cycle? Problem analysis Here, let's look at the following class diagram:

First we define a proxy interface in the common component, which defines the callback method in the application, then implements the proxy interface for each business component, and then does what it wants to do in the OnCreate method, Baseapplication will find all classes that implement Applicationdelegate when calling the OnCreate method, and invoke the methods of these implementation classes so that the business component knows the life cycle of our application; When a business component knows the application's declaration cycle, not only can the fragment be added to the common component in the business component, but the data can also be initialized in the business component, because the global context can be obtained in any component, In fact, this approach is equivalent to initializing the data in application.

How to manage components

In the Android component scenario, because all components are in the same project and rely on other components using compile project (': Component name ') , this can cause many problems.

1. Compilation is slow. since all component projects are in the same project, and the components or app shell projects will depend on other components, each package app needs to compile each component once, and if the components in the project reach more than 10, the results are really moving! As the number of components grows and the compilation time is almost exponentially increased, I think every Android developer has a deep understanding.
2. The component is not easy to reference. because our components are placed in the project in the form of source code, if another project requires a component, you can only copy the code to the new project at this time. This leads to the existence of a component in multiple projects, and ultimately there is no guarantee that the component's code will be modified, which means that the component is not guaranteed to be unique.
**3. Cannot control permissions, nor is it easy to confuse. Because the project contains all the component source code, there is certainly no way to control the code permissions, if a component is provided to you by another department or company, then they certainly do not want to give you the source code.

So what if we solve these problems? I think most Android developers can think of this approach. If you think of the open source tripartite library as a functional component, then obviously, what is the way we use these three-party libraries? Do you download its source code, it should be very few people will do it. So let's see how we introduced the three-party library:

    ‘com.github.bumptech.glide:glide:3.8.0‘    compile ‘io.reactivex.rxjava2:rxjava:2.1.3‘    compile ‘io.reactivex.rxjava2:rxandroid:2.0.1‘    compile ‘com.squareup.retrofit2:retrofit:2.3.0‘    compile ‘com.google.code.gson:gson:2.8.1‘ compile ‘org.greenrobot:eventbus:3.0.0‘
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

So everyone is familiar, these open source libraries are generally uploaded to Maven or Jcenter warehouse for our reference. Can we also upload our own components to Maven or Jcenter repositories? Of course, it's not going to get you to the open source repository, I mean we can build a private maven repository inside the company, upload our developed components to this private Maven repository, and the internal developers can easily bring the components into the project as they would reference the three-way library. It's their relationship, like this:

The main purpose of building warehouse management is as follows:

    1. Improve compilation performance and reliability
    2. Provides configuration management Center for all binary software components and their dependencies
    3. Provide an advanced configurable proxy for your organization and public warehouse
    4. Creating a private Component publishing center
    5. Improve the maintainability and manageability of components by improving their availability, versioning, security, and quality.

This also solves the problem we encountered in the component project. Originally I wanted to upload the components of Android component Project Androidmodulepattern to Jitpack , and then give you a demonstration, but unfortunately, I tried a lot of times have failed, we can only try.

The ultimate solution for Android components

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.