forked from bytedance/ByteX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangzhiqian
committed
Jul 22, 2021
1 parent
32f4207
commit 8fd3aeb
Showing
39 changed files
with
301 additions
and
427 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
71 changes: 4 additions & 67 deletions
71
GradleToolKit/src/main/kotlin/com/ss/android/ugc/bytex/gradletoolkit/BaseVariant.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 |
---|---|---|
@@ -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 |
35 changes: 0 additions & 35 deletions
35
GradleToolKit/src/main/kotlin/com/ss/android/ugc/bytex/gradletoolkit/BoosterBridge.kt
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
GradleToolKit/src/main/kotlin/com/ss/android/ugc/bytex/gradletoolkit/MergeResources.kt
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
...leToolKit/src/main/kotlin/com/ss/android/ugc/bytex/gradletoolkit/MergeSourceSetFolders.kt
This file was deleted.
Oops, something went wrong.
56 changes: 6 additions & 50 deletions
56
GradleToolKit/src/main/kotlin/com/ss/android/ugc/bytex/gradletoolkit/Project.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 |
---|---|---|
@@ -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() | ||
} |
18 changes: 6 additions & 12 deletions
18
GradleToolKit/src/main/kotlin/com/ss/android/ugc/bytex/gradletoolkit/Task.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
Oops, something went wrong.