简体   繁体   中英

How to use com.android.databinding in module(Android studio)?

I want create a library using com.android.databinding: dataBinder .

build.gradle for project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.android.databinding:dataBinder:1.0-rc4'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        //gradlew -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8580
    }
}


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

build.gradle for module :

apply plugin: 'com.android.library'  // apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    aaptOptions {
        additionalParameters "--no-version-vectors"
    }

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.1"
        multiDexEnabled true
        generatedDensities = []
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.android.support:support-v4:23.2.0'
 }

I change apply plugin: 'com.android.application' to apply plugin: 'com.android.library' .But when I changed the code to the following error occured.

Error this :

Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources] :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preDebugAndroidTestBuild UP-TO-DATE :app:preDebugUnitTestBuild UP-TO-DATE :app:preReleaseBuild UP-TO-DATE :app:preReleaseUnitTestBuild UP-TO-DATE :app:prepareComAndroidDatabindingAdapters10Rc3Library UP-TO-DATE :app:prepareComAndroidDatabindingLibrary10Rc3Library UP-TO-DATE :app:prepareComAndroidSupportAnimatedVectorDrawable2320Library UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72320Library UP-TO-DATE :app:prepareComAndroidSupportCardviewV72320Library UP-TO-DATE :app:prepareComAndroidSupportDesign2320Library UP-TO-DATE :app:prepareComAndroidSupportMultidex101Library UP-TO-DATE :app:prepareComAndroidSupportPaletteV72320Library UP-TO-DATE :app:prepareComAndroidSupportRecyclerviewV72320Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42320Library UP-TO-DATE :app:prepareComAndroidSupportSupportVectorDraw able2320Library UP-TO-DATE :app:prepareComGithubAfollestadMaterialDialogsCore0856Library UP-TO-DATE :app:prepareComGithubOrangegangstersSwipy122Library UP-TO-DATE :app:prepareComMakeramenRoundedimageview221Library UP-TO-DATE :app:prepareDeKeyboardsurferAndroidWidgetCrouton185Library UP-TO-DATE :app:prepareMeZhanghaiAndroidMaterialprogressbarLibrary114Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources :app:processDebugManifest :app:dataBindingLayoutsProcessDebugResources :app:processDebugResources D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\build\\intermediates\\res\\merged\\debug\\values-fa\\values-fa.xml Warning:(70) warning: string 'app_name' marked untranslatable but exists in locale 'fa' :app:dataBindingInfoClassProcessDebugResources :app:generateDebugB uildConfig UP-TO-DATE :app:generateDebugSources :app:prepareComAndroidSupportMultidexInstrumentation101Library UP-TO-DATE :app:prepareDebugAndroidTestDependencies :app:compileDebugAndroidTestAidl :app:compileDebugNdk UP-TO-DATE :app:compileLint :app:copyDebugLint UP-TO-DATE :app:compileDebugJavaWithJavac D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\activities\\common\\MoreInfoAppActivity.java Error:(9, 39) error: package com.xomorod.utility.databinding does not exist Error:(14, 3) error: cannot find symbol class ActivityMoreInfoAppBinding D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\activities\\splash\\SplashPresenterActivity.java Error:(13, 39) error: package com.xomorod.utility.databinding does not exist Error:(25, 12) error: cannot find symbol class ActivitySplashBinding D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\section\\fontManager\\CardFont.java Error:(16, 39) error: p ackage com.xomorod.utility.databinding does not exist Error:(64, 9) error: cannot find symbol class CardFontBinding D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\section\\language\\CardLanguage.java Error:(16, 39) error: package com.xomorod.utility.databinding does not exist Error:(63, 9) error: cannot find symbol class CardLanguageBinding D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\section\\market\\CardApp.java Error:(15, 39) error: package com.xomorod.utility.databinding does not exist Error:(58, 9) error: cannot find symbol class CardAppBinding D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\section\\themeManager\\CardTheme.java Error:(16, 39) error: package com.xomorod.utility.databinding does not exist Error:(63, 9) error: cannot find symbol class CardThemeBinding D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\activities\\common\\AbstractMa inActivity.java Error:(25, 15) error: attribute value must be constant Error:(28, 15) error: attribute value must be constant Error:(31, 15) error: attribute value must be constant D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\activities\\common\\MoreApplicationActivity.java Error:(25, 15) error: attribute value must be constant Error:(28, 15) error: attribute value must be constant D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\activities\\common\\SettingsActivity.java Error:(230, 19) error: attribute value must be constant Error:(301, 19) error: attribute value must be constant Error:(383, 19) error: attribute value must be constant D:\\WorkSpace\\Android\\Java\\Xomorod\\Utility\\src\\app\\src\\main\\java\\com\\xomorod\\utility\\section\\market\\MoreAppFragment.java > Error:(37, 15) error: attribute value must be constant Error:(40, 15) error: attribute value must be constant Error:(43, 15) error: attribute value must be constant Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Error:Execution failed for task ':app:compileDebugJavaWithJavac'. Compilation failed; see the compiler error output for details .Information:BUILD FAILED Information:Total time: 12.413 secs Information:24 errors Information:1 warning Information:See complete output in console

Follow the guide here:

http://developer.android.com/tools/data-binding/guide.html

You must add dataBinding.enabled = true to the android section. You should also use android gradle plugin 1.5.0 or later.

You don't need the apply plugin: 'com.android.databinding' anymore either. Data binding is now included into the android gradle plugin.

Only add

android {
    ...
    dataBinding {
        enabled = true
    }
}

if you are using gradle 2.0 or later. for example:

classpath 'com.android.tools.build:gradle:2.3.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM