Search code examples
androidgradleandroid-databinding

Gradle Plugin 3.2.0 with databinding, can not resolve package name


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 screenshotenter image description here


Solution

  • 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? :/