欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)AsmClassVisitorFactory使用詳解

 更新時間:2022年06月21日 17:27:13   作者:究極逮蝦戶  
這篇文章主要為大家介紹了Android開發(fā)AsmClassVisitorFactory使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

之前就和大家介紹過AGP(Android Gradle Plugin) 7.0.0版本之后Transform 已經過期即將廢棄的事情。而且也簡單的介紹了替換的方式是Transform Action,經過我這一陣子的學習和調研,發(fā)現只能說答對了一半吧。下面介紹個新東西AsmClassVisitorFactory。

com.android.build.api.instrumentation.AsmClassVisitorFactory

A factory to create class visitor objects to instrument classes.

The implementation of this interface must be an abstract class where the parameters and instrumentationContext are left unimplemented. The class must have an empty constructor which will be used to construct the factory object.

當前官方推薦使用的應該是這個類,這個類的底層實現就是基于gradle原生的Transform Action,這次的學習過程其實走了一點點彎路,一開始嘗試的是Transform Action,但是貌似彎彎繞繞的,最后也沒有成功,而且Transform Action的輸入產物都是單一文件,修改也是針對單一文件的,所以貌似也不完全是一個很好的替換方案,之前文章介紹的那種復雜的asm操作則無法負荷了。

AsmClassVisitorFactory根據官方說法,編譯速度會有提升,大概18%左右,這個下面我們會在使用階段對其進行介紹的。

我們先從AsmClassVisitorFactory這個抽象接口開始介紹起吧。

AsmClassVisitorFactory

@Incubating
interface AsmClassVisitorFactory<ParametersT : InstrumentationParameters> : Serializable {
    /**
     * The parameters that will be instantiated, configured using the given config when registering
     * the visitor, and injected on instantiation.
     *
     * This field must be left unimplemented.
     */
    @get:Nested
    val parameters: Property<ParametersT>
    /**
     * Contains parameters to help instantiate the visitor objects.
     *
     * This field must be left unimplemented.
     */
    @get:Nested
    val instrumentationContext: InstrumentationContext
    /**
     * Creates a class visitor object that will visit a class with the given [classContext]. The
     * returned class visitor must delegate its calls to [nextClassVisitor].
     *
     * The given [classContext] contains static information about the classes before starting the
     * instrumentation process. Any changes in interfaces or superclasses for the class with the
     * given [classContext] or for any other class in its classpath by a previous visitor will
     * not be reflected in the [classContext] object.
     *
     * [classContext] can also be used to get the data for classes that are in the runtime classpath
     * of the class being visited.
     *
     * This method must handle asynchronous calls.
     *
     * @param classContext contains information about the class that will be instrumented by the
     *                     returned class visitor.
     * @param nextClassVisitor the [ClassVisitor] to which the created [ClassVisitor] must delegate
     *                         method calls.
     */
    fun createClassVisitor(
        classContext: ClassContext,
        nextClassVisitor: ClassVisitor
    ): ClassVisitor
    /**
     * Whether or not the factory wants to instrument the class with the given [classData].
     *
     * If returned true, [createClassVisitor] will be called and the returned class visitor will
     * visit the class.
     *
     * This method must handle asynchronous calls.
     */
    fun isInstrumentable(classData: ClassData): Boolean
}

簡單的分析下這個接口,我們要做的就是在createClassVisitor這個方法中返回一個ClassVisitor,正常我們在構造ClassVisitor實例的時候是需要傳入下一個ClassVisitor實例的,所以我們之后在new的時候傳入nextClassVisitor就行了。

另外就是isInstrumentable,這個方法是判斷當前類是否要進行掃描,因為如果所有類都要通過ClassVisitor進行掃描還是太耗時了,我們可以通過這個方法過濾掉很多我們不需要掃描的類。

@Incubating
interface ClassData {
    /**
     * Fully qualified name of the class.
     */
    val className: String
    /**
     * List of the annotations the class has.
     */
    val classAnnotations: List<String>
    /**
     * List of all the interfaces that this class or a superclass of this class implements.
     */
    val interfaces: List<String>
    /**
     * List of all the super classes that this class or a super class of this class extends.
     */
    val superClasses: List<String>
}

ClassData并不是asm的api,所以其中包含的內容相對來說比較少,但是應該也勉強夠用了。這部分大家簡單看看就行了,就不多做介紹了呢。

新的Extension

AGP版本升級之后,應該是為了區(qū)分新舊版的Extension,所以在AppExtension的基礎上,新增了一個AndroidComponentsExtension出來。

我們的transformClassesWith就需要注冊在這個上面。這個需要考慮到變種,和之前的Transform還是有比較大的區(qū)別的,這樣我們就可以基于不同的變種增加對應的適配工作了。

        val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
        androidComponents.onVariants { variant ->
            variant.transformClassesWith(PrivacyClassVisitorFactory::class.java,
                    InstrumentationScope.ALL) {}
            variant.setAsmFramesComputationMode(FramesComputationMode.COPY_FRAMES)
        }

實戰(zhàn)

這次還是在之前的敏感權限api替換的字節(jié)碼替換工具的基礎上進行測試開發(fā)。

ClassVisitor

看看我們正常是如何寫一個簡單的ClassVisitor的。

ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor methodFilterCV = new ClassFilterVisitor(classWriter);
ClassReader cr = new ClassReader(srcClass);
cr.accept(methodFilterCV, ClassReader.SKIP_DEBUG);
return classWriter.toByteArray();

首先我們會構造好一個空的ClassWriter,接著會構造一個ClassVisitor實例,然后傳入這個ClassWriter。然后我們構造一個ClassReader實例,然后將byte數組傳入,之后調用classReader.accept方法,之后我們就能在visitor中逐個訪問數據了。

那么其實我們的類信息,方法啥的都是通過ClassReader讀入的,然后由當前的ClassVisitor訪問完之后交給我們最后一個ClassWriter。

其中ClassWriter也是一個ClassVisitor對象,他復雜重新將修改過的類轉化成byte數據??梢钥吹贸鰜?code>ClassVisitor就有一個非常簡單的鏈表結構,之后逐層向下訪問。

介紹完了這個哦,我們做個大膽的假設,如果我們這個ClassVisitor鏈表前插入幾個不同的ClassVisitor,那么我們是不是就可以讓asm修改逐個生效,然后也不需要多余的io操作了呢。這就是新的asm api 的設計思路了,也是我們這邊大佬的字節(jié)碼框架大佬的設計。另外bytex內的設計思路也是如此。

tips ClassNode 因為是先生成的語法樹,所以和一般的ClassVisitor有點小區(qū)別,需要在visitEnd方法內調用accept(next)

實際代碼分析

接下來我們上實戰(zhàn)咯。我將之前的代碼套用到這次的邏輯上來。

demo地址

abstract class PrivacyClassVisitorFactory : AsmClassVisitorFactory<InstrumentationParameters.None> {
    override fun createClassVisitor(classContext: ClassContext, nextClassVisitor: ClassVisitor): ClassVisitor {
        return PrivacyClassNode(nextClassVisitor)
    }
    override fun isInstrumentable(classData: ClassData): Boolean {
        return true
    }
}

我在isInstrumentable都返回的是true,其實我可以將掃描規(guī)則限定在特定包名內,這樣就可以加快構建速度了。

class PrivacyClassNode(private val nextVisitor: ClassVisitor) : ClassNode(Opcodes.ASM5) {
    override fun visitEnd() {
        super.visitEnd()
        PrivacyHelper.whiteList.let {
            val result = it.firstOrNull { whiteName ->
                name.contains(whiteName, true)
            }
            result
        }.apply {
            if (this == null) {
                //   println("filter: $name")
            }
        }
        PrivacyHelper.whiteList.firstOrNull {
            name.contains(it, true)
        }?.apply {
            val iterator: Iterator<MethodNode> = methods.iterator()
            while (iterator.hasNext()) {
                val method = iterator.next()
                method.instructions?.iterator()?.forEach {
                    if (it is MethodInsnNode) {
                        it.isPrivacy()?.apply {
                            println("privacy transform classNodeName: ${name@this}")
                            it.opcode = code
                            it.owner = owner
                            it.name = name
                            it.desc = desc
                        }
                    }
                }
            }
        }
        accept(nextVisitor)
    }
}
private fun MethodInsnNode.isPrivacy(): PrivacyAsmEntity? {
    val pair = PrivacyHelper.privacyList.firstOrNull {
        val first = it.first
        first.owner == owner && first.code == opcode && first.name == name && first.desc == desc
    }
    return pair?.second
}

這部分比較簡單,把邏輯抽象定義在類ClassNode內,然后在visitEnd方法的時候調用我之前說的accept(nextVisitor)方法。

另外就是注冊邏輯了,和我前面介紹的內容基本都是一樣的。

個人觀點

AsmClassVisitorFactory相比較于之前的Transform確實簡化了非常非常多,我們不需要關心之前的增量更新等等邏輯,只要專注于asm api的操作就行了。

其次就是因為減少了io操作,所以其速度自然也就比之前有所提升。同時因為基于的是Transform Action,所以整體性能還是非常ok的,那部分增量可以說是更簡單了。

另外我也和我的同事大佬交流過哦,復雜的這種類似上篇文章介紹的,最好還是使用Gradle Task的形式進行修改。

以上就是Android開發(fā)AsmClassVisitorFactory使用詳解的詳細內容,更多關于Android開發(fā)AsmClassVisitorFactory的資料請關注腳本之家其它相關文章!

相關文章

  • Android 8.0中一些坑以及對應的解決方法

    Android 8.0中一些坑以及對應的解決方法

    這篇文章主要給大家介紹了關于Android 8.0中一些坑以及對應的解決方法的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • Android基于高德地圖完全自定義Marker的實現方法

    Android基于高德地圖完全自定義Marker的實現方法

    這篇文章主要給大家介紹了關于Android基于高德地圖完全自定義Marker的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • android創(chuàng)建數據庫(SQLite)保存圖片示例

    android創(chuàng)建數據庫(SQLite)保存圖片示例

    這篇文章主要介紹了android創(chuàng)建數據庫,保存圖片到數據庫再從數據庫取圖片的方法,大家參考使用吧
    2014-01-01
  • Android高仿微信對話列表滑動刪除效果

    Android高仿微信對話列表滑動刪除效果

    這篇文章主要為大家詳細介紹了Android高仿微信對話列表滑動刪除效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • MotionLayout自定義開關按鈕實例詳解

    MotionLayout自定義開關按鈕實例詳解

    這篇文章主要為大家介紹了MotionLayout自定義開關按鈕實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android編程設計模式之工廠方法模式實例詳解

    Android編程設計模式之工廠方法模式實例詳解

    這篇文章主要介紹了Android編程設計模式之工廠方法模式,結合實例形式詳細分析了Android工廠方法模式的概念、原理、使用方法及相關注意事項,需要的朋友可以參考下
    2017-12-12
  • Android適配利用webview加載后圖片顯示過大的問題解決

    Android適配利用webview加載后圖片顯示過大的問題解決

    這篇文章主要給大家介紹了關于Android適配利用webview加載后圖片顯示過大問題的解決方法,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • Android自動獲取短信驗證碼功能

    Android自動獲取短信驗證碼功能

    這篇文章主要介紹了Android自動獲取短信驗證碼功能,通過自定義短信監(jiān)聽器用于自動填充驗證碼功能,具體實現代碼大家可以參考下本文
    2017-04-04
  • Android 中View.onDraw(Canvas canvas)的使用方法

    Android 中View.onDraw(Canvas canvas)的使用方法

    這篇文章主要介紹了Android 中View.onDraw(Canvas canvas)的使用方法的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 嗶哩嗶哩在Hilt組件化的使用技術探索

    嗶哩嗶哩在Hilt組件化的使用技術探索

    這篇文章主要為大家介紹了嗶哩嗶哩在Hilt組件化的使用技術探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06

最新評論