How to quickly build an app architecture for Android

Source: Internet
Author: User

Objective

Recently another project of the company, as the company's only Android engineer task came (the new mobile boss said the project or the main you are responsible, I will help you arrange the next progress ), listen to this I am sad in this company no matter how many or 1 Android development are I to build, Doing things that do not match the salary, the good thing is that no one interferes with the ability to learn what they want to learn at ordinary times.

How to choose an app architecture (MVC/MVP/MVVM)

More and more people are starting to talk about architecture recently. The same is true of my colleagues and engineers around me. Although I'm not particularly into the MVP, I still think it's a great idea, and I want to use it in my company's projects.

Project time is tight: rapid development Framework (forced)

There are also some quick development frameworks for Android on the web, and here are 3 key rapid development frameworks. For these rapid development framework, personally think can refer to, but is not recommended to use, because the whole app relies on a personal maintenance of the framework risk is too big, the framework has some learning costs, itself does not necessarily fully meet the needs of the app, the use of the code may be bloated, there is a schema limit.

    • Afinal

      GitHub Project Address: afinal

      Afinal is an Android Ioc,orm framework with four modules built into it: finalacitivity, Finalbitmap, Finaldb, Finalhttp. With finalactivity, you can bind UI and events in a way that is annotated. With Finalbitmap, you can easily load bitmap pictures without having to worry about oom and other issues. Through the Finaldb module, the Android SQLite database can be added and hacked through a line of code. With the Finalhttp module, you can request HTTP data in the form of Ajax.

      However, the project has not been updated since last year, and many of the IOC frameworks don't like it and performance is bad.

    • xUtils3.0

GitHub Project Address: xUtils3.0

    1. Xutils supports large files (over 2G) uploads, more comprehensive HTTP request protocol support (11 predicates), more flexible ORM, more event annotations support, and no confusion ...
    2. Xutils Minimum compatible Android 4.0 (API level 14). (Android 2.3?)
    3. XUTILS3 changes more so the new project is not maintained on the previous version (Github.com/wyouflf/xutils), as opposed to the previous version:

      • The HTTP implementation replaces httpclient with urlconnection, automatically resolves callback generics, and more secure breakpoint continuation policies.
      • Support standard cookie policy, differentiate domain, path ...
      • Event annotations Remove infrequently used features to improve performance.
      • Database API simplification improves performance to achieve and Greendao consistent performance.
      • Picture binding Support GIF (affected by System compatibility, some GIF files can only be displayed statically), WEBP; Support round, round, square and other cropping, support automatic rotation ...

      It can be seen that XUTILS3 is a good choice for rapid development.

Build your app from scratch with a simple look at these three architectural patterns:
    • Mvc:model-view-controller, Classic mode, easy to understand, the main drawbacks are two:
      The view's dependence on the model causes the view to also contain the business logic;
      The controller will become very thick and complex.
    • An evolutionary model of MVP:MODEL-VIEW-PRESENTER,MVC, the controller was replaced with a Presenter, mainly to solve the first shortcoming above, the View and Model decoupling, but the second drawback is still unresolved.
    • Mvvm:model-view-viewmodel, an optimized model for MVP, uses two-way binding: The change of View, which is automatically reflected in ViewModel, and vice versa.
In the face of many architectural patterns, which would you choose?

MVC,MVP or MVVM?

The more advanced the schema complexity, the more difficult it is to implement. And then build the project is to look at the needs of the project, others say you have to be practical only good, efficient implementation of the project's function is the best architectural model.

So, which one is the best?

Personally feel that suits you is the best, do not go blindly follow the follow-up, everyone said MVP good that you use, no practice there is no voice, so say with which architecture mode I do not express any opinion: the motive of any model is the same, that is how to avoid complex chaotic code, so that the implementation of unit testing becomes easy, Create high-quality applications and develop maintenance more efficiently.

When thinking about the architecture in a real project, I don't think about which model to use, I just think about how to do it faster and better with existing human resources and time resources, and how to prepare for later expansion or refactoring, with due consideration.

The schema in my project

This is the package schema for my last project:

Of course, according to the function of the package, the function of the project is not the same, and the sub-package is not the same, but basically similar.
So when you're sure about the architecture subcontracting, that's what you need to do.

As you can see from the above, we include logical functions and basic functions (common functionality) when architecting subcontracting.

Basic function Module:
    • Log Management System (Logmanager)

      Regardless of which project needs their own set of log management, one is for the production of debugging can be more efficient to view the filter log, and second, in order to package the release with a switch to control whether the log is printed. (My diary is for my Brother: Klog)

    • Exception Handling (Crashmanager)

      Function: When the program meets the abnormal situation, we can customize the exception handling, second, the program has different response to different models, then the test may not be found but we can upload the captured crash to the server, for the exception collection and bug fixes.

    • Utils (Tool Class)

      Tailoring your tool class to your project needs will greatly improve the speed of your project development (feedback, version validation updates you will be able to use)

      Look at the tool class for my last project:

    • Permission (Rights management System)

      This feature is absolutely required in the project, do not tell me that your project is not yet suitable for Android 6.0, suitable for sure will have rights management, I use the Android 6.0 permissions to handle the project in the practice, but also can, anyway, GitHub on the rights management of Open source things more, feel appropriate on OK.


Haha, so that your basic functions are set up, and then there are some logical functions of the package. Logic function module: 1. Encapsulate your own application and Baseactivity class, the maximum possible savings code, and join the MVP idea to structure. 2. Select your preferred network request framework and properly encapsulate it to speed up the development efficiency. 3. For sliding conflicts that may occur with nested scroll controls, or if the display is not complete we prefer to customize Viewpager,listview,gridview. 4. Package the ListView or Recyclerview to create a universal adapter, I think Xiang Brother's package is good [to create a universal adapter] (Https://github.com/hongyangAndroid/baseAdapter). 5. The general network data format is JSON (we tease: normal data JSON, swipe transaction data XML), so I JSON format with Gson encapsulation, XML format is temporarily used by pull parsing bean object encapsulation. 6. The database package, the data is not high requirements can be used in a simple package of the original curd just fine, ask for a higher point then use a third party's good. The choice of third-party open source libraries in the development process image loading library:
    • Glide: Compared to Uil,glide can support GIF and short video, support and activity,fragment,application life cycle linkage, support okhttp, volley

    • Fresco: Three-level cache awesome, better support for multi-frame animated pictures, such as Gif, WebP

    • UIL: old-style Although no longer updated maintenance, but powerful

      Choose according to your project needs, be familiar with UIL and use it, personal recommendation glide

Network Request Library:
    • Okhttp


      Okhttp is a high-performance HTTP library, supports synchronous, asynchronous, and implements the Spdy, HTTP2, WebSocket protocol, the API is simple and easy to use, and volley to implement the HTTP protocol cache.

    • Retrofit

      Simplified the network request process, while the internal Okhtttp client encapsulation, and 2.x to the previous 1.x version of some of the inappropriate responsibilities are transferred to Okhttp (for example, log, currently used Okhttp interceptor to achieve)

    • Volley:

      Volley is a simple asynchronous HTTP library, that's all. The disadvantage is that synchronization is not supported, which limits the development model, and cannot post big data, so it is not suitable for uploading files.

Personal advice to use the Retrofit,volley is not very versatile (most information).

Event Bus Library:

It is mainly used for message/event transmission, but it can realize decoupling between the components.

Both eventBus3.0 and Otto use annotations (@Subscribe, @Produce) to annotate the method, and Otto's more usage scenarios are in the main thread, relatively lightweight.

If you don't care about being lightweight, I think two is about the same, but many people recommend using Otto.

Dependency Injection Libraries:

Butterknife8.0:https://github.com/jakewharton/butterknife
Using Butterknife in any project is correct and no problem. A very lightweight library, because of the high performance savings code, and not the reflection mechanism that you want to implement.

Dagger2: It is not dynamic (when used without reflection at all) but the resulting code has the same level of simplicity and performance as handwritten code.

2 are great, you can choose the amount.

Database storage:
    • Litepal:litepal is an open-source Android database framework, it uses the object-relational mapping (ORM) mode, Litepal is very "light", jar package only 100k, use is relatively simple, the source address is Litepal address, Guo Shen developed the cattle.

    • Greendao:greendao and Litepal, the principle is not based on the reflection of the database operations, but the first to manually generate business needs of the model and DAO files, the business can directly call the corresponding DAO file for database operations, This avoids the performance loss and inefficiency caused by reflection. However, because of the need to manually generate model and DAO files, the configuration of Greendao is slightly more complex.

Greendao use up cumbersome but high efficiency, litepal use simple, so you choose it, the individual still feel litepal easy.

Simple caching

Asimplecache:asimplecache is a lightweight, open-source caching framework developed for Android. Lightweight to only one Java file (reduced by more than 10 classes).

    • You can cache normal strings, jsonobject, Jsonarray, Bitmap, drawable, serialized Java objects, and byte data. Ordinary strings, Jsonobject, Jsonarray, Bitmap, drawable, serialized Java objects, and byte data.
    • Replace Sharepreference as a configuration file
    • Network request data can be cached, such as Oschina's Android client can cache the content of the HTTP request, the cache time is assumed to be 1 hours, after timeout, automatically expire, let the client re-request new data, reduce client traffic, while reducing server concurrency.

Haha project needs the basic architecture required by the open Source Library has, you can rest assured that the development.

Summarize

In fact, the structure is not so difficult, and do not say how good to do, you have to believe that there is always a thing is right for you, to play a metaphor app architecture is to build a house, brick less on the lid, but must be strong, as if the framework is not necessarily strong but must be robust and scalable.

It's not too early, everybody rest early.

For more material Design series effects, please go to star my Https://github.com/zilianliuxue/AndroidStudy

How to quickly build an app architecture for Android

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.