In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the structure of Android multi-module construction". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Android studio allows you to create modules not only for your app and dependent libraries, but also for Android wear,Android TV,Google App Engine, etc., and these separate modules can be used in a separate project. Take Chestnut, for example, you may need to use Google Clound or Android Wear in the later stages of your app development. In this case, you can have three modules under your project: app,google cloud,Android Wear integration. Understanding the multi-module build under one project will speed up your development cycle.
Multi-module structure
Typically, a project contains multiple modules, which are in a parent directory folder. To tell gradle the structure of the project and which subfolder contains modules, you need to provide a settings.gradle file. Each module can provide its own independent 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.
This is the structure diagram of the multi-module project:
Project ├─── setting.gradle ├─── build.gradle ├─── app │ └─── build.gradle └─── library └─── build.gradle
This is the easiest and most direct 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 app and library modules are included in the build configuration. All you need to do is add subfolders for your module.
To add the library module as its dependency package to your app module, you need to add the following to the build.gradle file of app:
Dependencies {compile project (': library')}
To add a module as a dependency to app, you need to use the project () method, which takes the module path as an argument.
If you include submodules in your module, gradle can meet your requirements. For example, Chestnut, you can define your directory structure as follows:
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 two different dependent packages. These dependent modules are not located in the root directory of the project, but in a specific dependent folder. According to this structure, you need to define it in settings.xml as follows:
Include': app',': libraries:library1',': libraries:library2'
You will notice that it is also very easy to declare the module in the subdirectory. All paths revolve around the root directory, that is, when you add a module under a subfolder as a dependency package for another module, you should set the path as the root directory. This means that if the app module wants to rely on the library1,build.gradle file in the above example, it needs to be declared as follows:
Dependencies {compile project (': libraries:library1')}
If you declare dependencies in a subdirectory, all paths should be related to the root directory. This is because gradle defines your dependency packages based on the root directory of your project.
Build lifecycle
Understanding the build process makes it easy for you to understand multi-module builds. We talked a long time ago about the lifecycle 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 * * step, that is, the initialization phase, gradle will find the settings.grade file. If the file does not exist, then gradle will assume that you only have a separate build module. If you have multiple modules, the settings.gradle file defines the location of those 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 brings all the modules together, it becomes easy to understand several different building multi-module strategies. You can configure build.gradle for all modules in the root directory. This allows you to easily browse the configuration of the entire project, but it will become a mess, especially if your module requires different plug-ins. Another way is to separate the configuration of each module, which ensures that each module does not interfere with each other. It also makes it easy for you to track build changes, because you don't need to point out which changes caused which module errors, and so on.
Gradle's * strategy is hybrid. You can define a build file in the root directory to define the same familiarity of all modules, and then configure the parameters that only belong to that module in the build file in each module. Android studio follows this principle by creating a build.gradle file in the root directory and then another build file under each module folder.
Module tasks
When you have multiple modules in your project, you need to think before running the task. When you run a task from the command line interface, gradle will find out which module will perform the task. Take 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 that particular module, even if you use gradle wrapper in the root directory. Take Chestnut, for example, when you run.. / gradlew assembleDebug in the directory of the Android wear module, it only builds the Android wear module.
Switching between different folders to perform different tasks can be very uncomfortable, but fortunately, we have other ways. You can prepare a special task to execute your module. For example, to build only the Android Wear module, you just need to run gradlew: wear:assembleDebug in the root directory.
Add modules to your project
It's easy to add a new module to Android studio, and this view will also create a build file for you.
Add Java dependent libraries
When you create a new Java module, the build.grade file looks like this:
Apply plugin: 'java' dependencies {compile fileTree (dir:' libs', include: ['* .jar'])}
The Java module uses the Java plug-in, which means that many Android features cannot be used here because you don't need them.
Build files also have basic library management, you can add jar files in the libs folder. You can add more dependent libraries, according to Chapter 3.
It's easy to add a Java module 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 use the graphical interface of Android studio to create the Android module, and then the build file is as follows:
Apply plugin: 'com.android.library'
Remember: the Android dependent library contains not only Java code, but also Android resources, such as manifest and strings,layout files. After you introduce the module, you can use all the classes and resource files of the module.
Suggestion
I have some suggestions about multi-module projects, and there are some things you should know, which will save you time.
Run the module tasks in Android studio
When you have multiple modules, Android studio will analyze them and display them in cradle
Grade graphics makes it easy for you to run tasks between modules, but it doesn't run the same task for all modules at the same time, so if you want to do so, the quickest way is to use the command line.
Speed up your multi-module build
When you build your multi-module project, gradle executes all the modules in turn. When your computer has enough memory, making your build process multithreaded will be faster. This feature already exists in gradle, but it is turned off by default.
So if you want to start the parallel build, you need to configure the following properties in the grade.properties file:
Org.gradle.parallel=true
Gradle will select as many threads as possible to execute your build process, and each thread will execute a module. Parallel executes independent modules, that is, your modules are independent.
Module coupling
That is, you can reference the properties of other modules in one module, but I do not recommend that you do so, we can define these properties in the build file in the root directory.
This is the end of the content of "what is the structure of Android multi-module construction". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 226
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.