7

I have a layout file called activity_suggestions. I am using databinding in it. Hence the file ActivitySuggestionsBinding got generated. The project compiles successfully. But when I try to run the project, I get this error

e: error: cannot access ActivitySuggestionsBinding

I am using android studio 3.1.2 with kotlin version 1.4.1. Any help will be appreciated

Edit
Pasting my module level build.gradle and app level build.gradle

Module Build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {

    dataBinding {
        enabled = true
    }
..
}

dependencies{
..
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    provided 'javax.annotation:jsr250-api:1.0'
    implementation "android.arch.lifecycle:runtime:$rootProject.archVersion"
    implementation "android.arch.lifecycle:extensions:$rootProject.archVersion"
    annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archVersion"
    kapt "com.android.databinding:compiler:3.1.2"
..
}

App build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android{

    dataBinding{
        enabled = true
    }
..
}

dependencies{
    compile project(':module')
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    kapt "com.android.databinding:compiler:3.1.2"
..
}

This is the activity where I am accessing ActivitySuggestionsBinding. This compiles without any error.

class SuggestionsActivityScreen : BaseActivity() {

    var binding : ActivitySuggestionsBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_suggestions)
        binding?.model = SuggestionActivityViewModel()

    }
}

On compiling the base module (app), this is the error I get

 error: cannot access ActivitySuggestionsBinding
  class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
  Consult the following stack trace for details.
  com.sun.tools.javac.code.Symbol$CompletionFailure: class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found

This is my activity_suggestions.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="model"
            type="com.dom.domp.SuggestionActivityViewModel"/>
    </data>

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusableInTouchMode="true"
    android:padding="@dimen/step1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@{model.namedString}"/>
</RelativeLayout>
</layout>

And I have tried clean, invalidate cache. These don't solve the problem.

tynn
  • 38,113
  • 8
  • 108
  • 143
Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • @tynn : I am not sure what context you want. Anything specific? That's the only error I get. – Ashwin May 07 '18 at 07:37
  • Usually there's more information to it. Maybe well hidden in some logs. Also it might be interesting, which compiler has this error. – tynn May 07 '18 at 07:57
  • @tynn : pasting my build.gradle files in the edit – Ashwin May 07 '18 at 08:05
  • @tynn : Was pasting the gradle files helpful? – Ashwin May 07 '18 at 09:35
  • It's still missing where you want to access `ActivitySuggestionsBinding`. But you're app's gradle file is missing the kapt reference to the databinding compiler. – tynn May 07 '18 at 09:42
  • @tynn : I will add the code where I am accessing ActivitySuggestionsBinding. Regarding the kapt reference, I don't think I got you? what do you mean by kapt reference? – Ashwin May 07 '18 at 10:02
  • @tynn : Added the code where ActivitySuggestionsBinding is being accessed – Ashwin May 07 '18 at 10:07
  • @tynn : Understood. I added the kapt reference in app's build.gradle. Still the same error. – Ashwin May 07 '18 at 10:16
  • @tynn : Have the edits been helpful now? – Ashwin May 08 '18 at 03:50
  • Most of the time just clean and rebuild does the trick. Have you tried it? If so update this information in your question so that someone else doesn't recommend it – Sagar May 10 '18 at 02:27
  • Also update your question with activity_suggestions.xml – Sagar May 10 '18 at 02:33
  • @Sagar done. Added activity_suggestions.xml – Ashwin May 10 '18 at 03:59
  • @Ashwin rebuild didn't work? – Sagar May 10 '18 at 04:01
  • @Sagar : Nope. tried rebuildm clean, invalidate cache – Ashwin May 10 '18 at 05:23
  • I've never used data binding in a multi-module project, might have something to do with this? https://stackoverflow.com/questions/41765114/databinding-not-working-in-module – Mateo May 10 '18 at 14:57
  • @Ashwin Are you using ViewModel component in your binding? – Sagar May 11 '18 at 01:27
  • @Sagar : I am not using ViewModel that is provided by android. I am using a simple class for view model - SuggestionActivityViewModel – Ashwin May 11 '18 at 05:17
  • @SirCodesalot : Edited the question. I was just trying to obfuscate thedomain name. Request you to please delete your comment – Ashwin May 15 '18 at 01:53
  • Did you apply the android plugins in your _build.gradle_? – tynn May 15 '18 at 21:57
  • @tynn : Yes, I have. You can see the build.gradle files that I have pasted in the question. Both app and module apply the plugin kotlin-kapt – Ashwin May 16 '18 at 02:24
  • @Ashwin Try adding following to your module build.gradle `android { /// Existing Code kapt { generateStubs = true } }` I have also posted an answer for it. – Randheer May 16 '18 at 11:03

3 Answers3

0

In this demo it is work and try this way.. add only databinding enable code into app level gradle file like below code..

dataBinding {
    enabled = true
}

below code is my app level gradle file ..

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.adruser.rafdemo"
    minSdkVersion 23
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dataBinding {
    enabled = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'


}
repositories {
mavenCentral()
}

then after in project level gradle.properties file add below line..

android.databinding.enableV2=true

make user_layout.xml file like below code..

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable
        name="user"
        type="com.example.adruser.rafdemo.model.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text='@{user.name}'/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvName"
        android:text="@{user.dob}"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvAge"
        android:text="@{Integer.toString(user.age)}"
        app:layout_constraintTop_toBottomOf="@+id/ulTvName"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvDob"
        android:text="@{user.dob ?? user.expDob}"
        app:layout_constraintTop_toBottomOf="@+id/ulTvAge"
        />
</android.support.constraint.ConstraintLayout>
</layout>

then after make User.java pojo class like below code.. you can make .kt class pojo also

public class User {
public String name,dob,expDob;
public int age;
public User(){}
public User(String name, String dob,String expDob, int age) {
    this.name = name;
    this.dob = dob;
    this.age = age;
    this.expDob=expDob;
}
public static String display(){
    return "rajesh";
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

then finnaly make DatabindingActivity.kt class for binding ..

class DatabindingActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    var binding:UserLayoutBinding=DataBindingUtil.setContentView(this,R.layout.user_layout)
    val user = User("karan", null, "25/06/1994", 24)
    binding.user=user
}
}
  • @Ashwin i add my code it is working to me. i hope it's help full to you. i add more extra some dependency in app level gradle file you can remove it. –  May 16 '18 at 07:18
  • android.databinding.enableV2=true gives errors in other modules. These other modules I don't have access to codebase – Ashwin May 16 '18 at 10:16
0

Add following in you app build.gradle

kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to

This will do the trick.

$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle

Also, add this to your module build.gradle

android { /// Existing Code kapt { generateStubs = true } }

Randheer
  • 984
  • 6
  • 24
0

I had similar issue. I was getting error

error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found

Spent a couple of days to figure out that the problem is not with GDB but with Dagger2. It turned out, Dagger doesn't support using generated classes in code it generates.

So, I had such class in my :library module:

class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}

And the dagger component for it were located in :app module. So the two annotation processors interfere each other.

After I removed generic from my fragments it compiles fine.

So if you have such error, try to look if you try to inject into (or provide) class which is generated by another annotation proccessor, or inherits from such class, or have generic with such class.

b1n0m
  • 283
  • 3
  • 10