Skip to content

Commit

Permalink
fix(core.gradle-plugin): 支持从低版本AGP中获取Manifest
Browse files Browse the repository at this point in the history
fixup! 8a99c4f

fix #747
  • Loading branch information
shifujun committed Jan 5, 2022
1 parent 520aa3f commit b040630
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

package com.tencent.shadow.core.gradle

import com.android.SdkConstants.ANDROID_MANIFEST_XML
import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.tasks.ManifestProcessorTask
import com.android.build.gradle.tasks.ProcessApplicationManifest
import com.android.build.gradle.tasks.ProcessMultiApkApplicationManifest
import com.tencent.shadow.core.gradle.extensions.PackagePluginExtension
import com.tencent.shadow.core.manifest_parser.generatePluginManifest
Expand Down Expand Up @@ -99,7 +102,7 @@ class ShadowPlugin : Plugin<Project> {
}.forEach { pluginVariant ->
val output = pluginVariant.outputs.first()
val processManifestTask = output.processManifestProvider.get()
val manifestFile = (processManifestTask as ProcessMultiApkApplicationManifest).mainMergedManifest.get().asFile
val manifestFile = getManifestFile(processManifestTask)
val variantName = manifestFile.parentFile.name
val outputDir = File(project.buildDir, "generated/source/pluginManifest/$variantName")

Expand All @@ -108,20 +111,22 @@ class ShadowPlugin : Plugin<Project> {
it.dependsOn(processManifestTask)
it.inputs.file(manifestFile)
it.outputs.dir(outputDir).withPropertyName("outputDir")
val packageForR = (project.tasks.getByName("processPluginDebugResources").property("namespace") as Property<String>).get()

val packageForR = getPackageForR(project, variantName)

it.doLast {
generatePluginManifest(manifestFile,
outputDir,
"com.tencent.shadow.core.manifest_parser",
packageForR)
generatePluginManifest(
manifestFile,
outputDir,
"com.tencent.shadow.core.manifest_parser",
packageForR
)
}
}
project.tasks.getByName("compile${variantName.capitalize()}JavaWithJavac").dependsOn(task)

// 把PluginManifest.java添加为源码
appExtension.sourceSets.getByName(variantName).java {
srcDir(outputDir)
}
appExtension.sourceSets.getByName(variantName).java.srcDir(outputDir)
}
}

Expand Down Expand Up @@ -176,4 +181,40 @@ class ShadowPlugin : Plugin<Project> {
}
}

companion object {
private fun getManifestFile(processManifestTask: ManifestProcessorTask) =
when (processManifestTask) {
is ProcessMultiApkApplicationManifest -> {
processManifestTask.mainMergedManifest.get().asFile
}
is ProcessApplicationManifest -> {
try {
processManifestTask.mergedManifest.get().asFile
} catch (e: NoSuchMethodError) {
//AGP小于4.1.0
val dir =
processManifestTask.outputs.files.files
.first { it.path.contains("merged_manifests") }
File(dir, ANDROID_MANIFEST_XML)
}
}
else -> throw IllegalStateException("不支持的Task类型:${processManifestTask.javaClass}")
}

private fun getPackageForR(project: Project, variantName: String): String {
val linkApplicationAndroidResourcesTask =
project.tasks.getByName("process${variantName.capitalize()}Resources")
return (when {

linkApplicationAndroidResourcesTask.hasProperty("namespace") -> {
linkApplicationAndroidResourcesTask.property("namespace")
}
linkApplicationAndroidResourcesTask.hasProperty("originalApplicationId") -> {
linkApplicationAndroidResourcesTask.property("originalApplicationId")
}
else -> throw IllegalStateException("不支持的AGP版本")
} as Property<String>).get()
}
}

}
1 change: 1 addition & 0 deletions projects/sdk/core/manifest-parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
compileKotlin {
kotlinOptions {
jvmTarget = "1.6"
apiVersion = "1.3"// 兼容低版本Gradle和https://youtrack.jetbrains.com/issue/KT-39389
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,5 @@ private class PluginManifestBuilder(
else -> throw IllegalArgumentException("不认识$it")
}
}

private fun String.capitalize() =
replaceFirstChar {
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
}
}
}

0 comments on commit b040630

Please sign in to comment.