Android AGP update to 8.x, transform api is Deprecated. I don't know how to do it

195 Views Asked by At

First step

I want to collect all the class names that satisfy the condition, they have fixed prefixes and suffixes, such as:

  • com.xiaojinzi.component.impl.UserModuleGenerated
  • com.xiaojinzi.component.impl.BaseModuleGenerated
  • com.xiaojinzi.component.impl.OrderModuleGenerated

the name list is [User, Base, Order]

Second step

I have a class in my project, code show as below

public class ASMUtl {
   
    @NonNull
    public static List<String> getModuleNames() {
        return Collections.emptyList();
    }

}

I want to modify it to look like this

public class ASMUtl {
   
    @NonNull
    public static List<String> getModuleNames() {
        List<String> result = new ArrayList<>();
        // 示例代码
        result.add("user");
        result.add("Base");
        result.add("Order");
        return result;
    }

}

Before Gradle 8.x, I can use Transform Api to complete these two steps But now with Gradle 8.x I don't know how to achieve This problem is encountered in my 'Component' open source library, please help me

I have tried the method of AsmClassVisitorFactory Api, but it can only modify the class, and cannot collect information in advance

androidComponents.onVariants { variant ->

    variant
        .instrumentation
        .transformClassesWith(
            classVisitorFactoryImplClass = CollectModuleNameAsmClassVisitorFactory::class.java,
            scope = InstrumentationScope.ALL,
        ) {
            // empty
        }

variant.instrumentation.setAsmFramesComputationMode(FramesComputationMode.COPY_FRAMES)

}
0

There are 0 best solutions below