Gradle Plugin 3.2.0 with databinding, can not resolve package name

Deadly 提交于 2019-12-23 12:15:05

问题


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

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
}

And this is the build.gradle for the module which having the problem:

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'
}

I tried all sort of things:

  • Cleaning and rebuilding the project
  • Invalidating caches and restarting
  • Cleaning gradle and gradle cache
  • Updating Gradle to the latest version
  • Changing the module name and the used package name in it

None did work.

EDIT: added screenshot


回答1:


I had the SAME ISSUE today.

The problem is your package name itself. You mentioned:

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

Short Answer:

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

Long Answer:

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.

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

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!!

BONUS:

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

But why would anyone want to do this? :/




回答2:


I am using the Highcharts library (https://github.com/highcharts/highcharts-android) and I found the same problem with databinding - import after updating the gradle plugin to 3.2.0.

This version has probably errors included, so stay with the version 3.1.3.

The databinding file in version 3.2.0 :

import error in gradle 3.2.0

The databinding file in version 3.1.3:

import OK in gradle 3.1.3




回答3:


In your codes, it's actually using EmojiIconEdittext helper class which comes from supernova library but in your dependencies, there is no such dependency added in there.

So, add this to your Build.gradle dependencies:

implementation 'com.github.hani-momanii:SuperNova-Emoji:1.1'

Or remove the class (If you don't wanna use it) and then it will work fine.

P.S: The import actually tells the truth.

import supernova.emoji.helper


来源:https://stackoverflow.com/questions/52495124/gradle-plugin-3-2-0-with-databinding-can-not-resolve-package-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!