Gradle little knowledge for Android developers, hehe

Get into the habit of writing together! This is the first day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

What is Gradle

Oh ho, speaking of gradle, as long as you are an Android developer, I believe that no one is unfamiliar with it (I don't know anything about it in other fields, so we don't dare to say anything), but. . . How to use it well, and what convenience it can bring to our development, seems to be not very clear to most Android guys. Next, I will briefly introduce what role gradle plays in Android development.

The following introductions are all rough talk. There are many articles about in-depth understanding of gradle, and they are well written, but they are all too professional and not suitable for a novice like me to understand. The following points are all for my own self-understanding. The big guy is willing to correct me, and I hope to put it forward in the comment area in a friendly manner. I will study with an open mind, which will be of great help to me. At the same time, I hereby declare that this article is a rough technical literacy text. Not a professional article! ! , I will re-create it when I understand gradle in depth in the future. Thank you in advance for your friendly corrections.

  • Build tools (compile, package)
  • Introduce dependencies (method calls such as dependencies, implementation, etc.)
  • Configuration version (compileSdkVersion, buildToolsVersion, minSdkVersion, etc.)
  • Configure dependencies (allprojects, buildscript, classpath, etc.)
  • Upload dependencies to the remote end (upload to remote warehouses such as maven)
  • gradle plugin (kotlin-android 、com.android.library等)

... There are many more, I can't think of it for a while, and I can't find the language description. Help me add it in the comment area of ​​the big guys! !

So what is the little knowledge you want to talk about?

I don’t know if the big companies usually have multi-channel packaging, or if there are differences in the description of some functions on different platforms. I was in the previous company, because the company’s main business is tool apps, and the names of the apps on different application platforms are different. , When I didn't know gradle's flavorDismensions and productFlavors before, the method I used was to first make a package of platform A, then manually change the code, and change the package of platform B. This is not only time-consuming, but also prone to errors.

  • First define flavorDimensions "buildVersionApk", "version" in the android closure
  • Associate the dimension with the defined method in the productFlavors closure, and then replace the resource file inside
  • Run the gradle assembleXXX command to package
  • Finally, you can see the apk you want in the modified file path.
android{
...//其他配置

def flavorMap = [
        buildA : "test1",
        buildB : "test1",
        buildC   : "1.0.2",
]
//维度
flavorDimensions "buildApk", "version"
productFlavors {
    buildA {
        // 关联维度
        dimension 'buildApk'
        def appName = flavorMap.buildA
        resValue "string", "app_name", appName
    }
    buildB {
        dimension 'buildApk'
        def appName = flavorMap.buildB
        resValue "string", "app_name", appName
    }
    buildC{
        dimension "version"
        def appVersion = flavorMap.buildC
        resValue "string", "app_version", appVersion
    }
}
applicationVariants.all { variant ->
    variant.outputs.all {
        if (outputFile.name.contains("release")) {
            def appName = flavorMap[variant.productFlavors[0].name]
            variant.getPackageApplication().outputDirectory = new File(project.rootProject.rootDir, "/outputApk")
            outputFileName = "${appName}_v${versionName}.apk"

            println "======================================================================"
            println "终极输出路径>> : ${variant.getPackageApplication().outputDirectory}${File.separator}${outputFileName}"
            println "======================================================================"
        }
    }
}
}
复制代码

It's too dry to say words, let's go to the case

image.png

haha, do you understand?

For the above operations, you can directly copy the code. One thing to note is that if you want to replace the app_name, remember to block the code in the resource file, otherwise an error will be reported.

Guess you like

Origin juejin.im/post/7081828085925937166