使用Dagger2,房间和数据绑定时出错

时间:2018-06-29 14:11:32

标签: android gradle

存在"error: cannot find symbol class DataBindingComponent"中的错误app/build/generated/source/dataBinding/baseClasses/debug的问题。我已经尝试了人们在互联网上建议的所有选项,但没有任何帮助。 这些选项包括:     1.清理/重建项目     2.使缓存无效/重新启动

1)这是我的根build.gradle

buildscript {
        repositories {
            google()
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.3'
            classpath 'com.google.gms:google-services:4.0.1'
            // 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" }
            maven {
                url "https://maven.google.com"
            }
        }
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xmaxerrs" << "500"
            }
        }
    }

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

    ext {

        // Sdk and tools
        minSdkVersion = 17
        devMinSdkVersion = 21;
        devPreLollipopSdkVersion = 19;
        targetSdkVersion = 27
        compileSdkVersion = 27
        buildToolsVersion = '27.0.3'

        // App dependency
        supportLibraryVersion = '27.1.1'
        retrofit = "2.3.0"
        okHttp = "3.5.0"
        butterknife= "8.8.1"
        glide = "4.7.1"
        glideCompiler = "4.7.1"
        rxJava = '2.1.8'
        rxAndroid = '2.0.1'
        dagger2 = "2.16"

        // version
        versionCode = 1
        versionName = "1.0"
    }

2)这是我的模块build.gradle

`apply plugin: 'com.android.application'

android {
    dataBinding {
        enabled = true
    }
    compileSdkVersion rootProject.ext.targetSdkVersion
    dexOptions {
        maxProcessCount 2
        javaMaxHeapSize "2g"
    }
    defaultConfig {
        applicationId "com.movecrop.shipper"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled false
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "movecrop.shipper"

    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
    implementation "com.android.support:customtabs:$rootProject.supportLibraryVersion"
    implementation "com.android.support:design:$rootProject.supportLibraryVersion"
    implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:multidex:1.0.3'
    // glide
    implementation "com.github.bumptech.glide:glide:$rootProject.glide"
    annotationProcessor "com.github.bumptech.glide:compiler:$rootProject.glideCompiler"
    implementation 'jp.wasabeef:glide-transformations:2.0.2'
    // retrofit
    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
    implementation "com.squareup.retrofit2:retrofit-mock:$rootProject.retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofit"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofit"
    // okhttp3
    implementation "com.squareup.okhttp3:okhttp:$rootProject.okHttp"
    implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okHttp"
    implementation 'com.squareup.okio:okio:1.13.0'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.orhanobut:hawk:2.0.1'
    implementation 'com.jakewharton.byteunits:byteunits:0.9.1'
    implementation 'com.github.tajchert:nammu:1.2.0'
    // RxJava
    implementation "io.reactivex.rxjava2:rxjava:$rootProject.ext.rxJava"
    implementation "io.reactivex.rxjava2:rxandroid:$rootProject.ext.rxAndroid"
    // Bottom navigation
    implementation 'it.sephiroth.android.library.bottomnavigation:bottom-navigation:2.0.1-rc1'
    // curl
    implementation 'com.github.mrmike:Ok2Curl:master-SNAPSHOT'
    // Firebase
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-messaging:17.0.0'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
    // Dagger2
    implementation "com.google.dagger:dagger-android:$rootProject.dagger2"
    // Dagger2 if you use the support libraries
    implementation "com.google.dagger:dagger-android-support:$rootProject.dagger2"
    annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.dagger2"
    annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2"

    def lifecycle_version = "1.1.1"
    implementation "android.arch.lifecycle:livedata:$lifecycle_version"
    def room_version = "1.1.1"
    implementation "android.arch.persistence.room:runtime:$room_version"
    annotationProcessor "android.arch.persistence.room:compiler:$room_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

`

希望你们能给我一些建议,谢谢

已更新: 这是我的活动(及其布局xml)之一,显示错误:

package com.movecrop.shipper.ui.home;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingComponent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class HomeActivity
        extends AppCompatActivity
        implements HomeViewModel.HomeListener,
                   SwitchWorkingStatusReceiver.OnSwitchWorkingStatusListener {

    private HomeViewModel mHomeViewModel;
    ActivityHomeBinding mBinding;
    LayoutDrawerHeaderBinding mHeaderBinding;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private SwitchWorkingStatusReceiver mSwitchWorkingStateReceiver = new SwitchWorkingStatusReceiver();
    private static final String TAG = "HomeActivity";

    public static Intent newIntent(Context context) {
        Intent i = new Intent(context, HomeActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                           | Intent.FLAG_ACTIVITY_CLEAR_TASK
                           | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return i;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Injector.getInstance().component().inject(this);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_home);
        mBinding.setHomeActivity(this);
        mHomeViewModel = new HomeViewModel();
        mHomeViewModel.initialize(this);
        LocalBroadcastManager.getInstance(this).registerReceiver(
                mSwitchWorkingStateReceiver,
                SwitchWorkingStatusReceiver.getIntenFilter()
        );
        initDrawer();
        mHomeViewModel.loadUser();
        navigate(R.id.menu_dashboard);
    }
}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.movecrop.shipper.ui.home.HomeActivity">

    <data>

        <variable
            name="homeActivity"
            type="com.movecrop.shipper.ui.home.HomeActivity" />
    </data>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?android:actionBarSize"
                    app:title="@string/label_dashboard_title" />

                <ImageView
                    android:id="@+id/imgNotification"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_gravity="right|center_vertical"
                    android:cropToPadding="true"
                    android:padding="12dp"
                    android:visibility="invisible" />

            </FrameLayout>

            <FrameLayout
                android:id="@+id/frameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/drawerNavigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:headerLayout="@layout/layout_drawer_header"
            app:itemBackground="@drawable/bg_menu_item_selector"
            app:itemIconTint="@color/color_menu_item_selector"
            app:itemTextColor="@color/colorDrawerIconTint"
            app:menu="@menu/drawer">


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="vertical">

                <Button
                    android:id="@+id/btnToggle"
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/button_height"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="32dp"
                    android:layout_marginRight="32dp"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/bg_primary_button_unselected"
                    android:maxLines="1"
                    android:onClick="@{homeActivity::click}"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:text="@string/button_go_online"
                    android:textAllCaps="true"
                    android:textColor="@android:color/white"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/btnSetting"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_marginTop="32dp"
                    android:background="@drawable/ts_list_item"
                    android:drawableLeft="@drawable/ic_dm_setting"
                    android:drawablePadding="32dp"
                    android:onClick="@{homeActivity::click}"
                    android:paddingBottom="16dp"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:paddingTop="16dp"
                    android:textColor="@color/colorDrawerIconTint"
                    android:textStyle="bold"
                    android:fitsSystemWindows="true"
                    android:text="@string/dm_setting" />

            </LinearLayout>

        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>

</layout>

1 个答案:

答案 0 :(得分:3)

结果显示错误来自我的dao接口,该会议室数据库不是dagger2也不是数据绑定库。希望对任何有同样问题的人都有帮助。