Github-tenzliu /TenzModuleDemo: Android componentized demo

preface

Some time ago to see the recent has been very hot Android componentization and then he masturbated a demo, encountered a lot of problems and pits, in this record, specific can see the project source.

Componentization is nothing more than switching several modules under our App project between com.android.application and com.android.library. Divided into Modulelib (library module, some basic tool classes), ModuleMain (entry module), Moduleone (module 1), moduleTwo (module 2), App is just a shell project.

1: Create a new Android project, but the App is just an empty shell project

Build. Gradle for each module. Build. Gradle for each module

Note that mudulelib is only used as a base library, so there is no need to change it. Then there is where isModule comes from, which is in gradle.properties for the entire project

3. The relationship between the App project and each Module:

First, Modulelib is responsible for referencing some third-party libraries, some basic code, and some utility classes

The App:

This means that when isModule=true is set, each module handling Modulelib can run tests independently, which means that it can be developed separately by multiple people, and can be merged by setting false

Module of each module:

Note that the Modulelib reference library we use is API, not implementation, because implementation only works on this Module, but when you go to an App, it doesn’t work; Some library references are unique, such as Butterknife:

Implementation ‘com. Jakewharton: butterknife: 8.8.1’

AnnotationProcessor ‘com. Jakewharton: butterknife – compiler: 8.8.1’

Implementation needs to be in modulelib, but annotationProcessor needs to be in each module.

4. How to use moduleMain as the entry for the project App:

Notice here that minifest under our App has no activity

The Modulemain minifest has a declaration under it that acts as the activity launcher

Once started, the program will run MainActivity below ModuleMain.

5: How to handle the interaction between modules, such as the jump of some activities, and the fragment between different Modules in the Champions League, it should be seen that I used the ARouter route. https://github.com/alibaba/ARouter.

As mentioned above, modulelib references API ‘com.alibaba:arouter-api:1.3.1’, AnnotationProcessor ‘com.alibaba: arouter-Compiler :1.1.4’

Then add it under defaultConfig in build.gradle for each module

javaCompileOptions {

annotationProcessorOptions {

arguments = [moduleName :project.getName() ]

}

}
Copy the code

This official document has instructions.

Then use the activity to jump to in the Module or the fragment to get

And then there’s the jump,

The same goes for fragments,

And fragment fetching

6: The resource ID in module is in switch, case is not available:

You can use if,else instead

7: The use of butterknife

I’m sure many people use butterknife annotations, but using BB Utterknife in a Module can also cause a pit,

As mentioned above, in modulelib reference API ‘com. Jakewharton: butterknife: 8.5.1, Then under the each module annotationProcessor ‘com. Jakewharton: butterknife – compiler: 8.5.1’

The official documentation for Butterknife also states that if you use it in modules, you need to add it to each module

applyplugin:’com.jakewharton.butterknife’

Of course, modulelib is not required, but this error will occur when you use the latest version of ButterKnife 8.8.1. I have been working here for a long time, and it turns out that the latest version of Butterknife is incompatible. I just need to turn down the version, and I used 8.5.1 here

There is also a problem with R resources for Module using ButterKnife. You cannot use R for resources for butterKnife annotations. Instead, you need to use R2 provided by ButterKnife to find resources

Module isModule=true (isModule=true)

Resource Documents:

Look at the content comparison

The rest of the work is done.

The ~

The original author: TenzLiu links: https://www.jianshu.com/p/416ca689ebc2

To read more

Android 2018 Interview questions with Answers — For intermediate and advanced level (PART 2)

The screen ADAPTS to all-round parsing

Kotlin Study Notes – A surprisingly fun list collection summary

My Android Refactoring Tour: Plugins

Believe in yourself, there is nothing impossible, only unexpected

It’s not just technology that’s gained here!