Skip to content

Commit

Permalink
Merge pull request #109 from bytedance/yangzhiqian/master
Browse files Browse the repository at this point in the history
Upgrade asm libs from 6.2.1 to 9.2;Replace booster-gradle-api with gradle-compat-api;Check unexpected bytecode modifications;Fix memory leak caused by gradle;New whiteList feature in access-inline-plugin
  • Loading branch information
yangzhiqian authored Jul 22, 2021
2 parents acd9827 + 16fd1d1 commit 90979d0
Show file tree
Hide file tree
Showing 42 changed files with 313 additions and 430 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Change Log
### Version 0.3.0
- Upgrade asm libs from 6.2.1 to 9.2(Notes:ASM_API which is passed to asm visitors has not been changed,ASM6 by default.You could changed it by append 'bytex.ASM_API=\[ASM4、ASM5、ASM6、ASM7、ASM8、ASM9\]' in gradle.properties)
- Replace booster-gradle-api with gradle-compat-api.Both of them are perfect AGP compatible libraries.
`booster-gradle-api` is developed and maintained by [booster](https://github.com/didi/booster),which was used by ByteX for a long time .
`gradle-compat-api` is developed and maintained by bytedance,witch has been running well within the company for several months.
- Check unexpected bytecode modifications by plugins during the traverse phase in release build.(Notes:Plugins should only perceive the inputs in the traverse phase, and there should be no modifications)
- Fix memory leak caused by gradle
- New whiteList feature in access-inline-plugin.

### Version 0.2.8
- Replace hookTransform with HookInjector;
- Add proguard mapping parser&retracer;
Expand Down
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 90979d0

Please sign in to comment.