1 Introduction
In the work, due to various reasons, it is necessary to merge two independently runnable App
projects into one git
warehouse, and there are other modules in the separate App
project 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 Studio
to open the same root folder of the two projects, and then the two projects can be loaded, and Android Studio
the 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 onMyModule1
thisLibrary
MyProject2
: It is a separate App and does not depend on otherLibrary
Next, we will take these two projects as an example and gradle
merge them into one project through modification
2. include function usage
First of all, we all know that in Gralde
, include
functions can be used to depend on subprojects.
For example, in our new project, settings.gradle
there will be such a sentence in , which is used to depend on app
modules.
include ":app"
When we click include
, we can find include
that 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:yyyy
in this form
3. Implementation steps
3.1 Create a new project
We know include
the usage, then it can be include
used to realize the merger.
First, let's create a brand new project, for example MyProjectTest
, and delete app
the code under its folder.
3.2 Copy the project to MyProjectTest
Copy MyProject1
and MyProject2
copy toMyProjectTest
3.3 Modify settings.gradle
MyProjectTest
The 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 Sync
button, it will prompt an error
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 Sync
the button again, and we can find that there are two more runnable project options
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 Studio
open it separately MyProject1
, we will find MyProject1
that the compilation error is reported.
Because we're going to implementation project(':MyModule1')
modify In order to implementation project(':MyProject1:MyModule1')
be MyProjectTest
coupled with .
This is obviously unreasonable. Ideally, after merging two projects, the separate projects can still Android Studio
run 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.gradle
specify MyModule1
the path.
rootProject.name = "MyProjectTest"
include ':MyProject1:app'
include ':MyModule1'
include ':MyProject2:app'
project(':MyModule1').projectDir = new File('/MyProject1/MyModule1')
Then modify MyProject1
the following dependencies . implementation project(':MyProject1:MyModule1')
implementation project(':MyModule1')
Click Sync
the 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 Studio
opened by MyProject1
itself.
4.2 Rename the subproject
Click Edit Configurations
, we can also rename the sub-project
Delete the name .app
, the final effect is as follows
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 MyProject2
there is a sub-project that can be run independently, how to integrate it?
In fact, it is the same, in MyProjectTest
the settings.gradle
, add include
dependencies.
include ':MyProject2:xxxx'
Here xxxx
is the name of this sub-project, and then Sync
click, 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