Android Gradle operation, merge two projects into one project

1 Introduction

In the work, due to various reasons, it is necessary to merge two independently runnable Appprojects into one gitwarehouse, and there are other modules in the separate Appproject Module.
If you just copy two projects to the same folder, you still have to open each project separately, which is very inconvenient.
And the effect I want to achieve is Android Studioto open the same root folder of the two projects, and then the two projects can be loaded, and Android Studiothe project can be switched internally to compile and run.

As shown in the image below, we now have two projects

  • MyProject1: There is a dependency on MyModule1thisLibrary
  • MyProject2: It is a separate App and does not depend on otherLibrary

Next, we will take these two projects as an example and gradlemerge them into one project through modification

insert image description here

2. include function usage

First of all, we all know that in Gralde, includefunctions can be used to depend on subprojects.
For example, in our new project, settings.gradlethere will be such a sentence in , which is used to depend on appmodules.

include ":app"

When we click include, we can find includethat it is actually a method, and the parameter is a variable string, which can pass in the paths of several items, such as:app

default void include(String... projectPaths) {
    
    
    this.include((Iterable)Arrays.asList(projectPaths));
}

So what if the project needs to be in a subdirectory?
Here :is used to divide the directory, so the project that depends on the subdirectory is :xxxx:yyyyin this form

3. Implementation steps

3.1 Create a new project

We know includethe usage, then it can be includeused to realize the merger.
First, let's create a brand new project, for example MyProjectTest, and delete appthe code under its folder.

insert image description here

3.2 Copy the project to MyProjectTest

Copy MyProject1and MyProject2copy toMyProjectTest
insert image description here

3.3 Modify settings.gradle

MyProjectTestThe modification settings.gradle
was originally two lines of code like this

rootProject.name = "MyProjectTest"
include ':app'

change into

rootProject.name = "MyProjectTest"
include ':MyProject1:app'
include ':MyProject1:MyModule1'
include ':MyProject2:app'

Click the Syncbutton, it will prompt an error
insert image description here
because in MyProject1, we rely onMyModule1

implementation project(':MyModule1')

So the prompt here cannot be found, and needs to be modified to:MyProject1:MyModule1

implementation project(':MyProject1:MyModule1')

At this time, we click Syncthe button again, and we can find that there are two more runnable project options

insert image description here
Select a project, click to run, and you can find that it is installed on the phone normally.

4. Further optimization

At this point, the goal of this article is basically completed, realizing the merger of two projects into one project.
However, at this time, if we Android Studioopen it separately MyProject1, we will find MyProject1that the compilation error is reported.
Because we're going to implementation project(':MyModule1')modify In order to implementation project(':MyProject1:MyModule1')be MyProjectTestcoupled with .
This is obviously unreasonable. Ideally, after merging two projects, the separate projects can still Android Studiorun independently on .
So what should we do?

4.1 Specify the path to the project

In fact, we can use project.projectDir , to specify the path of the subproject.

Modified MyProjectTest, settings.gradlespecify MyModule1the path.

rootProject.name = "MyProjectTest"

include ':MyProject1:app'
include ':MyModule1'
include ':MyProject2:app'

project(':MyModule1').projectDir = new File('/MyProject1/MyModule1')

Then modify MyProject1the following dependencies . implementation project(':MyProject1:MyModule1')implementation project(':MyModule1')

Click Syncthe button again, click Run, and find that it can still run.

Moreover, at this time, it can also be run alone if it is Android Studioopened by MyProject1itself.

4.2 Rename the subproject

insert image description here

Click Edit Configurations, we can also rename the sub-project

insert image description here
Delete the name .app, the final effect is as follows

insert image description here
So far, we have completed all the operations of merging two projects into one project.

5. If there is another project that can be run separately?

If MyProject2there is a sub-project that can be run independently, how to integrate it?
In fact, it is the same, in MyProjectTestthe settings.gradle, add includedependencies.

include ':MyProject2:xxxx'

Here xxxxis the name of this sub-project, and then Syncclick, you can see the separate running entry of this sub-project.

6. Other

6.1 Source code of this article

Source code download address: In Android Studio, merge two projects into one project

6.2 Reference

[Android Gradle plugin] settings.gradle configuration file (configuration basic function | include function usage | directory level configuration | modify Module module build script name) _setting.gradle build multi-project settings build package name

Guess you like

Origin blog.csdn.net/EthanCo/article/details/132056793