-
Notifications
You must be signed in to change notification settings - Fork 1
/
flavor-plugin.gradle
433 lines (404 loc) · 17.8 KB
/
flavor-plugin.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.android'
//apply plugin: 'com.vanniktech.maven.publish'
apply plugin: 'maven-publish'
//关于flavor的更多信息,查看https://developer.android.google.cn/studio/build/build-variants?hl=zh-cn#groovy
//maven发布相关信息: https://developer.android.com/studio/build/maven-publish-plugin?hl=zh-cn
//这里可以进行项目lib统一配置
android {
compileSdkVersion 30
//
defaultConfig {
minSdkVersion 24
targetSdkVersion 30
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
// compileOptions {
// sourceCompatibility JavaVersion.VERSION_1_8
// targetCompatibility JavaVersion.VERSION_1_8
// }
// kotlinOptions {
// jvmTarget = "1.8"
// }
buildTypes {
release {
minifyEnabled false
//proguardFiles getDefaultProguardFile("proguard-android-optimize.txt") "proguard-rules.pro"
}
}
// 定义风味维度
flavorDimensions "channel"/* ,"pay"*/ //可以有多个,多个会进行组合
//声明产品风味
productFlavors {
//声明风味
first {
//指定风味维度
dimension "channel"
// 配置缺失维度策略,第一个参数是维度的名称,第二个维度是该维度对应的风味
missingDimensionStrategy "channel", "first"/*, "second"*/
//注意这里不设定的话,变体会和main中进行合并/覆盖,如果指定了则排除了main
// sourceSets {
// main.java.srcDirs = ['src/first/java']
// main.resources.srcDirs = ['src/first/res']
// }
//当依赖库中不包含当前风味,可以指定替代,取列表中的可用的第一个风味
// matchingFallbacks = ['debug', 'qa', 'release']
}
second {
dimension "channel"
missingDimensionStrategy "channel", "second"/*,"first"*/
//注意这里不设定的话,变体会和main中进行合并/覆盖,如果指定了则排除了main
// sourceSets {
// main.java.srcDirs = ['src/second/java']
// main.resources.srcDirs = ['src/second/res']
// }
}
}
// sourceSets.getByName("first") {
// main.java.srcDirs = ['src/first/java']
// main.resources.resDirs = ['src/first/res']
// }
// sourceSets.getByName("second") {
// main.java.srcDirs = ['src/second/java']
// main.resources.resDirs = ['src/first/res']
// }
// publishing {
// singleVariant("first"){}
// singleVariant("second")
// multipleVariants("first") {
// includeBuildTypeValues( "release")
// //includeFlavorDimensionAndValues("mode", "full")
// }
// multipleVariants("second") {
// includeBuildTypeValues( "release")
// //includeFlavorDimensionAndValues("mode", "full")
// }
// }
//过滤变种
// variantFilter { variant ->
// def names = variant.flavors*.name
// // To check for a certain build type, use variant.buildType.name == "<buildType>"
// if (names.contains("minApi21") && names.contains("demo")) {
// // Gradle ignores any variants that satisfy the conditions above.
// setIgnore(true)
// }
// }
}
//
//// Master task that will publish all variants
//def DefaultTask masterTask = project.tasks.create("installArchives", DefaultTask)
//
//
//android.libraryVariants.all { variant ->
//
// variant.outputs.forEach { output ->
//
// // Configuration defines which artifacts will be published, create one configuration for each variant output (artifact)
// def Configuration variantConfiguration = project.configurations.create("${variant.name}Archives")
// project.artifacts.add(variantConfiguration.name, output.packageLibrary)
//// project.artifacts.add(variantConfiguration.name, variant.packageLibrary)
//
// // Create one Upload type task for each configuration
// def Upload variantTask = project.tasks.create("${variant.name}Install", Upload)
// variantTask.configuration = variantConfiguration
//
//// variantTask.repositories.mavenInstaller {
//// pom.groupId = "com.yourcompany"
//// pom.artifactId = "yourLibrary"
//// pom.version = "1.0.0-${variant.name}" //Give a different version for each variant
//// pom.packaging = "aar"
//// }
//
// // Make all tasks depend on each other and on master task
// masterTask.dependsOn(variantTask)
// masterTask = variantTask
// }
// false
//}
//
//这是官方的 apply plugin: 'maven-publish' 的配置
//上传源码,支持java和kotlin
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier 'sources'
}
//publishing {
// publications {
// library(MavenPublication) {
// // 使用方引用 implementation 'com.mirko.android:lib:1.0.0-SNAPSHOT'
// groupId 'com.mirko.android'
// artifactId project.name
// version project.name
//
// // 必须有这个 否则不会上传AAR包
// afterEvaluate { artifact(tasks.getByName("bundleFirstReleaseAar")) }
// // 上传source,这样使用方可以看到方法注释
// artifact sourceJar
// }
// }
//}
//def String flavorName = ""
//
//task publishingFlavor() {
// android.libraryVariants.all { variant ->
// def String buildType = variant.buildType.name
// flavorName = variant.flavorName
// println("buildType=" + variant.buildType.name)
// println("variant=" + variant.flavorName)
//
// //遍历发布变种的release包
// if ("release".equals(buildType)) {
// // tasks.publishToMavenLocal.execute()
// }
// }
//}
//
//
//def String flavorName = "first"
////
//task publishMutilFlavor(group: "publish") {
//}
//
//
//task publishToFirst(group: "publish") {
// dependsOn(publishToMavenLocal)
// doFirst{
// flavorName = "first"
// }
// doLast {
// println("publishToFirst flavorType=------${flavorName}")
// flavorName = "second"
// //tasks.getByName(publishToSecond).execute()
// }
// println("publishToFirst flavorType=------${flavorName}")
//}
//
//task publishToSecond(group: "publish") {
// flavorName = "second"
// doFirst{
// flavorName = "second"
// println("publishToSecond doFirst flavorType=------${flavorName}")
//
// }
// doLast {
// println("publishToSecond flavorType=------${flavorName}")
//
// }
// println("publishToSecond flavorType=------${flavorName}")
// dependsOn(publishToMavenLocal)
//
//
//
//}
//
////
//publishMutilFlavor.dependsOn(publishToSecond)
//publishToSecond.dependsOn(publishToFirst)
////这种能打包,但对于变体module 依赖变体module的情况,无法失败,导致发布失败,暂时只能支持,本身是变体module不依赖变体的module
//所以这里暂时一次只发布一个变体,多个变体就多次发布。
afterEvaluate { // components.release 只有在配置完成之后,才能拿到值
println("variant=------" + android.libraryVariants.size())
publishing {
// 配置maven 仓库
repositories { RepositoryHandler handler ->
handler.mavenLocal()
}
// println("publishing customValue=------${customValue}")
// println("publishing flavorType=------${flavorName}")
println("publishing hasProperty=------${project.hasProperty('flavorName')}")
//通过gradle命令形式传参,如果没有对应参数,则不再执行
if (project.hasProperty('flavorName')) {
def flavorName = project.property('flavorName')
publications { PublicationContainer publicationContainer ->
println("publications=------")
// println("flavorName=------${flavorName}")
"${project.name}${flavorName}"(MavenPublication) {
// from components.release // 注释1:使用 Android Gradle 插件生成的组件,作为发布的内容,包含aar和依赖关系
from components."${flavorName}Release"
// 注释1:使用 Android Gradle 插件生成的组件,作为发布的内容,包含aar和依赖关系
artifact sourceJar // 上传源码
//用这种方法可以,但是依赖不全,即使手动补的,依然会丢失本地依赖,而直接用from components.release 则能保留全部依赖关系
// artifact(tasks.getByName("bundleFirstReleaseAar"))
// // 使用方引用 implementation 'com.mirko.android:lib:1.0.0-SNAPSHOT'
groupId = "com.mirko.android"
artifactId = project.name + "-${flavorName}"
version = "1.0.0-SNAPSHOT"
}
// secondRelease(MavenPublication) {
//// from components.release // 注释1:使用 Android Gradle 插件生成的组件,作为发布的内容,包含aar和依赖关系
// from components.secondRelease
// // 注释1:使用 Android Gradle 插件生成的组件,作为发布的内容,包含aar和依赖关系
// artifact sourceJar // 上传源码
// artifact(tasks.getByName("bundleSecondReleaseAar"))
//// // 使用方引用 implementation 'com.mirko.android:lib:1.0.0-SNAPSHOT'
// groupId = "com.mirko.android"
// artifactId = project.name + "-second"
// version = "1.0.0-SNAPSHOT"
// }
// android.libraryVariants.all { variant ->
// String buildType = variant.buildType.name
// String flavorName = variant.flavorName
//// if ("release".equals(buildType)) {
// if ("release".equals(buildType)) {
////
// "${project.name}${flavorName}${buildType}"(MavenPublication) {
//// from components.release // 注释1:使用 Android Gradle 插件生成的组件,作为发布的内容,包含aar和依赖关系
// from components."${flavorName}Release"
//
//// def result = components."${flavorName}Release"
//// println("publications=------" + result.name)
//// from result
//
//
//
// // 注释1:使用 Android Gradle 插件生成的组件,作为发布的内容,包含aar和依赖关系
// artifact sourceJar // 上传源码
//
//// // 使用方引用 implementation 'com.mirko.android:lib:1.0.0-SNAPSHOT'
// groupId = "com.mirko.android"
// artifactId = project.name + "-" + flavorName
// version = "1.0.0-SNAPSHOT"
////
//////// println("allDependencies=------${configurations} ${configurations.getProperties()}")
//////
////// pom.withXml {
//////
////// configurations."implementation".allDependencies.each {
////// println("allDependencies=------${it.group} ${it.name} ${it.version}")
//////
////// if(null != it.group) {
////// def dependencyNode = asNode().appendNode('dependencies').appendNode('dependency')
////// dependencyNode.appendNode('groupId', it.group)
////// dependencyNode.appendNode('artifactId', it.name)
////// dependencyNode.appendNode('version', it.version)
////// }
////// }
////// }
////
// }
//
//
// }
// }
}
}
}
// publishToFirst.dependsOn publishToMavenLocal
// publishToSecond.dependsOn publishToMavenLocal
}
//这个可以自己上传产物,但是依赖关系可能丢失
//publishing {
// repositories { RepositoryHandler handler ->
// handler.mavenLocal()
// }
// publications { PublicationContainer publicationContainer ->
// android.libraryVariants.all { variant ->
// String buildType = variant.buildType.name
// String flavorName = variant.flavorName
// if ("release".equals(buildType)) {
// "${project.name}${flavorName}${buildType}"(MavenPublication) {
// // 依赖 bundleReleaseAar 任务,并上传其产出的aar
// def name = flavorName.substring(0, 1).toUpperCase() + flavorName.substring(1)
// afterEvaluate { artifact(tasks.getByName("bundle${name}ReleaseAar")) }
// artifact sourceJar
// groupId = "com.mirko.android"
// artifactId = project.name + "-" + flavorName
// version = "1.0.0-SNAPSHOT"
// // pom文件中声明依赖,从而传递到使用方
//// pom.withXml {
//// def dependenciesNode = asNode().appendNode('dependencies')
//// configurations.implementation.allDependencies.each {
//// // 避免出现空节点或 artifactId=unspecified 的节点
//// if (it.group != null && (it.name != null && "unspecified" != it.name) && it.version != null) {
//// println "dependency=${it.toString()}"
//// def dependencyNode = dependenciesNode.appendNode('dependency')
//// dependencyNode.appendNode('groupId', it.group)
//// dependencyNode.appendNode('artifactId', it.name)
//// dependencyNode.appendNode('version', it.version)
//// dependencyNode.appendNode('scope', 'implementation')
//// }
//// }
//// }
//
// //带上依赖 ,否则会报错
// pom.withXml {
// def dependenciesNode = asNode().appendNode('dependencies')
//
// def scopes = [configurations.implementation]
// if (configurations.hasProperty("api")) {
// scopes.add(configurations.api)
// }
// if (configurations.hasProperty("implementation")) {
// scopes.add(configurations.implementation)
// }
// if (configurations.hasProperty("debugImplementation")) {
// scopes.add(configurations.debugImplementation)
// }
// if (configurations.hasProperty("releaseImplementation")) {
// scopes.add(configurations.releaseImplementation)
// }
//
//// if (project.ext.targetType != "jar") {
//// scopes.add(configurations.provided)
//// }
//
// scopes.each { scope ->
// scope.allDependencies.each {
// if (it instanceof ModuleDependency) {
// boolean isTransitive = ((ModuleDependency) it).transitive
// if (!isTransitive) {
// println "<<<< not transitive dependency: [${it.group}, ${it.name}, ${it.version}]"
// return
// }
// }
//
// if (it.group == "${project.rootProject.name}.libs" || it.version == 'unspecified') {
// return
// }
//
// if (it.group && it.name && it.version) {
// def dependencyNode = dependenciesNode.appendNode('dependency')
// dependencyNode.appendNode('groupId', it.group)
// dependencyNode.appendNode('artifactId', it.name)
// dependencyNode.appendNode('version', it.version)
// dependencyNode.appendNode('scope', scope.name)
// }
// }
// }
// }
// }
// }
// }
// }
//}
//android.libraryVariants.all { variant ->
// def String buildType = variant.buildType.name
// def flavorName = variant.flavorName
// println("buildType=" + variant.buildType.name)
// println("variant=" + variant.flavorName)
//
// //遍历发布变种的release包
// if ("release".equals(buildType)) {
// mavenPublish {
// coordinates("com.mirko.android", project.name, "1.0.0-SNAPSHOT")
//
// sonatypeHost = null
// releaseSigningEnabled = false
//// androidVariantToPublish = "${flavorName}Release"
//// androidVariantToPublish = "firstRelease"
//// androidVariantToPublish = "secondRelease"
// }
//mavenPublishing {
// coordinates("example.mylibrary", project.name+"-first", "1.0.3-first-SNAPSHOT")
//// androidVariantToPublish = "firstRelease"
//}
// }
//}
////这是 另一个 apply plugin: 'com.vanniktech.maven.publish' 的配置
//mavenPublish {
// sonatypeHost = null
// releaseSigningEnabled = false
//// androidVariantToPublish = "firstDebug"
// androidVariantToPublish = "firstRelease"
//}