Skip to content

Commit b040630

Browse files
committed
fix(core.gradle-plugin): 支持从低版本AGP中获取Manifest
fixup! 8a99c4f fix #747
1 parent 520aa3f commit b040630

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

projects/sdk/core/gradle-plugin/src/main/kotlin/com/tencent/shadow/core/gradle/ShadowPlugin.kt

+50-9
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
package com.tencent.shadow.core.gradle
2020

21+
import com.android.SdkConstants.ANDROID_MANIFEST_XML
2122
import com.android.build.gradle.AppExtension
2223
import com.android.build.gradle.AppPlugin
2324
import com.android.build.gradle.BaseExtension
25+
import com.android.build.gradle.tasks.ManifestProcessorTask
26+
import com.android.build.gradle.tasks.ProcessApplicationManifest
2427
import com.android.build.gradle.tasks.ProcessMultiApkApplicationManifest
2528
import com.tencent.shadow.core.gradle.extensions.PackagePluginExtension
2629
import com.tencent.shadow.core.manifest_parser.generatePluginManifest
@@ -99,7 +102,7 @@ class ShadowPlugin : Plugin<Project> {
99102
}.forEach { pluginVariant ->
100103
val output = pluginVariant.outputs.first()
101104
val processManifestTask = output.processManifestProvider.get()
102-
val manifestFile = (processManifestTask as ProcessMultiApkApplicationManifest).mainMergedManifest.get().asFile
105+
val manifestFile = getManifestFile(processManifestTask)
103106
val variantName = manifestFile.parentFile.name
104107
val outputDir = File(project.buildDir, "generated/source/pluginManifest/$variantName")
105108

@@ -108,20 +111,22 @@ class ShadowPlugin : Plugin<Project> {
108111
it.dependsOn(processManifestTask)
109112
it.inputs.file(manifestFile)
110113
it.outputs.dir(outputDir).withPropertyName("outputDir")
111-
val packageForR = (project.tasks.getByName("processPluginDebugResources").property("namespace") as Property<String>).get()
114+
115+
val packageForR = getPackageForR(project, variantName)
116+
112117
it.doLast {
113-
generatePluginManifest(manifestFile,
114-
outputDir,
115-
"com.tencent.shadow.core.manifest_parser",
116-
packageForR)
118+
generatePluginManifest(
119+
manifestFile,
120+
outputDir,
121+
"com.tencent.shadow.core.manifest_parser",
122+
packageForR
123+
)
117124
}
118125
}
119126
project.tasks.getByName("compile${variantName.capitalize()}JavaWithJavac").dependsOn(task)
120127

121128
// 把PluginManifest.java添加为源码
122-
appExtension.sourceSets.getByName(variantName).java {
123-
srcDir(outputDir)
124-
}
129+
appExtension.sourceSets.getByName(variantName).java.srcDir(outputDir)
125130
}
126131
}
127132

@@ -176,4 +181,40 @@ class ShadowPlugin : Plugin<Project> {
176181
}
177182
}
178183

184+
companion object {
185+
private fun getManifestFile(processManifestTask: ManifestProcessorTask) =
186+
when (processManifestTask) {
187+
is ProcessMultiApkApplicationManifest -> {
188+
processManifestTask.mainMergedManifest.get().asFile
189+
}
190+
is ProcessApplicationManifest -> {
191+
try {
192+
processManifestTask.mergedManifest.get().asFile
193+
} catch (e: NoSuchMethodError) {
194+
//AGP小于4.1.0
195+
val dir =
196+
processManifestTask.outputs.files.files
197+
.first { it.path.contains("merged_manifests") }
198+
File(dir, ANDROID_MANIFEST_XML)
199+
}
200+
}
201+
else -> throw IllegalStateException("不支持的Task类型:${processManifestTask.javaClass}")
202+
}
203+
204+
private fun getPackageForR(project: Project, variantName: String): String {
205+
val linkApplicationAndroidResourcesTask =
206+
project.tasks.getByName("process${variantName.capitalize()}Resources")
207+
return (when {
208+
209+
linkApplicationAndroidResourcesTask.hasProperty("namespace") -> {
210+
linkApplicationAndroidResourcesTask.property("namespace")
211+
}
212+
linkApplicationAndroidResourcesTask.hasProperty("originalApplicationId") -> {
213+
linkApplicationAndroidResourcesTask.property("originalApplicationId")
214+
}
215+
else -> throw IllegalStateException("不支持的AGP版本")
216+
} as Property<String>).get()
217+
}
218+
}
219+
179220
}

projects/sdk/core/manifest-parser/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
compileKotlin {
1313
kotlinOptions {
1414
jvmTarget = "1.6"
15+
apiVersion = "1.3"// 兼容低版本Gradle和https://youtrack.jetbrains.com/issue/KT-39389
1516
}
1617
}
1718

projects/sdk/core/manifest-parser/src/main/kotlin/com/tencent/shadow/core/manifest_parser/PluginManifestGenerator.kt

-5
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,5 @@ private class PluginManifestBuilder(
352352
else -> throw IllegalArgumentException("不认识$it")
353353
}
354354
}
355-
356-
private fun String.capitalize() =
357-
replaceFirstChar {
358-
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
359-
}
360355
}
361356
}

0 commit comments

Comments
 (0)