Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with AGP 4.0.1 and 4.0.2 and optimize some code #39

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -15,73 +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.getField("buildTypes")
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
Expand Down
Empty file.
Empty file.
Empty file.