From 16c4f1016c74cf8632f058249d9b2b9d4a19557c Mon Sep 17 00:00:00 2001 From: chenjuntao Date: Mon, 20 May 2024 17:58:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=20vivo=20?= =?UTF-8?q?=E7=9A=84=20Android=2014=20=E8=AE=BE=E5=A4=87=E4=B8=8A=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E5=89=AA=E8=B4=B4=E6=9D=BF=E5=8A=9F=E8=83=BD=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AA=E9=80=80=E9=97=AE=E9=A2=98=20https://sentry.?= =?UTF-8?q?shanqu.cc/organizations/lightgame/issues/277509?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gh/gamecenter/plugin/GhTransform.kt | 6 +-- .../transform/AppCompatEditTextTransformer.kt | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/transform/AppCompatEditTextTransformer.kt diff --git a/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/GhTransform.kt b/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/GhTransform.kt index dc4ed6d4a3..3121eb6aed 100644 --- a/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/GhTransform.kt +++ b/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/GhTransform.kt @@ -6,10 +6,7 @@ import com.android.build.api.transform.Transform import com.android.build.api.transform.TransformInvocation import com.android.build.gradle.AppExtension import com.android.build.gradle.internal.pipeline.TransformManager -import com.gh.gamecenter.plugin.transform.ActivityTransformer -import com.gh.gamecenter.plugin.transform.DiskLruCacheTransformer -import com.gh.gamecenter.plugin.transform.ExoSourceManagerTransformer -import com.gh.gamecenter.plugin.transform.RoomTransformer +import com.gh.gamecenter.plugin.transform.* import javassist.ClassPool import org.apache.commons.io.FileUtils import org.apache.commons.io.IOUtils @@ -51,6 +48,7 @@ class GhTransform(var project: Project) : Transform() { mTransformHelper.addTransformer(DiskLruCacheTransformer()) mTransformHelper.addTransformer(RoomTransformer()) mTransformHelper.addTransformer(ActivityTransformer()) + mTransformHelper.addTransformer(AppCompatEditTextTransformer()) } /** diff --git a/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/transform/AppCompatEditTextTransformer.kt b/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/transform/AppCompatEditTextTransformer.kt new file mode 100644 index 0000000000..dd4c3b3740 --- /dev/null +++ b/buildSrc/src/main/kotlin/com/gh/gamecenter/plugin/transform/AppCompatEditTextTransformer.kt @@ -0,0 +1,50 @@ +package com.gh.gamecenter.plugin.transform + +import javassist.ClassPool +import javassist.CtClass +import javassist.NotFoundException +import javassist.bytecode.ClassFile +import java.io.BufferedInputStream +import java.io.DataInputStream +import java.io.InputStream + +class AppCompatEditTextTransformer : Transformer { + + private val classPool = ClassPool.getDefault() + + override fun getModifyClassName(): String { + return "AppCompatEditText" + } + + override fun modifyClass(filePath: String, inputStream: InputStream): CtClass? { + if (filePath.contains(getModifyClassName())) { + + System.out.println("发现 AppCompatEditText") + + val classFile = ClassFile(DataInputStream(BufferedInputStream(inputStream))) + val ctClass = classPool.get(classFile.name) + + if (ctClass.isFrozen) { + ctClass.defrost() + } + + // 使用 try catch 包裹找到的第一个 onTextContextMenuItem 方法 (若能找到的话) + // https://sentry.shanqu.cc/organizations/lightgame/issues/277509 + try { + val ctMethod = ctClass.getDeclaredMethod("onTextContextMenuItem") + + val exceptionType: CtClass = ClassPool.getDefault().get("java.lang.Exception") + val catchBody = "{ System.out.println(\$e); return false; }" + ctMethod.addCatch(catchBody, exceptionType) + + System.out.println("为 AppCompatEditText.onTextContextMenuItem 添加 try catch") + } catch (e: NotFoundException) { + e.printStackTrace() + } + + return ctClass + } + return null + } + +} \ No newline at end of file