LagouAndroidShare icon indicating copy to clipboard operation
LagouAndroidShare copied to clipboard

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

Open pinguo-zhouwei opened this issue 4 years ago • 1 comments

关于在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);
            }
        }

pinguo-zhouwei avatar Jul 29 '20 03:07 pinguo-zhouwei

第一个问题的第一种解决方案找不到插件,第二种方案有效。[赞]

AymerZhang avatar Aug 11 '20 02:08 AymerZhang