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.
fix(core.gradle-plugin): 修复AGP 3.1.0及更高版本的兼容性
主要以黑盒自动化测试驱动本次代码修复。详见: projects/test/gradle-plugin-agp-compat-test/README.md AGPCompatImpl更理想的方式是按AGP版本拆分成多个文件, 但考虑到AGP版本号获取的是一个字符串,而且业务有可能使用一些beta等版本的AGP, 版本号匹配风险比较大。所以目前实现方式是try-catch的方式。 移除pom中对com.android.tools.build依赖的声明。 这个声明会把AGP依赖带入构建项目中,可能会导致项目声明的AGP版本不生效。 fix Tencent#757
- Loading branch information
Showing
20 changed files
with
658 additions
and
62 deletions.
There are no files selected for viewing
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,37 @@ | ||
name: gradle-plugin-agp-compat-test | ||
|
||
on: | ||
pull_request: | ||
branches: [ master ] | ||
paths: | ||
- 'projects/sdk/core/gradle-plugin' | ||
|
||
jobs: | ||
gradle-plugin-agp-compat-test: | ||
runs-on: ubuntu-latest | ||
env: | ||
DISABLE_TENCENT_MAVEN_MIRROR: true | ||
steps: | ||
- name: Inject slug/short variables | ||
uses: rlespinasse/github-slug-action@v3.x | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
- uses: actions/cache@v2 | ||
with: | ||
path: | | ||
~/.gradle/caches | ||
~/.gradle/wrapper | ||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
${{ runner.os }}-gradle- | ||
- name: revert gradle wrapper mirror setting | ||
run: echo 'distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip' > gradle/wrapper/gradle-wrapper.properties | ||
- name: revert gradle wrapper mirror setting | ||
working-directory: projects/test/gradle-plugin-agp-compat-test | ||
run: echo 'distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip' > gradle/wrapper/gradle-wrapper.properties | ||
- name: test AGP compatibility when core.gradle-plugin changed | ||
working-directory: projects/test/gradle-plugin-agp-compat-test | ||
run: ./test.sh | ||
|
||
- name: stop gradle deamon for actions/cache | ||
run: ./gradlew --stop |
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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
/* | ||
* 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.api.BaseVariantOutput | ||
import com.android.build.gradle.internal.dsl.ProductFlavor | ||
import org.gradle.api.Project | ||
import org.gradle.api.Task | ||
import java.io.File | ||
|
||
/** | ||
* 不同版本AGP的兼容层 | ||
*/ | ||
internal interface AGPCompat { | ||
fun getManifestFile(processManifestTask: Task): File | ||
fun getPackageForR(project: Project, variantName: String): String | ||
fun addFlavorDimension(baseExtension: BaseExtension, dimensionName: String) | ||
fun setProductFlavorDefault(productFlavor: ProductFlavor, isDefault: Boolean) | ||
fun getProcessManifestTask(output: BaseVariantOutput): Task | ||
} |
103 changes: 103 additions & 0 deletions
103
...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,103 @@ | ||
package com.tencent.shadow.core.gradle | ||
|
||
import com.android.SdkConstants | ||
import com.android.build.gradle.BaseExtension | ||
import com.android.build.gradle.api.BaseVariantOutput | ||
import com.android.build.gradle.internal.dsl.ProductFlavor | ||
import com.android.build.gradle.tasks.ProcessApplicationManifest | ||
import com.android.build.gradle.tasks.ProcessMultiApkApplicationManifest | ||
import org.gradle.api.Project | ||
import org.gradle.api.Task | ||
import org.gradle.api.provider.Property | ||
import java.io.File | ||
|
||
internal class AGPCompatImpl : AGPCompat { | ||
|
||
override fun getProcessManifestTask(output: BaseVariantOutput): Task = | ||
try { | ||
output.processManifestProvider.get() | ||
} catch (e: NoSuchMethodError) { | ||
output.processManifest | ||
} | ||
|
||
override fun getManifestFile(processManifestTask: Task) = | ||
when (processManifestTask.javaClass.superclass.simpleName) { | ||
"ProcessMultiApkApplicationManifest" -> { | ||
(processManifestTask as ProcessMultiApkApplicationManifest) | ||
.mainMergedManifest.get().asFile | ||
} | ||
"ProcessApplicationManifest" -> { | ||
try { | ||
(processManifestTask as ProcessApplicationManifest) | ||
.mergedManifest.get().asFile | ||
} catch (e: NoSuchMethodError) { | ||
//AGP小于4.1.0 | ||
val dir = | ||
processManifestTask.outputs.files.files | ||
.first { it.parentFile.name == "merged_manifests" } | ||
File(dir, SdkConstants.ANDROID_MANIFEST_XML) | ||
} | ||
} | ||
"MergeManifests" -> { | ||
val dir = try {// AGP 3.2.0 | ||
processManifestTask.outputs.files.files | ||
.first { it.parentFile.parentFile.parentFile.name == "merged_manifests" } | ||
} catch (e: NoSuchElementException) { | ||
// AGP 3.1.0 | ||
processManifestTask.outputs.files.files | ||
.first { it.path.contains("intermediates${File.separator}manifests${File.separator}full${File.separator}") } | ||
} | ||
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 getStringFromProperty( | ||
when { | ||
linkApplicationAndroidResourcesTask.hasProperty("namespace") -> { | ||
linkApplicationAndroidResourcesTask.property("namespace") | ||
} | ||
linkApplicationAndroidResourcesTask.hasProperty("originalApplicationId") -> { | ||
linkApplicationAndroidResourcesTask.property("originalApplicationId") | ||
} | ||
linkApplicationAndroidResourcesTask.hasProperty("packageName") -> { | ||
linkApplicationAndroidResourcesTask.property("packageName") | ||
} | ||
else -> throw IllegalStateException("不支持的AGP版本") | ||
} | ||
) | ||
} | ||
|
||
override fun addFlavorDimension(baseExtension: BaseExtension, dimensionName: String) { | ||
val flavorDimensionList = baseExtension.flavorDimensionList | ||
as MutableList<String>? // AGP 3.6.0版本可能返回null | ||
if (flavorDimensionList != null) { | ||
flavorDimensionList.add(dimensionName) | ||
} else { | ||
baseExtension.flavorDimensions(dimensionName) | ||
} | ||
} | ||
|
||
override fun setProductFlavorDefault(productFlavor: ProductFlavor, isDefault: Boolean) { | ||
try { | ||
productFlavor.isDefault = isDefault | ||
} catch (ignored: NoSuchMethodError) { | ||
// AGP 3.6.0版本没有这个方法,就不设置了。 | ||
// 设置Default主要是为了IDE中的Build Variants上下文自动选择时不要选成插件, | ||
// 以便在IDE直接运行插件apk模块时运行Normal版本 | ||
} | ||
} | ||
|
||
companion object { | ||
fun getStringFromProperty(x: Any?): String { | ||
return when (x) { | ||
is String -> x | ||
is Property<*> -> x.get() as String | ||
else -> throw Error("不支持的AGP版本") | ||
} | ||
} | ||
} | ||
} |
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
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,10 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
.idea | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.gradletasknamecache | ||
|
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,11 @@ | ||
## core.gradle-plugin模块的AGP各版本黑盒测试 | ||
|
||
准备一个桩工程`stub-project`,通过命令行参数控制其AGP版本和Shadow版本。 | ||
|
||
自动化测试脚本: `test.sh`。其中先编译Shadow,发布到本地Maven,然后用这个Shadow版本进行测试。 | ||
|
||
注意脚本会echo出执行的命令,如果遇到测试失败,可复制命令手工重新执行。 | ||
|
||
### 确定实际使用的AGP版本: | ||
|
||
查看`stub-project/build/intermediates/app_metadata/pluginDebug/app-metadata.properties` |
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 @@ | ||
// Top-level build file where you can add configuration options common to all sub-projects/modules. |
16 changes: 16 additions & 0 deletions
16
projects/test/gradle-plugin-agp-compat-test/gradle.properties
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,16 @@ | ||
# Project-wide Gradle settings. | ||
# IDE (e.g. Android Studio) users: | ||
# Gradle settings configured through the IDE *will override* | ||
# any settings specified in this file. | ||
# For more details on how to configure your build environment visit | ||
# http://www.gradle.org/docs/current/userguide/build_environment.html | ||
# Specifies the JVM arguments used for the daemon process. | ||
# The setting is particularly useful for tweaking memory settings. | ||
org.gradle.jvmargs=-Xmx4096m | ||
# When configured, Gradle will run in incubating parallel mode. | ||
# This option should only be used with decoupled projects. More details, visit | ||
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects | ||
# org.gradle.parallel=true | ||
android.useAndroidX=true | ||
org.gradle.caching=false | ||
|
Binary file added
BIN
+57.8 KB
projects/test/gradle-plugin-agp-compat-test/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
projects/test/gradle-plugin-agp-compat-test/gradle/wrapper/gradle-wrapper.properties
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,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
#distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip | ||
distributionUrl=https\://mirrors.tencent.com/gradle/gradle-7.0.2-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.