Skip to content

关于Gradle Plugin 3.6.0以上并且使用Androidx的问题 #6

@pinguo-zhouwei

Description

@pinguo-zhouwei

关于在gralde 3.6.0以上跑示例同时使用了androidx的同学看这里,有2个坑:

  • 1、crash错误:Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.appcompat.R$drawable"
  • 2、Logcat 看不到日志输出
为啥会有这2个问题?
  • 第一个问题原因是:gradle 3.6.0以上R类不会转为.class文件而会转成jar,因此在Transform实现中需要单独拷贝,TransformInvocation.inputs.jarInputs
  • 第二个问题是因为,androidx中,AppCompatActivity的报名变了,因此在ClassVisitor中,判断条件要更改
解决方案:

第一个问题的解决方案:在transform方法中,inputs.each block 中添加如下代码:

 transformInput.jarInputs.forEach {
            it.file.copyTo(
                info.outputProvider.getContentLocation(it.name, inputTypes, scopes, Format.JAR),
                overwrite = true
            )
        }

或者:

transformInput.jarInputs.each { JarInput jarInput ->
                File file = jarInput.file
                System.out.println("find jar input: " + file.name)
                def dest = outputProvider.getContentLocation(jarInput.name,
                        jarInput.contentTypes,
                        jarInput.scopes, Format.JAR)
                FileUtils.copyFile(file, dest)
            }

第二个问题的解决方案就很简单:在ClassVisitor类的的viditmethod方法中,将下面的代码:

 if(superClassname.equals("android/support/v7/AppCompatActivity")){
            if(name.startsWith("onCreate")){
               return new LifeCycleMethodVisitor(methodVisitor,className,name);
            }
        }

改为

 if(superClassname.equals("androidx/appcompat/app/AppCompatActivity")){
            if(name.startsWith("onCreate")){
               return new LifeCycleMethodVisitor(methodVisitor,className,name);
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions