Skip to content

Commit

Permalink
merge internal
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhiqian committed Jul 22, 2021
1 parent 32f4207 commit 8fd3aeb
Show file tree
Hide file tree
Showing 39 changed files with 301 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* Created by tanlehua on 2019-04-29.
*/
public enum Artifact {
AAR, ALL_CLASSES/*project*/, APK, JAR, PROCESSED_JAR, CLASSES/*from jar classes*/, JAVAC, MERGED_ASSETS, MERGED_RES, MERGED_MANIFESTS, MERGED_MANIFESTS_WITH_FEATURES, PROCESSED_RES, SYMBOL_LIST, SYMBOL_LIST_WITH_PACKAGE_NAME,
AAR, ALL_CLASSES/*project*/, APK, JAR, @Deprecated PROCESSED_JAR, CLASSES/*from jar classes*/, JAVAC, MERGED_ASSETS, MERGED_RES, MERGED_MANIFESTS, MERGED_MANIFESTS_WITH_FEATURES, PROCESSED_RES, SYMBOL_LIST, SYMBOL_LIST_WITH_PACKAGE_NAME,
RAW_RESOURCE_SETS, RAW_ASSET_SETS;
}
3 changes: 2 additions & 1 deletion GradleToolKit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ dependencies {
compileOnly "com.android.tools.build:gradle:$gradle_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile project(':GradleEnvApi')
compile("com.bytedance.android.build:gradle-compat-api:$gradle_compat_version")
runtime("com.bytedance.android.build:gradle-compat-impl:$gradle_compat_version")

compile('com.google.auto.service:auto-service:1.0-rc4') {
exclude module: 'guava'
}
implementation "com.didiglobal.booster:booster-android-gradle-api:3.1.0"
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,18 @@
package com.ss.android.ugc.bytex.gradletoolkit

import com.android.build.api.artifact.ArtifactType
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.api.BaseVariantImpl
import com.android.build.gradle.internal.publishing.AndroidArtifacts
import com.android.build.gradle.internal.scope.VariantScope
import com.android.build.gradle.internal.variant.BaseVariantData
import com.android.build.gradle.tasks.MergeResources
import com.bytedance.gradle.compat.extension.compatVariant
import org.gradle.api.artifacts.ArtifactCollection
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import java.io.File

val BaseVariant.scope: VariantScope
get() = if (ANDROID_GRADLE_PLUGIN_VERSION.major < 4 || (ANDROID_GRADLE_PLUGIN_VERSION.major == 4 && ANDROID_GRADLE_PLUGIN_VERSION.minor == 0)) {
this.scope40
} else {
this.scope41
}
private val BaseVariant.scope40: VariantScope
get() = ReflectionUtils.callMethod<BaseVariantData>(this, javaClass, "getVariantData", arrayOf(), arrayOf()).scope

private val BaseVariant.scope41: VariantScope
get() = ReflectionUtils.getField<Any>(this, BaseVariantImpl::class.java, "componentProperties").let {
ReflectionUtils.callMethod<BaseVariantData>(it, it.javaClass, "getVariantScope", arrayOf(), arrayOf()) as VariantScope
}
get() = compatVariant.variantScope.originScope()

fun BaseVariant.getArtifactCollection(configType: AndroidArtifacts.ConsumedConfigType, artifactScope: AndroidArtifacts.ArtifactScope, artifactType: AndroidArtifacts.ArtifactType): ArtifactCollection {
return if (ANDROID_GRADLE_PLUGIN_VERSION.major < 4 || (ANDROID_GRADLE_PLUGIN_VERSION.major == 4 && ANDROID_GRADLE_PLUGIN_VERSION.minor == 0)) {
this.getArtifactCollection40(configType, artifactScope, artifactType)
} else {
this.getArtifactCollection41(configType, artifactScope, artifactType)
}
}

private fun BaseVariant.getArtifactCollection40(configType: AndroidArtifacts.ConsumedConfigType, artifactScope: AndroidArtifacts.ArtifactScope, artifactType: AndroidArtifacts.ArtifactType): ArtifactCollection {
return scope.getArtifactCollection(configType, artifactScope, artifactType)
}

private fun BaseVariant.getArtifactCollection41(configType: AndroidArtifacts.ConsumedConfigType, artifactScope: AndroidArtifacts.ArtifactScope, artifactType: AndroidArtifacts.ArtifactType): ArtifactCollection {
return ReflectionUtils.getField<Any>(this, BaseVariantImpl::class.java, "componentProperties").let {
ReflectionUtils.callPublicMethod<Any>(it, it.javaClass, "getVariantDependencies", arrayOf(), arrayOf()).let {
ReflectionUtils.callPublicMethod<ArtifactCollection>(it, it.javaClass, "getArtifactCollection", arrayOf(
AndroidArtifacts.ConsumedConfigType::class.java, AndroidArtifacts.ArtifactScope::class.java, AndroidArtifacts.ArtifactType::class.java
), arrayOf(configType, artifactScope, artifactType))
}
}
}

fun BaseVariant.getArtifactFiles(artifactType: ArtifactType): Collection<File> {
return if (ANDROID_GRADLE_PLUGIN_VERSION.major > 3 || (ANDROID_GRADLE_PLUGIN_VERSION.major == 3 && ANDROID_GRADLE_PLUGIN_VERSION.minor >= 5)) {
this.getArtifactFiles35_(artifactType)
} else {
this.getArtifactFiles30_(artifactType)
}
}

private fun BaseVariant.getArtifactFiles35_(artifactType: ArtifactType): Collection<File> {
return scope.artifacts.getFinalProducts<Directory>(artifactType).orNull?.map { it.asFile }?.toSet()
?: emptyList()
}

private fun BaseVariant.getArtifactFiles30_(artifactType: ArtifactType): Collection<File> {
return scope.artifacts.getArtifactFiles(artifactType).files
return compatVariant.rawArtifacts.getArtifactCollection(configType, artifactScope, artifactType)
}

val BaseVariant.blameLogOutputFolder: File
get() = if (ANDROID_GRADLE_PLUGIN_VERSION.major < 4 || (ANDROID_GRADLE_PLUGIN_VERSION.major == 4 && ANDROID_GRADLE_PLUGIN_VERSION.minor == 0)) {
this.blameLogOutputFolder40
} else {
this.blameLogOutputFolder41
}
private val BaseVariant.blameLogOutputFolder40: File
get() = scope.resourceBlameLogDir


private val BaseVariant.blameLogOutputFolder41: File
get() = mergeResources.let {
ReflectionUtils.callMethod<DirectoryProperty>(it, MergeResources::class.java, "getBlameLogOutputFolder", arrayOf(), arrayOf()).asFile.get()
}
get() = compatVariant.blameLogOutputFolder

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,61 +1,17 @@
package com.ss.android.ugc.bytex.gradletoolkit

import com.android.build.gradle.internal.VariantManager



import com.android.build.gradle.internal.scope.VariantScope
import com.android.builder.model.Version
import com.android.repository.Revision
import com.bytedance.gradle.compat.AGP

import org.gradle.api.Project

//todo:fix me
val revision = Revision.parseRevision(Version.ANDROID_GRADLE_PLUGIN_VERSION)
fun Project.findVariantScope(variantName: String): VariantScope? {
return findVariantManager().findVariantScope(variantName)
}


fun Project.findVariantManager(): VariantManager {
return if (revision.major > 3 || revision.minor >= 6) {
findVariantManager36()
} else {
findVariantManager35()
}
}

private fun Project.findVariantManager35(): VariantManager {
return project.plugins.findPlugin(com.android.build.gradle.AppPlugin::class.java)!!.variantManager
}

private fun Project.findVariantManager36(): VariantManager {
return project.plugins.findPlugin("com.android.internal.application")!!.let {
it.javaClass.getMethod("getVariantManager").invoke(it) as VariantManager
}
}

fun VariantManager.findVariantScope(variantName: String): VariantScope? {
return if (revision.major < 4) {
findVariantScope3X(variantName)
} else if (revision.minor == 0) {
findVariantScope40(variantName)
} else {
findVariantScope41(variantName)
}
}

private fun VariantManager.findVariantScope3X(variantName: String): VariantScope? {
return variantScopes.firstOrNull { it.fullVariantName == variantName }
}

private fun VariantManager.findVariantScope40(variantName: String): VariantScope? {
return variantScopes.firstOrNull { it::class.java.getMethod("getName").invoke(it) == variantName }
}


private fun VariantManager.findVariantScope41(variantName: String): VariantScope? {
for (info in this.javaClass.getMethod("getMainComponents").invoke(this) as List<Any>) {
val properties = info.javaClass.getMethod("getProperties").invoke(info)
if (properties.javaClass.getMethod("getName").invoke(properties) == variantName) {
return properties.javaClass.getMethod("getVariantScope").invoke(properties) as VariantScope
}
}
return null
return AGP.withAndroidPlugin(this).variantManager.variants.firstOrNull { it.name==variantName }?.originScope()
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package com.ss.android.ugc.bytex.gradletoolkit

import com.android.build.gradle.AppExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension

import com.android.build.gradle.api.BaseVariant
import com.bytedance.gradle.compat.AGP
import org.gradle.api.Task

val Task.variant: BaseVariant?
get() = this.let {
val android = project.extensions.findByName("android") as? BaseExtension ?: return@let null
val allVariant = if (android is AppExtension) {
android.applicationVariants.map { it as BaseVariant }
} else if (android is LibraryExtension) {
android.libraryVariants.map { it as BaseVariant }
} else {
return@let null
}
if(!AGP.isAndroidModule(project)) return@let null
val android = AGP.withAndroidExtension(project)
val allVariant = android.variants
var matchedVariant: BaseVariant? = null
for (variant in allVariant) {
if (it.name.endsWith(variant.name.capitalize())) {
if (matchedVariant == null || matchedVariant.name.length < variant.name.length) {
matchedVariant = variant
matchedVariant = variant.raw()
}
}
}
Expand Down
Loading

0 comments on commit 8fd3aeb

Please sign in to comment.