Android gradle configure global variables, manage global library version of the pros and cons


ext define the variable

Gradle mainly be achieved using the keyword ext.
ext defined in different positions, range and action manner subsequent use is not the same.

  1. Defined in root / build.gradle in the buildscript {}
ext {  
	va = 'aaa'
	vb = 'bbb'  
}
或
ext.va = 'aaa'
ext.vb = 'bbb'

As above, two global variables are defined. Other gradle file directly va, vbor "$va", "$vb"you can.

  1. In other positions root> build.gradle is allprojects with buildscript gradle or the same level; also a global variable.
    When calling needs rootProject.ext.vaor"$rootProject.ext.va"

  2. Gardle defined in general only valid in the current domain. Non-global


The global version management

Various versions of the library will depend on the configuration and unified position, that is, in the above-mentioned global ext. Gradle or addition of one file in order to set the configuration. Then apply from: "config.gradle 'introduced at an appropriate position as said before, is introduced at the position of two global variables follow should be paid attention.
As defined in the root / buildscript in

ext {
	refs = [
		junit : "junit:junit:4.12"
	]
}
//再来个这样的
ext.refs.testRunner = "com.android.support.test:runner:1.0.2"

Refs defined above is a map. You may ext.refs=[:]define an empty map, subsequent re-use.
use::

implementation refs.junit
implementation refs.testRunner

benefit

Unified management.
Or find a group name or which version of the library, go find a place on the line.


harm

Unable to-date information by the intelligence tips AS version. AS3.4 can not be in the project structure, see the relevant dependencies set, let alone updated.
To crack this thing, are you going to write a dependency gradle original file, and then when you want to see which libraries you want to update, use this file to compile gradle in settings.gradle in:project(":app").buildFileName = 'app_origin.gradle'

Packaging Failure

Met today, AS3.4, due to the configuration in productFlavors of a flavor in { minsdkVersion configs.minsdkVersion}, use the command and AS packaging, are being given the task can not be packaged.


Published 400 original articles · won praise 364 · Views 1.62 million +

Guess you like

Origin blog.csdn.net/jjwwmlp456/article/details/89973590