Android Gradle file dismantling

Gradle is a Groovy语言written building tool.
Application in Android lies mainly in build.gradleand settings.gradle
we can start with the Android's build.gradle(project)go research it's something.

1.build.gradle(Project)

First just find a piece of code inside out:
Here Insert Picture Description
as with groovy language, and so people do not understand the why of this code structure will feel a bit strange.
But because kotlin learned before, the structure of the code above is actually a closure , also known as first-class functions support . Inside the parameters of repositories, google (), jcenter ( ) is passed as a parameter to the allprojects () This function.
NOTE : The so-called closure, is passed to the function A can be a function B as a parameter. In Java there is no such concept, but with a similar approach, is the callback method listener. I am not here introduced before the Kotlin have talked about this concept: Kotlin learning (5) function and functional programming .
In fact, the above code is equivalent to this:

allprojects(new Action<Project>() {
    @Override
    void execute(Project project) {
        project.repositories{
            google()
            jcenter()
        }
    }
})

2.build.gradle(app)

2.1 buildTypes

Indicate that the module is used to which the crowd.

  • debug representation is for own use.
  • relase representation is used to outsiders
  • internal representation is used to insiders

Here Insert Picture Description
If you debug build in the project directory, or Release internal packet, then the main gradle automatically associated and corresponding packet code, so that the tag will form different patterns.
There are two general project will be the default type, you can see the Variant according to the leftmost Build Android Studio:
Here Insert Picture Description
Indicates the current two versions, debug and release versions, by switching version, or write code on the corresponding package, we can give different groups with different App. And the compiler when installing these versions, other versions of the code will be ignored, so this does not lead to increase in size or efficiency APK slow.

2.2 productFlavors

Its role and buildTypes almost, but it is another layer of dimension. This layer dimension is a dimension that we can define.
For example, we want the original debug App (own use), above relase (release), plus a layer of paid version and free version.
After so many one dimension, becomes: debug free version, debug paid version, relase free version, relase paid version, look at the code below:

android {
    ...
    //注意在声明productFlavors前,要声明flavorDimensions
    flavorDimensions 'cost'
    //其中free和paid是自己定义的维度,这个方法并不是groovy定义,而是通过闭包的特性被创建出这么一个方法出来
    productFlavors {
        free {
        }
        paid {
        }
    }
}

After building, then go see the Build Variants, you can see in the original debug, release one more dimension:
Here Insert Picture Description
it is not that already a little help out?
flavorDimensionsDeclares dimension attribute, you can define your own, free and paid above belong to pay do not pay the property, so I named it cost (you can also call other). I can also completely in it, one more dimension:

  //在付费版、免费版属性的基础上,再多加一个国际化的属性
  flavorDimensions 'cost', 'nation'
    productFlavors {
        free {
            dimension 'cost'
        }
        paid {
            dimension 'cost'
        }
       
        china {
            dimension 'nation'
        }
        international{
            dimension 'nation'
        }
    }

其中china一个版、国际一个版,并用 dimension来声明它是声明属性的
Here Insert Picture Description
这样子看,项目突然就复杂了起来,但是这样子确实是对一些需求很复杂、工程量很大的项目,划分的稍微清晰、维护也方便了一点。这就有点像后台服务端的正式服、测试服这么一个说法。这种做法针对当前产品做出来的,具体该怎么用,或者用还是不用,都是可以很快讨论出来的。

2.3 dependencies

Here Insert Picture Description
从闭包的角度去理解的话,implementation ‘xxxxx’ 其实就是跟下面的代码差不多

//下面是伪代码:implementation 'xxxxx' 
denpendenciesHandler.add('implementation','xxxxx')

compile、implementation、api的区别
complie在2018年被弃用,使用implementation和api取代之。而为什么会有这么一个现象,在于他们有不同的作用。
注1:首先compile、implementation、api这三个依赖方法并不是针对、解决远程依赖的问题的(因为我们单项目里用的很多都是远程的开源框架),因为这些远程框架他们更新的频率很低。而用来解决本地依赖所产生的一些问题!!!!!!
下面举例的Aproject、Bproject、Cproject的三个都是本地的项目。
注2:打包和编译是两个分开的概念。

  • compile
    传递依赖。
    假如Aproject 通过compile依赖Bproject, 并且Bproject 通过compile依赖了 Cproject,就产生了一条依赖链A->B->C
    那么编译之后,Aproject能够用引用Cproject的内容。
    但这样做是由劣势的,因为如果Cproject 进行了改变,那必然会导致Bproject进行一次重新的编译,而Bproject重新的编译会导致Aproject也会进行一次重新编译。
    假设如果Cproject改变了很多,那么整个依赖链都会产生多次重新编译。
    再假设如果Cproject的改变,Aproject实际上用不到,那么对于A来说,就产生了浪费(因为编译后这些C的东西已经确确实实存在了)。
    这就是为什么complie被弃用的原因。
  • implementation
    不会传递依赖
    如果Aproject 通过implementation依赖了 Bproject,并且Bproject通过 implementation依赖了Cproject,这个时候的依赖链就是 A->B B->C
    改变C对A来说并不会重新编译。而是只对B来说会重新编译。这样就能减少资源、性能的浪费
  • api
    和implementation没有太大的区别

3. gradlew

作为一个命令,可以通过 ./gradlew 或者 gradlew执行它。它的作用是运行gralde配置文件
如果本地没有任何gradle文件,则执行这个命令会生成一个gradle文件。
Here Insert Picture Description

4. gradle-wrapper.properties

Here Insert Picture Description
-All the file version of the tool will be able to view the statement expressed gradle greadle source, -bin was not

5. settings.gradle

Android project reasons of assembly, may comprise a plurality of sub
as shown below: The entire project from the app, Aproject, Bproject composition
Here Insert Picture Description
when grandle running first will find settings.gradle, inside view includeof the item contains. For example, this project (app) include in it, it will determine that he is the main projects and more projects.
In other words, settings.gralde judge this component is a multi-component item of the main assembly or subassembly gralde runtime (bit of a mouthful)
It is done when the initialization phase.

About doLast doFirst

You can use doLast in the task, doFirst when calling this task, which calls the method

doLast{
...
}

doFirst{
....
}

They define the task execution order. doFrist insert the task in the first execution, doLast executed after doFirst

Published 248 original articles · won praise 99 · Views 100,000 +

Guess you like

Origin blog.csdn.net/rikkatheworld/article/details/103718090