background

In the growth process of Android App, with more and more package functions and more and more complex teams, in order to ensure clear and maintainability of functions, increase the cohesion of modules, reduce the risk of Git push code conflicts and so on, the development mode of App will gradually become integrated development mode

  • Integrated development pattern

Each person is responsible for one or more modules, which are isolated from each other, packaged and upgraded independently, and finally integrated into the App for packaging in a remote dependent manner

The problem

There is a problem when adopting integrated development mode. Since the main App is packaged with remote dependencies on Implement, how do you debug the module’s native code when developing locally? Our general approach is as follows:

  • Change the remote dependencies to local dependencies in build.gradle
  • Define locally dependent module addresses in setting.gradle
/ / build. Gradle file
dependencies {
    // This is how dependencies are defined in the main App
    implementation 'the IO. Making. Pettywing: floatwindow: 1.0.0'
    // When the business is being developed locally
    implementation project(':floatwindow')}/ / setting. Gradle file
include ':floatwindow'
project(":floatwindow").projectDir = file(".. /floatwindow/floatwindow")

Copy the code

However, this setting is very troublesome and risky. Every time the project is pulled to the local area, it has to be changed again when it is packaged in the remote area. Is there an easy way to do this

To solve

  1. Create a new local local_develop.gradle file and add it to git.ignore
ext {
    dependencies = [
            [
                    // Whether to enable the module's local source dependency, true source dependency, false AAR dependency
                    isLocalModule: true.// The repository is stored locally relative to the main project path, because I put it in the same directory as the main project, so the relative path here needs to jump out of the level 1 directory
                    projectPath : ".. /.. /android/Floatwindow".// module name
                    projectName : ":floatwindow".// maven groupId:artifactId
                    projectMaven : "io.github.pettywing:floatwindow"]]}Copy the code
  1. Add the following code to setting.gradle
File localPropertiesFile = new File(rootDir.getAbsolutePath() + "/local.properties")
File localDevelopFile = new File(rootDir.getAbsolutePath() + "/local_develop.gradle")
if (localPropertiesFile.exists() && localDevelopFile.exists()) {
    apply from: "local_develop.gradle"
    if (ext.has("dependencies")) {
        ext.getProperty("dependencies").each { projectConfig ->
            // If the project is locally dependent, use the local project source code instead
            if (projectConfig["isLocalModule"]) {
                includeBuild(projectConfig["projectPath"]) {
                    dependencySubstitution {
                        substitute module(projectConfig["projectMaven"]) with project(projectConfig["projectName"])}}}}}}Copy the code

So when the project is packaged remotely, it will be packaged with a remote dependent version because there is no local_develop.gradle. When your project runs locally, if you add local_develop.gradle, it automatically changes the remote dependencies to local dependencies. And because local_develop.gradle is ignored, multiple developers can use it without affecting each other.

practice

AndroidUi warehouse

other

Git Git Git Git Git Git Git Git Git Git Git Git Git Git