Android Studio not only allows you to create modules for your App and dependent libraries, but it also creates modules for Android Wear,android Tv,google app Engine, which can be used in a single project. For a chestnut, you may need to use Google Clound or Android Wear in the late stages of your app development. In this case, you can have three modules under your project: App,google cloud,android wear integration, respectively. Understanding multi-module builds under one project will accelerate your development cycle.
In this chapter, we will learn the construction of multi-modules and show some practical and useful chestnuts:
Structure of multi-module construction
Typically, a project contains multiple modules, which are under a parent directory folder. To tell Gradle the structure of the project and which subfolder contains the module, you need to provide a settings.gradle file. Each module can provide its own build.gradle file. We've learned about how setting.gradle and build.gradle work, and now we just need to learn how to use them.
Here is the structure diagram of the multi-module project:
project ├─── setting.gradle ├─── build.gradle ├─── app │ └─── build.gradle └─── library └─── build.gradle
This is the simplest and most straightforward way to create your multi-module project. The Setting.gradle file declares all the modules under the project, and it should look like this:
include ‘:app‘, ‘:library‘
This ensures that both the app and library modules are included in the build configuration. All you need to do is just add subfolders for your module.
In order to add the Library module as its dependency package in your app module, you need to add the following to the app's Build.gradle file:
dependencies { compile project(‘:library‘) }
In order to add a module to the app as a dependency, you need to use the project () method, which has the parameters of the module path.
If the submodule is also included in your module, Gradle can meet your requirements. For a chestnut, you can define your directory structure as:
project├─── setting.gradle├─── build.grade├─── app│ └─── build.gradle└─── libraries ├─── library1 │ └─── build.gradle └─── library2 └─── build.gradle
The app module is still in the root directory, but now the project has 2 different dependent packages. These dependent modules are not located at the root of the project, but within a specific dependent folder. Based on this structure, you need to define this in settings.xml:
include ‘:app‘, ‘:libraries:library1‘, ‘:libraries:library2‘
You will notice that it is also very easy to declare the module in the subdirectory. All paths are centered around the root directory, which means that when you add a module that is located under a subfolder as a dependency package for another module, you should make the path the root directory. This means that if the app module wants to rely on the Library1,build.gradle file in the example above, it needs to be declared:
dependencies { compile project(‘:libraries:library1‘)}
If you declare a dependency in a subdirectory, all paths should be related to the root directory. This is because Gradle defines your dependent packages based on the root directory of your project.
Build life cycle
Understanding the build process makes it easy to understand how multiple modules are built. We talked earlier about the life cycle of the build. So now you should know the basic process, but some very important details may not be very clear to you.
In the first step, the initialization phase, Gradle will look for the Settings.grade file. If the file does not exist, then Gradle assumes you only have a separate build module. If you have multiple modules, the Settings.gradle file defines the location of the modules. If these subdirectories contain their own build.gradle files, Gradle will run them and merge them into the build task. This explains why you need to declare that the dependencies declared in a module are relative to the root directory.
Once you understand how the build task aggregates all of the modules together, it's easy to understand how several different building multi-module strategies will be made. You can configure all the modules in the root directory under the Build.gradle. This allows you to simply browse the entire project configuration, but this will become mess, especially if your module requires a different plugin. Another way is to separate the configuration of each module, which ensures that each module does not interfere with each other. This also makes it easy for you to track the build changes, because you don't have to point out which module is causing the error.
The biggest strategy for gradle is mixing. You can define a build file in the root directory to define the same familiarity of all modules, and then configure the build file in each module to only the parameters that belong to that module. Android Studio follows this principle by creating a Build.gradle file in the root directory, and then creating another build file under each module folder.
Module tasks
When you have multiple modules in your project, you need to think about them before you run the task. When you run a task on the command line interface, Gradle will find out which module will perform the task. For a chestnut, when you have a mobile app module and an Android Wear module, you run the Gradlew assembledebug task. When you change the folder location of one of the modules, Gradle will only run which special module, even if you use Gradle wrapper in the root directory. Raise a chestnut when you run. /gradlew Assembledebug in the Android Wear module directory, it will only build Android Wear module.
Switching between different folders to perform different tasks can be frustrating, and fortunately, we have other options. You can prepare a special task to execute your module. For a chestnut, to build only the Android Wear module, you simply run Gradlew:wear:assembleDebug in the root directory.
Add a module to your project
Adding a new module to Android Studio is an easy thing to do, and the view will also create a build file for you. As shown in the following:
Adding Java Dependent libraries
When you create a new Java module, the Build.grade file will be like this:
‘java‘ dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])}
Java modules use Java Plugins, which means that many Android features are not available here because you don't need them.
The build file also has basic library management, and you can add jar files under the Libs folder. You can add more dependent libraries, according to the contents of chapter Iii.
It's easy to add Java modules to your app module, isn't it?
dependencies { compile project(‘:javalib‘)}
This tells Gradle to introduce a module called Javelin, and if you add this dependency to your app module, the Javalib module will always be built before your app module is built.
Add Android Dependent libraries
Similarly, we created the Android module using the graphical interface of Android studio and then built the file as follows:
plugin: ‘com.android.library‘
Remember: The Android Dependency library contains not only Java code, but also Android resources like manifest and strings,layout files, and after you introduce the module, you can use all of the module's classes and resource files.
Suggestions
I have a bit of advice about multi-module projects, and there are some things you should know clearly that will save you time.
Running modules in Android studio tasks
When you have multiple modules, Android Studio will parse it out and show it in Cradle:
Grade graphics makes it easy to run tasks between modules, but it does not run a single task for all modules at the same time, so if you want to do this, the quickest way is to use the command line.
Accelerate your multi-module build
When you build your multi-module project, Gradle executes all the modules in turn. When your computer's memory is large enough, it will make your build process more multithreaded. This feature already exists in Gradle, but it is turned off by default.
So if you want to start parallel build, you need to configure the following properties in the Grade.properties file:
org.gradle.parallel=true
Gradle will choose as many threads as possible to execute your build process, and each thread executes a module. The parallel executes a standalone module, i.e. your module is independent.
Module coupling
That is, you can refer to the properties of other modules in one module, but I do not recommend that you do so, and we can define these properties in the build file in the root directory entirely.
Summarize
We learned how to build multiple modules in a project and how adding new modules affected the execution of the build task.
We studied the relevant cases and gave some suggestions.
In the next section, we will learn how to test and use Gradle to make your tests easier. We will learn how to run your unit tests in the JVM and how to run the tests on your phone.
Original address: 1190000004247809
Follow the public number for more information:
Android-gradle (Fri)