Skip to content

Commit 0fd8ce9

Browse files
author
huangx
committed
解决可动态调整manifestPlaceholders项目编译失败问题
1 parent 2f6695b commit 0fd8ce9

8 files changed

Lines changed: 77 additions & 49 deletions

File tree

app/build.gradle

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
3-
apply plugin: 'android-junk-code'
3+
if (PLUGIN_ENABLE.toBoolean()) {
4+
apply plugin: 'android-junk-code'
5+
}
46
apply plugin: 'com.getkeepsafe.dexcount'
57
apply plugin: 'kotlin-kapt'
68

@@ -30,23 +32,62 @@ android {
3032
checkReleaseBuilds false
3133
abortOnError false
3234
}
33-
}
3435

35-
androidJunkCode {
36-
def config = {
37-
packageBase = "cn.hx.plugin.ui"
38-
packageCount = 30
39-
activityCountPerPackage = 3
40-
excludeActivityJavaFile = false
41-
otherCountPerPackage = 50
42-
methodCountPerClass = 20
43-
resPrefix = "junk_"
44-
drawableCount = 300
45-
stringCount = 300
36+
flavorDimensions 'PRODUCT'
37+
38+
productFlavors {
39+
free {
40+
dimension 'PRODUCT'
41+
}
42+
pro {
43+
dimension 'PRODUCT'
44+
}
45+
}
46+
47+
applicationVariants.all { variant ->
48+
def mergedFlavor = variant.mergedFlavor
49+
switch (variant.flavorName) {
50+
case "free":
51+
switch (variant.buildType.name) {
52+
case "debug":
53+
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey1"]
54+
break
55+
case "release":
56+
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey2"]
57+
break
58+
}
59+
break
60+
case "pro":
61+
switch (variant.buildType.name) {
62+
case "debug":
63+
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey3"]
64+
break
65+
case "release":
66+
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey4"]
67+
break
68+
}
69+
break
70+
}
4671
}
47-
variantConfig {
48-
debug config
49-
release config
72+
}
73+
74+
if (PLUGIN_ENABLE.toBoolean()) {
75+
androidJunkCode {
76+
def config = {
77+
packageBase = "cn.hx.plugin.ui"
78+
packageCount = 30
79+
activityCountPerPackage = 30
80+
excludeActivityJavaFile = false
81+
otherCountPerPackage = 50
82+
methodCountPerClass = 20
83+
resPrefix = "junk_"
84+
drawableCount = 300
85+
stringCount = 300
86+
}
87+
variantConfig {
88+
freeRelease config
89+
proRelease config
90+
}
5091
}
5192
}
5293

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
android:roundIcon="@mipmap/ic_launcher_round"
1111
android:supportsRtl="true"
1212
android:theme="@style/AppTheme">
13+
14+
<meta-data android:name="API_KEY" android:value="${API_KEY}"/>
1315
<activity
1416
android:name=".MainActivity"
1517
android:exported="true">

build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ buildscript {
99
dependencies {
1010
classpath 'com.android.tools.build:gradle:4.2.1'
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12-
classpath "com.github.qq549631030:android-junk-code:1.1.2"
13-
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0'
1412
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:3.0.1"
15-
13+
// classpath project(":library")
14+
classpath "com.github.qq549631030:android-junk-code:1.1.3"
1615
// NOTE: Do not place your application dependencies here; they belong
1716
// in the individual module build.gradle files
1817
}

gradle.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ kotlin.code.style=official
2222

2323
org.gradle.warning.mode=all
2424

25-
RELEASE_SIGNING_ENABLED=true
25+
RELEASE_SIGNING_ENABLED=true
26+
27+
PLUGIN_ENABLE = true

library/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
buildscript {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0'
8+
}
9+
}
10+
111
apply plugin: 'groovy'
212
apply plugin: "com.vanniktech.maven.publish"
313

library/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#project
22
GROUP=com.github.qq549631030
3-
VERSION_NAME=1.1.2
3+
VERSION_NAME=1.1.3
44

55
POM_ARTIFACT_ID=android-junk-code
66
POM_NAME=AndroidJunkCode

library/src/main/groovy/cn/hx/plugin/junkcode/ext/AndroidJunkCodeExt.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.gradle.api.Action
44
import org.gradle.api.NamedDomainObjectContainer
55

66
class AndroidJunkCodeExt {
7-
Map<String, Closure<JunkCodeConfig>> configMap = [:]
87

98
NamedDomainObjectContainer<JunkCodeConfig> variantConfig
109

library/src/main/groovy/cn/hx/plugin/junkcode/plugin/AndroidJunkCodePlugin.groovy

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
1111

1212
@Override
1313
void apply(Project project) {
14-
def android = project.extensions.getByType(AppExtension)
14+
def android = project.extensions.findByType(AppExtension)
1515
if (!android) {
1616
throw IllegalArgumentException("must apply this plugin after 'com.android.application'")
1717
}
@@ -25,27 +25,6 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
2525
createGenerateJunkCodeTask(project, android, variant, junkCodeConfig)
2626
}
2727
}
28-
29-
project.afterEvaluate {
30-
android.applicationVariants.all { variant ->
31-
def variantName = variant.name
32-
def generateJunkCodeTaskName = "generate${variantName.capitalize()}JunkCode"
33-
def generateJunkCodeTask = project.tasks.findByName(generateJunkCodeTaskName)
34-
if (generateJunkCodeTask) {
35-
//已经用variantConfig方式配置过了
36-
return
37-
}
38-
def closure = generateJunkCodeExt.configMap[variantName]
39-
if (closure) {
40-
def junkCodeConfig = new JunkCodeConfig()
41-
closure.delegate = junkCodeConfig
42-
closure.resolveStrategy = Closure.DELEGATE_FIRST
43-
closure.call()
44-
println("AndroidJunkCode: configMap配置方式已过时,请使用variantConfig配置方式")
45-
createGenerateJunkCodeTask(project, android, variant, junkCodeConfig)
46-
}
47-
}
48-
}
4928
}
5029

5130
private def createGenerateJunkCodeTask = { project, android, variant, junkCodeConfig ->
@@ -69,11 +48,7 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
6948
for (int i = variant.sourceSets.size() - 1; i >= 0; i--) {
7049
def sourceSet = variant.sourceSets[i]
7150
if (!sourceSet.manifestFile.exists()) {
72-
android.sourceSets."${sourceSet.name}".manifest.srcFile(manifestFile.absolutePath)
73-
def processMainManifestTask = project.tasks.findByName("process${variantName.capitalize()}MainManifest")
74-
if (processMainManifestTask) {
75-
processMainManifestTask.dependsOn(generateJunkCodeTask)
76-
}
51+
sourceSet.manifest.srcFile(manifestFile.absolutePath)
7752
break
7853
}
7954
}

0 commit comments

Comments
 (0)