-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from bytedance/bugfix/agp-4.0.0-alpha07/gradle…
…-6.1-milestone-2 release 0.1.2, Compatible with `AGP-4.0.0-alpha07`
- Loading branch information
Showing
12 changed files
with
215 additions
and
23 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
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
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
37 changes: 37 additions & 0 deletions
37
plugin/src/main/kotlin/com/bytedance/android/plugin/internal/BundleResolution.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 @@ | ||
package com.bytedance.android.plugin.internal | ||
|
||
import com.android.build.gradle.internal.scope.VariantScope | ||
import org.gradle.api.Project | ||
import java.io.File | ||
import java.nio.file.Path | ||
|
||
/** | ||
* Created by YangJing on 2020/01/07 . | ||
* Email: [email protected] | ||
*/ | ||
internal fun getBundleFilePath(project: Project, variantScope: VariantScope): Path { | ||
val agpVersion = getAGPVersion(project) | ||
val flavor = variantScope.variantData.name | ||
return if (agpVersion.startsWith("3.")) { | ||
getBundleFileForAGP3(project, flavor).toPath() | ||
} else { | ||
getBundleFileForAGP4(project, flavor).toPath() | ||
} | ||
} | ||
|
||
fun getBundleFileForAGP3(project: Project, flavor: String): File { | ||
// AGP-3.2.1: package{}Bundle task is com.android.build.gradle.internal.tasks.BundleTask | ||
// AGP-3.4.1: package{}Bundle task is com.android.build.gradle.internal.tasks.PackageBundleTask | ||
val bundleTaskName = "package${flavor.capitalize()}Bundle" | ||
val bundleTask = project.tasks.getByName(bundleTaskName) | ||
return File(bundleTask.property("bundleLocation") as File, bundleTask.property("fileName") as String) | ||
} | ||
|
||
fun getBundleFileForAGP4(project: Project, flavor: String): File { | ||
// AGP-4.0.0-alpha07: use FinalizeBundleTask to sign bundle file | ||
val finalizeBundleTask = project.tasks.getByName("sign${flavor.capitalize()}Bundle") | ||
// FinalizeBundleTask.finalBundleFile is the final bundle path | ||
val bundleFile = finalizeBundleTask.property("finalBundleFile") | ||
val regularFile = bundleFile!!::class.java.getMethod("get").invoke(bundleFile) | ||
return regularFile::class.java.getMethod("getAsFile").invoke(regularFile) as File | ||
} |
66 changes: 66 additions & 0 deletions
66
plugin/src/main/kotlin/com/bytedance/android/plugin/internal/ProjectResolution.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,66 @@ | ||
package com.bytedance.android.plugin.internal | ||
|
||
import com.android.build.gradle.internal.VariantManager | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.initialization.dsl.ScriptHandler | ||
import org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier | ||
|
||
/** | ||
* Created by YangJing on 2020/01/07 . | ||
* Email: [email protected] | ||
*/ | ||
internal fun getVariantManager(project: Project): VariantManager { | ||
val appPlugin: Plugin<Any>? = when { | ||
// AGP-4.0.0-alpha07: move all methods to com.android.internal.application | ||
project.plugins.hasPlugin("com.android.internal.application") -> { | ||
project.plugins.getPlugin("com.android.internal.application") | ||
} | ||
project.plugins.hasPlugin("com.android.application") -> { | ||
project.plugins.getPlugin("com.android.application") | ||
} | ||
else -> { | ||
throw GradleException("Unexpected AppPlugin") | ||
} | ||
} | ||
return getVariantManagerFromAppPlugin(appPlugin) | ||
?: throw GradleException("get VariantManager failed") | ||
} | ||
|
||
private fun getVariantManagerFromAppPlugin(appPlugin: Any?): VariantManager? { | ||
return if (appPlugin == null) return null else try { | ||
for (method in appPlugin::class.java.methods) { | ||
if (method.name == "getVariantManager") { | ||
return method.invoke(appPlugin) as VariantManager? | ||
} | ||
} | ||
for (method in appPlugin::class.java.declaredMethods) { | ||
if (method.name == "getVariantManager") { | ||
return method.invoke(appPlugin) as VariantManager? | ||
} | ||
} | ||
return null | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
|
||
internal fun getAGPVersion(project: Project): String { | ||
var agpVersion: String? = null | ||
for (artifact in project.rootProject.buildscript.configurations.getByName(ScriptHandler.CLASSPATH_CONFIGURATION) | ||
.resolvedConfiguration.resolvedArtifacts) { | ||
val identifier = artifact.id.componentIdentifier | ||
if (identifier is DefaultModuleComponentIdentifier) { | ||
if (identifier.group == "com.android.tools.build") { | ||
if (identifier.module == "gradle") { | ||
agpVersion = identifier.version | ||
} | ||
} | ||
} | ||
} | ||
if (agpVersion == null) { | ||
throw GradleException("get AGP version failed") | ||
} | ||
return agpVersion | ||
} |
48 changes: 48 additions & 0 deletions
48
plugin/src/main/kotlin/com/bytedance/android/plugin/internal/SigningConfigResolution.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,48 @@ | ||
package com.bytedance.android.plugin.internal | ||
|
||
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 java.io.File | ||
|
||
/** | ||
* Created by YangJing on 2020/01/06 . | ||
* Email: [email protected] | ||
*/ | ||
internal fun getSigningConfig(project: Project, variantScope: VariantScope): SigningConfig { | ||
val agpVersion = getAGPVersion(project) | ||
return if (agpVersion.startsWith("3.")) { | ||
getSigningConfigForAGP3(project, variantScope) | ||
} else { | ||
getSigningConfigForAGP4(project, variantScope) | ||
} | ||
} | ||
|
||
private fun getSigningConfigForAGP4(project: Project, variantScope: VariantScope): SigningConfig { | ||
val variantManager = getVariantManager(project) | ||
val buildTypes = variantManager::class.java.getMethod("getBuildTypes").invoke(variantManager) as Map<*, *> | ||
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) | ||
return invokeSigningConfig(signingConfig) | ||
} | ||
|
||
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 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 | ||
val keyPassword: String = any::class.java.getMethod("getKeyPassword").invoke(any) as String | ||
val storePassword: String = any::class.java.getMethod("getStorePassword").invoke(any) as String | ||
return SigningConfig( | ||
storeFile, storePassword, keyAlias, keyPassword | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
plugin/src/main/kotlin/com/bytedance/android/plugin/model/SigningConfig.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,14 @@ | ||
package com.bytedance.android.plugin.model | ||
|
||
import java.io.File | ||
|
||
/** | ||
* Created by YangJing on 2020/01/07 . | ||
* Email: [email protected] | ||
*/ | ||
data class SigningConfig( | ||
val storeFile: File?, | ||
val storePassword: String?, | ||
val keyAlias: String?, | ||
val keyPassword: String? | ||
) |
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
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