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

v3.3.0 #227

Merged
merged 5 commits into from
May 8, 2021
Merged
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
Expand Up @@ -8,7 +8,6 @@ import com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedCon
import com.didiglobal.booster.kotlinx.file
import com.didiglobal.booster.kotlinx.separatorsToSystem
import com.didiglobal.booster.kotlinx.touch
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedArtifactResult
import java.io.File
import java.io.PrintWriter
Expand All @@ -26,7 +25,6 @@ class ResolvedArtifactResults(private val variant: BaseVariant) : Collection<Res
.map { variant.getArtifactCollection(RUNTIME_CLASSPATH, ALL, it) }
.map { it.artifacts }
.flatten()
.filter { it.id.componentIdentifier !is ProjectComponentIdentifier }
.distinctBy { it.id.componentIdentifier.displayName }
.sortedBy { it.id.componentIdentifier.displayName }
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ internal class BoosterTransformInvocation(

override val artifacts = this

override val dependencies: Collection<String> by lazy {
ResolvedArtifactResults(variant).map {
it.id.displayName
}
}

override val klassPool: AbstractKlassPool = object : AbstractKlassPool(compileClasspath, transform.bootKlassPool) {}

override val applicationId = delegate.applicationId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.didiglobal.booster.kotlinx
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
import java.util.regex.Pattern

private val PATTERN_JAVA_IDENTIFIER = Pattern.compile("[a-zA-Z_\$][a-zA-Z\\d_\$]*")

fun String.separatorsToUnix(): String {
return if ('/' == File.separatorChar) this else this.replace(File.separatorChar, '/')
Expand All @@ -19,3 +22,5 @@ fun String.md5(): String {
fun String.matches(wildcard: Wildcard): Boolean {
return wildcard.matches(this)
}

fun String.isValidJavaIdentifier(): Boolean = PATTERN_JAVA_IDENTIFIER.matcher(this).matches()
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.didiglobal.booster.kotlinx

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Unit test for String extension
*
* @author johnsonlee
*/
class TextTest {

@Test
fun `test java identifier validation`() {
assertFalse("".isValidJavaIdentifier())
assertFalse("1".isValidJavaIdentifier())
assertFalse(".".isValidJavaIdentifier())
assertTrue("_".isValidJavaIdentifier())
assertTrue("$".isValidJavaIdentifier())
assertFalse("1a".isValidJavaIdentifier())
assertFalse(".a".isValidJavaIdentifier())
assertTrue("_a".isValidJavaIdentifier())
assertTrue("\$a".isValidJavaIdentifier())
assertTrue("ab".isValidJavaIdentifier())
assertTrue("ab1".isValidJavaIdentifier())
assertTrue("ab$".isValidJavaIdentifier())
assertTrue("ab_".isValidJavaIdentifier())
assertFalse("ab.".isValidJavaIdentifier())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.objectweb.asm.Opcodes.GETSTATIC
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.FieldInsnNode
import org.objectweb.asm.tree.LdcInsnNode
import java.io.File
import java.io.PrintWriter
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.Pattern
Expand Down Expand Up @@ -60,8 +59,8 @@ class RInlineTransformer : ClassTransformer {
}

val retainedSymbols: Set<String>
val classpath = context.compileClasspath.map { it.canonicalPath}
if (classpath.any { it.contains(PREFIX_SUPPORT_CONSTRAINT_LAYOUT) || it.contains(PREFIX_JETPACK_CONSTRAINT_LAYOUT) }) {
val deps = context.dependencies
if (deps.any { it.contains(SUPPORT_CONSTRAINT_LAYOUT) || it.contains(JETPACK_CONSTRAINT_LAYOUT) }) {
// Find symbols that should be retained
retainedSymbols = context.findRetainedSymbols()
if (retainedSymbols.isNotEmpty()) {
Expand All @@ -72,7 +71,7 @@ class RInlineTransformer : ClassTransformer {
retainedSymbols = emptySet()
}

logger.println(classpath.joinToString("\n - ", "classpath:\n - ", "\n"))
logger.println(deps.joinToString("\n - ", "dependencies:\n - ", "\n"))
logger.println("$PROPERTY_IGNORES=$ignores\n")

retainedSymbols.ifNotEmpty { symbols ->
Expand Down Expand Up @@ -171,8 +170,7 @@ private val PROPERTY_PREFIX = Build.ARTIFACT.replace('-', '.')

private val PROPERTY_IGNORES = "$PROPERTY_PREFIX.ignores"

private val PREFIX_SUPPORT_CONSTRAINT_LAYOUT = "${File.separatorChar}com.android.support.constraint${File.separatorChar}constraint-layout"

private val PREFIX_JETPACK_CONSTRAINT_LAYOUT = "${File.separator}androidx.constraintlayout${File.separator}constraintlayout"
private const val SUPPORT_CONSTRAINT_LAYOUT = "com.android.support.constraint:constraint-layout:"
private const val JETPACK_CONSTRAINT_LAYOUT = "androidx.constraintlayout:constraintlayout:"

private val logger_ = Logging.getLogger(RInlineTransformer::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.didiglobal.booster.aapt2.MAGIC
import com.didiglobal.booster.aapt2.RES_FILE
import com.didiglobal.booster.aapt2.Resources
import com.didiglobal.booster.aapt2.ResourcesInternal
import com.didiglobal.booster.kotlinx.isValidJavaIdentifier
import com.didiglobal.booster.kotlinx.stackTraceAsString
import org.gradle.api.logging.Logging
import java.io.File
Expand Down Expand Up @@ -112,10 +113,21 @@ internal fun Resources.XmlNode.findAllRetainedSymbols(): Collection<String> {
}.attributeList?.forEach { attr ->
when (attr.name) {
"constraint_referenced_ids" -> addAll(attr.value.split(PATTERN_COMMA))
"layout_constraintHorizontal_bias",
"layout_constraintVertical_bias",
"layout_constraintCircleRadius",
"layout_constraintCircleAngle",
"layout_constraintDimensionRatio",
"layout_constraintWidth_default",
"layout_constraintHeight_default",
"layout_constraintWidth_percent",
"layout_constraintHeight_percent",
"layout_constraintHorizontal_chainStyle",
"layout_constraintVertical_chainStyle",
"layout_constraintHorizontal_weight",
"layout_constraintVertical_weight" -> Unit // just ignore
else -> if (attr.name.startsWith("layout_constraint")) {
addAll(attr.value.split(PATTERN_COMMA).filter {
it != "parent"
}.map {
addAll(attr.value.split(PATTERN_COMMA).filter(::isValidSymbol).map {
it.substringAfter('/')
})
}
Expand All @@ -125,6 +137,8 @@ internal fun Resources.XmlNode.findAllRetainedSymbols(): Collection<String> {
}
}

internal fun isValidSymbol(token: String) = token.isNotEmpty() && "parent" != token && token.isValidJavaIdentifier()

private val logger = Logging.getLogger(RetainedSymbolCollector::class.java)

private val PATTERN_COMMA = Pattern.compile("\\s*,\\s*")
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.didiglobal.booster.transform.r.inline

import java.io.File
import java.util.regex.Pattern
import kotlin.test.Test
import kotlin.test.assertFalse
Expand All @@ -23,4 +22,12 @@ class RegexTest {
}
}

@Test
fun `valid symbol`() {
assertTrue(isValidSymbol("H"))
assertTrue(isValidSymbol("icon"))
assertTrue(isValidSymbol("ic_app"))
assertFalse(isValidSymbol("4:1"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ abstract class AbstractTransformContext(

override val artifacts = object : ArtifactManager {}

override val dependencies: Collection<String> by lazy {
compileClasspath.map { it.canonicalPath }
}

override val klassPool = object : AbstractKlassPool(runtimeClasspath, bootKlassPool) {}

override val originalApplicationId = applicationId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ interface TransformContext {
*/
val artifacts: ArtifactManager

/**
* The original dependencies, may be same to [compileClasspath]
*/
val dependencies: Collection<String>

/**
* The class pool
*/
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ allprojects { project ->
apply plugin: 'de.marcphilipp.nexus-publish'

group = 'com.didiglobal.booster'
version = '3.2.0'
version = '3.3.0-rc1'

repositories {
mavenLocal()
Expand Down