Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

What is Gradle

Oh ho, speaking of Gradle, I’m sure everyone who is an Android developer is familiar with it, but… How to use it well and what convenience it can bring to our development is not clear to most Android guys. Let me give you a quick overview of gradle’s role in Android development.

The following introduction, all are thick, a thorough understanding of gradle many articles, write very well, too, but what he said is too professional, not for me this new small white understanding, the following point of view, as I understand, if there are bosses are willing to correct, hope that the friendly in the comments section, I will learn from them, it will be a great help to me At the same time, in this statement, This article is a rough technical literacy text. Not professional article!! In the future, I will re-create gradle when I have a deeper understanding of it. Thank you in advance for your kind comments.

  • Build tools (compile, package)
  • Introduce dependencies (method calls like dependencies, implementation, etc.)
  • Configure versions (compileSdkVersion, buildToolsVersion, minSdkVersion, etc.)
  • Configure dependent environments (AllProjects, BuildScript, CLASspath, etc.)
  • Upload dependencies to remote (upload to remote repositories such as Maven)
  • Gradle Plugin (Kotlin-Android, com.android. Library, etc.)

. There are a lot of, temporarily can not recall, can not find the language description, you big guy comment area to help me supplement!!

So what’s your tip

I don’t know if there are any multi-channel packages in major companies, or some function descriptions of different platforms are different. I worked in my previous company, because the company’s main business is tool apps, and the names of apps on different application platforms are different. Before I knew the flavorDismensions and productFlavors of Gradle, I could make A package for Platform A first, and then change the code manually and change the package for platform B. This is time-consuming and error-prone.

  • First define flavorDimensions “buildVersionApk”, “version” in android closure
  • Associate dimensions with defined methods in the productFlavors closure, and then replace the resource files inside
  • To do this, run the gradle assembleXXX command
  • Finally, you can see the apK you want in the modified file path
android{ ... Def flavorMap = [buildA: "test1", buildB: "test1", buildC: ",] // flavorDimensions "buildApk", Flavormap. buildA resValue "string", "productFlavors {buildA {// CreateFlavors '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 "ultimate output path > > : ${variant.getPackageApplication().outputDirectory}${File.separator}${outputFileName}" println "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="}}}}Copy the code

Words are too dry. Let’s do it

Haha, do you understand

Note that if you want to replace the app_name, remember to mask the code in the resource file, otherwise an error will be reported