From 6086f9cfe2a6fe07c4a6242070f3f34839ba44d5 Mon Sep 17 00:00:00 2001 From: Yaser Yu Date: Thu, 22 Oct 2020 20:26:04 +0800 Subject: [PATCH 1/2] adapt to AGP4.0.2 --- .../android/plugin/internal/SigningConfigResolution.kt | 3 ++- samples/app/src/main/res/drawable/ic_abc.png | 0 samples/app/src/main/res/drawable/ic_bcd.png | 0 samples/app/src/main/res/drawable/ic_keep.png | 0 4 files changed, 2 insertions(+), 1 deletion(-) delete mode 100644 samples/app/src/main/res/drawable/ic_abc.png delete mode 100644 samples/app/src/main/res/drawable/ic_bcd.png delete mode 100644 samples/app/src/main/res/drawable/ic_keep.png diff --git a/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt b/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt index 71a46c7..8d488a1 100644 --- a/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt +++ b/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt @@ -74,7 +74,8 @@ private fun getBuildTypesForAGP4009(variantManager: VariantManager): Map<*, *>? val variantInputModelField = variantManager::class.java.getDeclaredField("variantInputModel") variantInputModelField.isAccessible = true val variantInputModel = variantInputModelField.get(variantManager) - val buildTypesField = variantInputModel::class.java.getField("buildTypes") + val buildTypesField = variantInputModel::class.java.getDeclaredField("buildTypes") + buildTypesField.isAccessible = true return buildTypesField.get(variantInputModel) as Map<*, *>? } catch (e: Exception) { null diff --git a/samples/app/src/main/res/drawable/ic_abc.png b/samples/app/src/main/res/drawable/ic_abc.png deleted file mode 100644 index e69de29..0000000 diff --git a/samples/app/src/main/res/drawable/ic_bcd.png b/samples/app/src/main/res/drawable/ic_bcd.png deleted file mode 100644 index e69de29..0000000 diff --git a/samples/app/src/main/res/drawable/ic_keep.png b/samples/app/src/main/res/drawable/ic_keep.png deleted file mode 100644 index e69de29..0000000 From 8af7524bac024a16fd21f471d1d07351a097e1e5 Mon Sep 17 00:00:00 2001 From: Yaser Yu Date: Fri, 23 Oct 2020 00:58:39 +0800 Subject: [PATCH 2/2] Add compatibility with AGP 4.0.* --- .../internal/SigningConfigResolution.kt | 74 ++----------------- 1 file changed, 8 insertions(+), 66 deletions(-) diff --git a/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt b/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt index 8d488a1..8263571 100644 --- a/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt +++ b/plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.kt @@ -1,11 +1,8 @@ package com.bytedance.android.plugin.internal -import com.android.build.gradle.internal.VariantManager import com.android.build.gradle.internal.scope.VariantScope import com.bytedance.android.plugin.model.SigningConfig -import org.gradle.api.GradleException import org.gradle.api.Project -import org.gradle.internal.impldep.org.eclipse.jgit.errors.NotSupportedException import java.io.File /** @@ -15,74 +12,19 @@ import java.io.File internal fun getSigningConfig(project: Project, variantScope: VariantScope): SigningConfig { val agpVersion = getAGPVersion(project) // get signing config - return when { - // AGP3.2+: use VariantScope.getVariantConfiguration.getSigningConfig - agpVersion.startsWith("3.") -> { - getSigningConfigForAGP3(project, variantScope) - } - // AGP4.0+: VariantScope class removed getVariantConfiguration method. - // VariantManager add getBuildTypes method - // Use BuildType.getSigningConfig method to get signingConfig - else -> { - getSigningConfigForAGP4(agpVersion, project, variantScope) - } - } -} - -private fun getSigningConfigForAGP3(project: Project, variantScope: VariantScope): SigningConfig { val variantData = variantScope.variantData - val variantConfiguration = variantData::class.java.getMethod("getVariantConfiguration").invoke(variantData) - val signingConfig = variantConfiguration::class.java.getMethod("getSigningConfig").invoke(variantConfiguration) - return invokeSigningConfig(signingConfig) -} - -private fun getSigningConfigForAGP4(agpVersion: String, project: Project, variantScope: VariantScope): SigningConfig { - val variantManager = getVariantManager(project) - var buildTypes = getBuildTypesForAGPBefore4008(variantManager) - if (buildTypes == null) { - buildTypes = getBuildTypesForAGP4009(variantManager) - } - if (buildTypes == null) { - throw NotSupportedException("AGP $agpVersion is not supported, please Please ask for an issue or pull request.") + val variantDslInfoMethodName = if (agpVersion.startsWith("3.")) { + // AGP3.2+: use VariantScope.getVariantConfiguration.getSigningConfig + "getVariantConfiguration" + } else{ + // AGP4.0+: use VariantScope.getVariantDslInfo.getSigningConfig + "getVariantDslInfo" } - val flavor = variantScope.variantData.name - val buildTypeData = buildTypes[variantScope.variantData.name] - ?: throw GradleException("get buildType failed for $flavor") - val buildType = buildTypeData::class.java.getMethod("getBuildType").invoke(buildTypeData) - val signingConfig = buildType::class.java.getMethod("getSigningConfig").invoke(buildType) + val variantDslInfo = variantData::class.java.getMethod(variantDslInfoMethodName).invoke(variantData) + val signingConfig = variantDslInfo::class.java.getMethod("getSigningConfig").invoke(variantDslInfo) return invokeSigningConfig(signingConfig) } -/** - * Return SigningConfig. - * Range: 4.* to 4.0.0-alpha08 - */ -private fun getBuildTypesForAGPBefore4008(variantManager: VariantManager): Map<*, *>? { - return try { - variantManager::class.java.getMethod("getBuildTypes").invoke(variantManager) as Map<*, *> - } catch (e: Exception) { - return null - } -} - -/** - * Return SigningConfig. - * Range: 4.0.0-alpha09 and after all. - */ -private fun getBuildTypesForAGP4009(variantManager: VariantManager): Map<*, *>? { - return try { - val variantInputModelField = variantManager::class.java.getDeclaredField("variantInputModel") - variantInputModelField.isAccessible = true - val variantInputModel = variantInputModelField.get(variantManager) - val buildTypesField = variantInputModel::class.java.getDeclaredField("buildTypes") - buildTypesField.isAccessible = true - return buildTypesField.get(variantInputModel) as Map<*, *>? - } catch (e: Exception) { - null - } -} - - private fun invokeSigningConfig(any: Any): SigningConfig { val storeFile: File = any::class.java.getMethod("getStoreFile").invoke(any) as File val keyAlias: String = any::class.java.getMethod("getKeyAlias").invoke(any) as String