forked from Tencent/Shadow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core.gradle-plugin): 将不同AGP版本兼容代码重构到AGPCompat接口中
- Loading branch information
Showing
3 changed files
with
103 additions
and
55 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
projects/sdk/core/gradle-plugin/src/main/kotlin/com/tencent/shadow/core/gradle/AGPCompat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Tencent Shadow available. | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* Licensed under the BSD 3-Clause License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.tencent.shadow.core.gradle | ||
|
||
import com.android.build.gradle.BaseExtension | ||
import com.android.build.gradle.tasks.ManifestProcessorTask | ||
import org.gradle.api.Project | ||
import java.io.File | ||
|
||
/** | ||
* 不同版本AGP的兼容层 | ||
*/ | ||
internal interface AGPCompat { | ||
fun getBaseExtension(project: Project): BaseExtension | ||
fun getManifestFile(processManifestTask: ManifestProcessorTask): File | ||
fun getPackageForR(project: Project, variantName: String): String | ||
} |
63 changes: 63 additions & 0 deletions
63
...ts/sdk/core/gradle-plugin/src/main/kotlin/com/tencent/shadow/core/gradle/AGPCompatImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.tencent.shadow.core.gradle | ||
|
||
import com.android.SdkConstants | ||
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 org.gradle.api.Project | ||
import org.gradle.api.plugins.BasePlugin | ||
import org.gradle.api.provider.Property | ||
import java.io.File | ||
import kotlin.reflect.full.declaredFunctions | ||
import kotlin.reflect.jvm.isAccessible | ||
|
||
internal class AGPCompatImpl : AGPCompat { | ||
override fun getBaseExtension(project: Project): BaseExtension { | ||
val plugin = project.plugins.getPlugin(AppPlugin::class.java) | ||
if (com.android.builder.model.Version.ANDROID_GRADLE_PLUGIN_VERSION == "3.0.0") { | ||
val method = BasePlugin::class.declaredFunctions.first { it.name == "getExtension" } | ||
method.isAccessible = true | ||
return method.call(plugin) as BaseExtension | ||
} else { | ||
return project.extensions.getByName("android") as BaseExtension | ||
} | ||
} | ||
|
||
override 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, SdkConstants.ANDROID_MANIFEST_XML) | ||
} | ||
} | ||
else -> throw IllegalStateException("不支持的Task类型:${processManifestTask.javaClass}") | ||
} | ||
|
||
override 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() | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters