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

fix gradle7.0+ transform #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ org.gradle.caching=false
agp_version=4.1.0
# kotlin sdk version
kotlin_version=1.3.72
rhea_gradle_plugin_vesion=2.0.1
rhea_gradle_plugin_vesion=2.0.3-rc03
# java sdk version
JAVA_VERSION=1.8
asm_version=6.2.1
android.enableR8=false
precise_instrumentation_enable=false
#pom settings
POM_GROUP_ID=com.bytedance.btrace
POM_VERSION_NAME=2.0.3-rc02
POM_VERSION_NAME=2.0.3-rc03
POM_DESCRIPTION=btrace is a high performance Android trace tool which is based on Perfetto.
POM_URL=https://github.com/bytedance/btrace
POM_INCEPTION_YEAR=2020
Expand Down
24 changes: 23 additions & 1 deletion gradle/android-artifacts.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ task androidJavadocs(type: Javadoc) {
if (variant.name == 'release') {
try {
owner.classpath += variant.getJavaCompileProvider().get().classpath
} catch(Exception e) {
} catch (Exception e) {
owner.classpath += variant.getJavaCompile().classpath
}
}
Expand All @@ -38,3 +38,25 @@ artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}

afterEvaluate {
publishing {
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url = project.rootProject.buildDir.path + "/repo"
}
}
publications {
paidRelease(MavenPublication) {
// The following applies a component to this publication
// which results in publishing an app bundle.
from components.release

groupId = POM_GROUP_ID
artifactId = project.name
version = POM_VERSION_NAME
}
}
}
}
24 changes: 24 additions & 0 deletions gradle/java-artifacts.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,27 @@ artifacts {
archives javadocJar
archives sourcesJar
}
jar {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
afterEvaluate {
publishing {
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url = project.rootProject.buildDir.path + "/repo"
}
}
publications {
paidRelease(MavenPublication) {
// The following applies a component to this publication
// which results in publishing an app bundle.
from components.java

groupId = POM_GROUP_ID
artifactId = project.name
version = POM_VERSION_NAME
}
}
}
}
8 changes: 7 additions & 1 deletion rhea-build/rhea-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: "java-library"
apply plugin: 'java'
apply plugin: 'kotlin'

version POM_VERSION_NAME
group POM_GROUP_ID

Expand All @@ -13,3 +14,8 @@ dependencies {

apply from: rootProject.file('gradle/java-artifacts.gradle')
apply from: rootProject.file('gradle/gradle-maven-upload.gradle')

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.bytedance.rheatrace.plugin

import com.android.build.gradle.AppExtension
import com.android.build.gradle.BaseExtension
import com.bytedance.rheatrace.common.utils.RheaLog
import com.bytedance.rheatrace.plugin.extension.RheaBuildExtension
import com.bytedance.rheatrace.plugin.extension.TraceCompilation
Expand All @@ -27,6 +28,8 @@ import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.internal.classloader.VisitableURLClassLoader
import java.io.File

class RheaTracePlugin : Plugin<Project> {
private val TAG = "RheaTracePlugin"
Expand All @@ -44,11 +47,30 @@ class RheaTracePlugin : Plugin<Project> {
if (!project.plugins.hasPlugin("com.android.application")) {
throw GradleException("Rhea Plugin: Android Application plugin required.")
}
val appExtension = project.extensions.getByName("android") as AppExtension
RheaTraceCompat().inject(
project.extensions.getByName("android") as AppExtension,
appExtension,
project,
rheaTrace
)
CopyMappingTask.registerTaskSaveMappingToAssets(project, rheaTrace)
project.afterEvaluate {
val sdkDirectory = appExtension.sdkDirectory
val compileSdkVersion =
appExtension.compileSdkVersion
?: throw IllegalStateException("compileSdkVersion获取失败")
val androidJarPath = "platforms/${compileSdkVersion}/android.jar"
val androidJar = File(sdkDirectory, androidJarPath)
appendFileToClassLoader(androidJar)
}
}

private fun appendFileToClassLoader(file: File) {
if (javaClass.classLoader is VisitableURLClassLoader) {
val classLoader =
javaClass.classLoader as VisitableURLClassLoader
println("appendToClassLoader $file")
classLoader.addURL(file.toURL())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,19 @@ open class MethodTracer(
changedFileOutput.createNewFile()
return changedFileOutput
}

private fun innerTraceMethodFromJar(input: File, output: File) {
var zipOutputStream: ZipOutputStream? = null
var zipFile: ZipFile? = null
var inputZipFile: ZipFile? = null
try {
zipOutputStream = ZipOutputStream(FileOutputStream(output))
zipFile = ZipFile(input)
val enumeration = zipFile.entries()
var zipEntry: ZipEntry? = null
inputZipFile = ZipFile(input)
val enumeration = inputZipFile.entries()
while (enumeration.hasMoreElements()) {
try {
zipEntry = enumeration.nextElement()
val zipEntryName = zipEntry.name
if (zipEntryName.endsWith(".class")) {
val inputStream = zipFile.getInputStream(zipEntry)
val inputZipEntry = enumeration.nextElement()
val zipEntryName = inputZipEntry.name
if (zipEntryName.endsWith(".class")) {
try {
val inputStream = inputZipFile.getInputStream(inputZipEntry)
val classReader = ClassReader(inputStream)
val classWriter = ClassWriter(ClassWriter.COMPUTE_FRAMES)
val classVisitor: ClassVisitor =
Expand All @@ -204,55 +202,53 @@ open class MethodTracer(
)
classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES)
val data = classWriter.toByteArray()
val byteArrayInputStream: InputStream = ByteArrayInputStream(data)

// 创建新的 ZipEntry 并将数据写入
val newZipEntry = ZipEntry(zipEntryName)
FileUtil.addZipEntry(zipOutputStream, newZipEntry, byteArrayInputStream)
}
} catch (e: Throwable) {
RheaLog.e(
TAG,
"Failed to trace class:%s, e:%s",
if (zipEntry == null) "null" else zipEntry.name,
e
)
if (zipEntry != null) {
FileUtil.addZipEntry(zipOutputStream, zipEntry, zipFile.getInputStream(zipEntry))
} else {
throw RuntimeException("zipEntry == null")
zipOutputStream.putNextEntry(newZipEntry)
zipOutputStream.write(data)
zipOutputStream.closeEntry()
} catch (e: Throwable) {
RheaLog.e(
TAG,
"Failed to trace class: %s, e: %s",
inputZipEntry.name,
e
)
}
} else {
// 直接将条目复制到输出压缩包中
val newZipEntry = ZipEntry(zipEntryName)
zipOutputStream.putNextEntry(newZipEntry)
val inputStream = inputZipFile.getInputStream(inputZipEntry)
inputStream.copyTo(zipOutputStream)
zipOutputStream.closeEntry()
}
}
} catch (e: Exception) {
e.printStackTrace()
RheaLog.e(
TAG,
"[innerTraceMethodFromJar] input:%s output:%s e:%s",
"[innerTraceMethodFromJar] input: %s output: %s e: %s",
input.name,
output,
e
)
} finally {
try {
Files.copy(
input.toPath(),
output.toPath(),
StandardCopyOption.REPLACE_EXISTING
)
} catch (e1: Exception) {
e1.printStackTrace()
zipOutputStream?.close()
} catch (e: Exception) {
// 处理关闭异常
}
} finally {
try {
if (zipOutputStream != null) {
zipOutputStream.finish()
zipOutputStream.flush()
zipOutputStream.close()
}
zipFile?.close()
inputZipFile?.close()
} catch (e: Exception) {
RheaLog.e(TAG, "close stream err!")
// 处理关闭异常
}
}
}


private fun listClassFiles(
classFiles: ArrayList<File>,
folder: File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.bytedance.rheatrace.precise.PreciseInstrumentationContext
import com.bytedance.rheatrace.precise.extension.PreciseInstrumentationExtension
import com.google.common.hash.Hashing
import org.gradle.api.Project
import org.gradle.internal.classloader.VisitableURLClassLoader
import java.io.File
import java.util.*
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -77,7 +78,6 @@ class TraceWeaver(
legacyReplaceChangedFile: ((File, Map<File, Status>) -> Any)?,
legacyReplaceFile: ((File, File) -> (Any))?
) {

RheaLog.i(TAG, "isIncremental ===> $isIncremental")
val executor: ExecutorService = Executors.newFixedThreadPool(16)

Expand Down Expand Up @@ -115,6 +115,7 @@ class TraceWeaver(
val collectJarInputTask = CollectJarInputTask(
file, status, inputToOutput, isIncremental, traceClassOutputDirectory, legacyReplaceFile, jarInputOutMap
)
appendFileToClassLoader(file)
futures.add(executor.submit(collectJarInputTask))
}
}
Expand Down Expand Up @@ -153,6 +154,15 @@ class TraceWeaver(
collectedMethodMap.clear()
}

private fun appendFileToClassLoader(file: File) {
if (javaClass.classLoader is VisitableURLClassLoader) {
val classLoader =
javaClass.classLoader as VisitableURLClassLoader
println("appendToClassLoader $file")
classLoader.addURL(file.toURL())
}
}

private fun initMethodId(isIncremental: Boolean): AtomicInteger {
val methodCollectMethodFile = File(RheaFileUtils.getMethodMapFilePath(project, variantName))
if (methodCollectMethodFile.exists() && isIncremental) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ class RheaTraceLegacyTransform(
}

private fun doTransform(invocation: TransformInvocation, transformArgs: TransformArgs) {

val start = System.currentTimeMillis()

val isIncremental = invocation.isIncremental && this.isIncremental
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class RheaTraceTransform(
}

private fun transforming(invocation: TransformInvocation, transformArgs: TransformArgs) {

val start = System.currentTimeMillis()

val outputProvider = invocation.outputProvider!!
Expand Down
8 changes: 7 additions & 1 deletion rhea-build/rhea-precise-instrumentation/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
apply plugin: "java-library"
apply plugin: 'java'
apply plugin: 'kotlin'

version POM_VERSION_NAME
Expand All @@ -13,4 +15,8 @@ dependencies {
implementation project(":rhea-build-common")
}
apply from: rootProject.file('gradle/java-artifacts.gradle')
apply from: rootProject.file('gradle/gradle-maven-upload.gradle')
apply from: rootProject.file('gradle/gradle-maven-upload.gradle')
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}