Gradle Plugin 3.2.0具有数据绑定功能,无法解析包名称 [英] Gradle Plugin 3.2.0 with databinding, can not resolve package name

查看:86
本文介绍了Gradle Plugin 3.2.0具有数据绑定功能,无法解析包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Android Studio从3.1.2更新到3.2.0,并将gradle插件更新到3.2.0之后,我遇到了生成的数据绑定类的问题,该类抱怨类名不存在的软件包,但是它确实存在.该软件包属于项目中的一个模块.

After updating Android Studio from 3.1.2 to 3.2.0, and updating the gradle plugin with it to 3.2.0, I am having a problem with a generated databinding classes which are complaining about a package name that does not exist, but it does exist. The package belongs to a module in the project.

以下是我在尝试运行该应用程序时遇到的错误:

Here are the errors that I am getting when trying to run the app:

错误:找不到符号类助手

error: cannot find symbol class Helper

错误:软件包助手不存在

error: package Helper does not exist

这是我的项目级别build.gradle文件:

This is my project level build.gradle file:

buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath "com.google.gms:google-services:4.0.1"
        classpath 'io.fabric.tools:gradle:1.25.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://jitpack.io'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是有问题的模块的build.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
    }
}

dependencies {
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

我尝试了各种事情:

  • 清洁和重建项目
  • 使缓存无效并重新启动
  • 清理gradle和gradle缓存
  • 将Gradle更新到最新版本
  • 更改模块名称和其中使用的包名称

没有工作.

添加了屏幕截图

推荐答案

我今天有相同的问题.

I had the SAME ISSUE today.

问题在于您的软件包名称本身.您提到:

The problem is your package name itself. You mentioned:

我的项目中没有Helper类,它是一个程序包名称.该程序包确实存在,但是数据绑定找不到它.

there is no Helper class in my project, it is a package name. The package does exist, but databinding can't find it.

简短答案:

更改以小写字母开头的软件包名称.发生此问题的原因是您的程序包名称为Helper.将其更改为helper.

Change your package name that starts with a lower case letter. The problem happens because your package name is Helper. Change it to helper.

详细答案:

在Android Plugin 3.2.0及更高版本中,默认情况下启用数据绑定V2. 我想数据绑定V2编译器会将以大写字母开头的任何组件都视为一个类,而不是一个包.实际上,它正在查找名称为Helper而不是包Helper的类.

In Android Plugin 3.2.0 and above, the databinding V2 is enabled by default. I guess the databinding V2 compiler treats any component that starts with an upper case letter as a class, not a package. It is actually looking for a class with a name Helper, not the package Helper.

由于您之前使用的是Android 3.1.2,而后者使用的是数据绑定V1编译器,因此程序包名称不是问题.

Because you were using Android 3.1.2 before, which is using the databinding V1 compiler, the package name wasn't an issue.

我重命名了我项目中所有的包(文件夹)名称,以小写字母开头,并最终编译了该项目.重命名软件包时,请确保使用重构工具(Shift + F6),这样更改也可以应用于您的XML文件!

I renamed all my package(folder) names in my project to start with a lower case letter and the project was finally compiled. Make sure to use the refactor tool (Shift + F6) when you rename the packages so that the change can be applied to your XML files also!!

奖励:

如果要保留软件包名称以大写字母开头,但又要使用Android Plugin 3.2.0(不建议这样做),请转到根文件夹中的gradle.properties并添加这条线.这将禁用databindingV2编译器,并强制项目使用旧的V1编译器.因此,您的班级名称将无关紧要.

Just in case you want to keep the package names to start with upper case letters, but also want to use the Android Plugin 3.2.0 (which is not really recommended), go to gradle.properties in the root folder and add this line. This disables the databindingV2 compiler and forces the project to use the old V1 compiler. Therefore your class name won't matter.

android.databinding.enableV2 = false

android.databinding.enableV2=false

但是为什么有人要这样做呢? :/

But why would anyone want to do this? :/

这篇关于Gradle Plugin 3.2.0具有数据绑定功能,无法解析包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆