title: gradle使用 date: 2019-01-10 18:38:26 tags: [工具] categories: 工具

https://docs.gradle.org/current/userguide/customizing_dependency_resolution_behavior.html#sec:component_selection_rules http://google.github.io/android-gradle-dsl/current/ https://www.kotlincn.net/docs/reference/server-overview.html

https://blog.csdn.net/u010014658/article/details/78999031

gradle命令:

  • gradle -v //版本号
  • gradle clean //清除build文件夹
  • gradle build //检查依赖并打包
  • gradle assembleDebug //编译打包Debug包
  • gradle assembleRelease //编译打包Release包
  • gradle installRelease //打包并安装Release包
  • gradle unstallRelease //卸载Release包
  • gradle dependencies //查看依赖图表
  • gradle clean build -x test //跳过测试编译
  • gradle –profile build //分析构建任务
  • gradle build –dry-run //编译并不执行任务
  • gradle install //安置项目jar包到本地Maven仓库
  • gradle tasks //查看Gradle任务
  • gradle tasks –all //查看所有Gradle任务
  • gradle build –daemon //使用Gradle守护程序(Daemon)
  • gradle build –offline //用离线模式运行
  • gradle clean build –refresh-dependencies //刷新Gradle依赖缓存
项目中存在的多个module,或者依赖的Library中引用了相同的库,但库的版本不一致。例如主项目中引用的是Glide4.2,但依赖的第三方库中使用的却是Glide3.8。此时需要统一项目中Glide的版本。
1
2
3
4
5
6
7
在build.gradle中添加如下配置:
configurations.all {
resolutionStrategy {
force 'com.github.bumptech.glide:glide:4.2.0'
force 'com.github.bumptech.glide:compiler:4.2.0'
}
}
从命令行解析buildType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
project.getGradle().addListener(new DependencyResolutionListener() {
@Override
void beforeResolve(ResolvableDependencies dependencies) {
}

@Override
void afterResolve(ResolvableDependencies dependencies) {
def projectPath = dependencies.path.toLowerCase()
println("projectPath : ")
dependencies.resolutionResult.allDependencies.each { dependecy ->
println("depaaaaa: " + dependentComponents.toString())
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//1. 从命令行中解析出buildType
def currentBuildType = "debug" //debug
gradle.startParameter.taskNames.each({
String taskNameL = it.toLowerCase();
if(taskNameL.contains("assemble") || taskNameL.contains("install")) {
if(taskNameL.contains("debug")) {
currentBuildType = "debug"
return;
} else if(taskNameL.contains("release")) {
currentBuildType = "release"
return;
}else{
currentBuildType = "debug"
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
applicationVariants.all { variant ->
variant.packageApplication.outputDirectory =
new File(project.buildDir.absolutePath + "/outputs/apk")

variant.outputs.all {
outputFileName = "linkim.jar"
}
variant.getCompileConfiguration().resolutionStrategy {
// Use Gradle's ResolutionStrategy API
// to customize how this variant resolves dependencies.
println("resolutionStrategy : " + variant.buildType.name + ";project name : " + variant)
}
}
1
2
3
4
compile project(':libim',{ pj ->
//2. 将解析出的buildType设置给library
pj.ext.buildType = currentBuildType
})

库中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
roject.getGradle().addListener(new DependencyResolutionListener() {
@Override
void beforeResolve(ResolvableDependencies dependencies) {
}

@Override
void afterResolve(ResolvableDependencies dependencies) {
def projectPath = dependencies.path.toLowerCase()
println("libim projectPath : " + projectPath)
}
})

def currentBuildType = extensions.extraProperties.has("buildType") ? "${ext.buildType}" : "debug"



if(currentBuildType == "debug"){
provided rootProject.ext.depsLibs.ljim
}else{
provided("com.lianjia.sdk:chatui:2.32.0@aar")
}
坚持原创技术分享,您的支持将鼓励我继续创作!