问题
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.
回答1:
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
}
}
回答2:
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
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/50208356/cannot-access-activity-binding-android