Android custom View: parallax animation

Time:2022-11-15

background

In the growth process of Android App, with more and more package functions, the team becomes more and more complex. In order to ensure the clarity and maintainability of functions, increase the cohesion of modules, reducegitpush the risk of code conflicts, etc.,Appdevelopment mode will gradually become integrated development mode

Integrated Development Mode

Each person is responsible for one or more modules, the modules are isolated from each other, packaged and upgraded independently, and finally integrated into the App for packaging in the form of remote dependencies

question

When using the integrated development mode, you will encounter a problem, because the main App is throughimplementThe remote dependencies are packaged. When developing locally, how to debug the local code of the module? Generally, our approach is as follows:

  1. existbuild.gradleChange remote dependencies to local dependencies
  2. existsetting.gradleDefine the module address of local dependencies in
// build.gradle file
dependencies {
    // This is how dependencies are defined in the main App
    implementation 'io.github.pettywing:floatwindow:1.0.0'
    // When the business is developed locally, change to
    implementation project(':floatwindow')
}

// setting.gradle file 
include ':floatwindow'
project(":floatwindow").projectDir = file("../floatwindow/floatwindow")

But this setting is very troublesome and the risk is high. Every time the project is pulled locally, it needs to be changed once, and when it is packaged at the remote end, it needs to be modified again. Is there any simple way?

solve

Create a new local_develop.gradle file locally and add it to git.ignore

ext {
    dependencies = [
            [
                    // Whether to enable the local source code dependency of the module, true is the source code dependency, false is the AAR dependency
                    isLocalModule: true,
                    // The relative path of the main project after the code warehouse is stored locally, because I put it in the same level directory of the main project, so the relative path here needs to jump out of the first-level directory
                    projectPath  : "../../android/Floatwindow",
                    // module name
                    projectName  : ":floatwindow",
                    // maven groupId:artifactId
                    projectMaven : "io.github.pettywing:floatwindow"
            ]
    ]
}

Add the following code in 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 a local dependency, replace it with the local project source code
            if (projectConfig["isLocalModule"]) {
                includeBuild(projectConfig["projectPath"]) {
                    dependencySubstitution {
                        substitute module(projectConfig["projectMaven"]) with project(projectConfig["projectName"])
                    }
                }
            }
        }
    }
}

In this way, when the project is packaged at the remote end, since there is nolocal_develop.gradle, which will be packaged with the remote dependency version. When the project is running locally, if you add local_develop.gradle, it will automatically change the remote dependency to the local dependency; and becauselocal_develop.gradle is ignored, multiple developers will not affect each other during use.