Android component-based actual combat, ant gold clothes push on all sides

Posted by -Mike- on Wed, 15 Dec 2021 10:45:20 +0100

(1) Distinguish between application and library

if (isApplicationModule1.toBoolean()){

    apply plugin: 'com.android.application'

}else{

    apply plugin: 'com.android.library'

}





(2) Add applicationId

 defaultConfig {

        if (isApplicationModule1.toBoolean()){

            applicationId "com.demo.myapplication.module1"

        }

    }





(3) Android manifest startup portal

 sourceSets {

        main {

            if (isApplicationModule1.toBoolean()) {

                manifest.srcFile 'src/main/module/AndroidManifest.xml'

            } else {

                manifest.srcFile 'src/main/AndroidManifest.xml'

            }

        }

    }





4. Introduce other modules into the main app module

One thing to note is that if each component runs independently, it cannot be used as a dependency library for the app module, so the build.exe under the app module Dependency judgment is required in the gradle file

    if (!isApplicationModule1.toBoolean()){

        implementation project(':module1')

    }



    if (!isApplicationModule2.toBoolean()){

        implementation project(':module2')

    }

    if (!isApplicationModule3.toBoolean()){

        implementation project(':module3')

    }



    implementation project(':common')





Introduce common module into Module

    implementation project(':common')





At this point, each Module can run independently by modifying the configuration.

5. Introduction of Arouter

Due to the decoupling between modules, the usual Intent jump requires guided packets. After Arouter is introduced, it can jump without guided packets.

Integrating Arouter

(1) Introduce dependency

Add in common

    api("com.alibaba:arouter-api:$rootProject.ext.arouterLibVersion") { exclude group: 'com.android.support' }

    annotationProcessor "com.alibaba:arouter-compiler:$rootProject.ext.arouterAnnotationLibVersion"





Annotations need to be added under each Module

annotationProcessor "com.alibaba:arouter-compiler:$rootProject.ext.arouterAnnotationLibVersion"





(2) Build. Of each Module introduced into Arouter Add the following code to gradle

defaultConfig {



        ...



        //arouter

        javaCompileOptions {

            annotationProcessorOptions {

                arguments = [moduleName: project.getName()]

            }

        }

    }



(3) Initialize in Application

       if (isDebug) {

            ARouter.openLog();

            ARouter.openDebug();

        }

        ARouter.init(this);



(4) Jump using Arouter

Define route

@Route(path = "/app/main")

public class MainActivity extends AppCompatActivity {

...

}



Jump route

ARouter.getInstance().build("/app/main").navigation();



6. ButterKnife's pit

There is no problem using this Library for a single project, but an error is reported when using it as a component. In addition to a series of configurations, there is also an uncomfortable place that you need to change r in the Library to R2, but when you want to change the Library to Application, you have to change R2 to R, isn't it very uncomfortable.

!!! So I'm currently giving up using ButterKnife in the Library

7. Pit of switch (view. Getid())

Due to the view in the Library Getid may be a variable, so you need to change switch to if.

Shortcut:

Move the cursor over the line of switch, press alt + Enter, and click Replace switch to if

8. Duplication of resources

 No static field XXX of type I in class Lcom/XXX/R$id error





The problem is that the layout and main layout names are repeated. Get into the habit of adding an identifier in front of the resource file in the Library, such as library_layout

Another way is just to help add constraints and reminders.

android {

    compileSdkVersion rootProject.ext.compileSdkVersion

    buildToolsVersion rootProject.ext.buildToolsVersion



    // Add resource constraints and reminders

    if (toolsIsLibrary.toBoolean()) {

       resourcePrefix "${project.name}_"

    }

}





To ensure that it does not conflict with other module s, it is recommended to prefix resource files such as string, style, color and dimens ions.

6, Stepping pit

1. ARouter componentized packaging failed

Problem: program type already present: com alibaba. android. arouter. routes. ARouterGroupGroupservice

The same Group cannot be used in ModuleA and ModuleB.

According to the prompt, the error service is a duplicate Group. After global search, it is found that the following codes exist in both ModuleA and ModuleB.

package com.bp.tech.common.util;



import android.content.Context;



import com.alibaba.android.arouter.facade.annotation.Route;

import com.alibaba.android.arouter.facade.service.SerializationService;



import java.lang.reflect.Type;



@Route(path = "/service/json")

public class JsonServiceImpl implements SerializationService {



    @Override

    public void init(Context context) {



    }



    @Override

    public <T> T json2Object(String text, Class<T> clazz) {

        return GsonUtils.parseObject(text, clazz);

    }



    @Override

    public String object2Json(Object instance) {

        return GsonUtils.toJsonString(instance);

    }



    @Override

    public <T> T parseObject(String input, Type clazz) {

        return GsonUtils.parseObject(input, clazz);

    }
# ending

- tencent T4 level Android Brain map of architecture technology; Leak detection and vacancy filling, systematization, in-depth learning and improvement

![img](https://img-blog.csdnimg.cn/img_convert/bf440fcd5a63c0cc2fe7ee9afb017f25.png)

- **First line Internet Android Interview questions with detailed explanations (elementary to advanced topics)**

> These questions were asked during the interview with Tencent, Baidu, Xiaomi, LETV, meituan, 58, cheetah, 360, Sina, Sohu and other front-line Internet companies this year. And most of them have sorted out the answers. Being familiar with these knowledge points will greatly increase the probability of passing the first two rounds of technical interviews

![img](https://img-blog.csdnimg.cn/img_convert/9f1f4e6d0c6429753c283b37a27ca3cd.png)

> have Android Development 3-5 I hope to break through the bottleneck and become a small partner of architects. You can pay attention to me

**[CodeChina Open source projects:< Android study PDF+Architecture video+Interview documents+Source code Notes](https://codechina.csdn.net/m0_60958482/android_p7)**



**<Android study PDF+Architecture video+Interview documents+Source code Notes "to get learning materials and improve yourself to challenge BAT The interview is difficult**

**Why share for free?**

Technical brain map; Leak detection and vacancy filling, systematization, in-depth learning and improvement

[External chain picture transfer...(img-8NNL4Sak-1630821113365)]

- **First line Internet Android Interview questions with detailed explanations (elementary to advanced topics)**

> These questions were asked during the interview with Tencent, Baidu, Xiaomi, LETV, meituan, 58, cheetah, 360, Sina, Sohu and other front-line Internet companies this year. And most of them have sorted out the answers. Being familiar with these knowledge points will greatly increase the probability of passing the first two rounds of technical interviews

[External chain picture transfer...(img-Kd40bPjF-1630821113367)]

> have Android Development 3-5 I hope to break through the bottleneck and become a small partner of architects. You can pay attention to me

**[CodeChina Open source projects:< Android study PDF+Architecture video+Interview documents+Source code Notes](https://codechina.csdn.net/m0_60958482/android_p7)**



**<Android study PDF+Architecture video+Interview documents+Source code Notes "to get learning materials and improve yourself to challenge BAT The interview is difficult**

**Why share for free?**

> If the threshold is set, many developers will miss this set of advanced architecture materials and the possibility of becoming architects. This has lost our original intention; So that more people can improve their technology and pattern, promotion and salary increase through efficient and high-quality learning.

Topics: Android Design Pattern