Android component-based actual combat, zero foundation can also understand

Posted by Notre on Wed, 15 Dec 2021 02:34:30 +0100

 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);

    }

}







Just extract it into the public Module.

  1. Arouter is unable to jump

    Can't jump after opening InstantRun (can't jump under higher version Gradle plug-in)?

terms of settlement:

## Recommended learning materials

* Android Complete Handbook of advanced learning

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

* Android Benchmarking Ali P7 Learning video

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

* BAT TMD Big factory Android High frequency interview questions

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

(String input, Type clazz) {

        return GsonUtils.parseObject(input, clazz);

    }

}







Just extract it into the public Module.

  1. Arouter is unable to jump

    Can't jump after opening InstantRun (can't jump under higher version Gradle plug-in)?

terms of settlement:

## Recommended learning materials

* Android Complete Handbook of advanced learning

  [External chain picture transfer...(img-ZexfUZ64-1630901771681)]

* Android Benchmarking Ali P7 Learning video

  [External chain picture transfer...(img-VktaLcgf-1630901771683)]

* BAT TMD Big factory Android High frequency interview questions

[External chain picture transfer...(img-8D9Rw9Rg-1630901771684)]

**[CodeChina Open source projects:< Android Summary of study notes+Mobile architecture video+Real interview questions for large factories+Project practice source code](https://codechina.csdn.net/m0_60958482/android_p7)**

Topics: Android Design Pattern