DSL元素“android.dataBinding.enabled”已过时,并已替换为“android.buildFeatures.dataBinding”

ndh0cuux  于 2023-01-28  发布在  Android
关注(0)|答案(9)|浏览(143)

生成项目时收到以下警告

DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.

我使用的是Android Studio Canary 6

wz8daaqr

wz8daaqr1#

Android Gradle Plugin 4.0.0-alpha05开始,有一个名为buildFeatures的新块,用于启用构建特性。
因此,为了启用与新AGP插件的数据绑定,您必须在模块中执行以下操作(例如:应用程序)级别Gradle文件

内部版本.gradle(Groovy DSL)

// shorter version
// android.buildFeatures.dataBinding true

// longer version

android {

    buildFeatures {

         dataBinding true

         // for view binding:
         // viewBinding true
    }
}

构建版本.gradle.kts(KotlinDSL)

// shorter version
// android.buildFeatures.dataBinding = true

// longer version

android {

  buildFeatures {

         dataBinding = true

         // for view binding:
         // viewBinding = true
    }
}

参考:https://developer.android.com/studio/releases/gradle-plugin#buildFeatures

np8igboo

np8igboo2#

把它放在build.gradle(应用程序级别)中,它可以在大于或等于4.0.0的android studio版本中运行。

android {
    buildFeatures{ 
        dataBinding true // for data binding 
        viewBinding true // for view binding
    }
}
3vpjnl9f

3vpjnl9f3#

出现此警告的原因是

dataBinding {
        enabled=true
    }

    viewBinding {
        enabled=true
    }

此代码风格已弃用,现在将从gradle版本5中删除。如果您仍想使用此代码风格,则可以使用androidx传统支持依赖项
在应用程序lavel build.gradle中

implementation 'androidx.legacy:legacy-support-v4:1.0.0'

否则,您可以使用新的代码样式来启用数据绑定和视图绑定
像这样

android {

  buildFeatures {

         dataBinding = true

         // for view binding:
         // viewBinding = true
    }
}
h9a6wy2h

h9a6wy2h4#

将此代码放入Gradle脚本〉〉build.gradle(模块:)appName.app*
buildTypes**之后,包含数据投标代码

buildTypes {
       release {
           .......
          ........
       }
   }
 //here is the code...
   buildFeatures {
       dataBinding = true
   }

就是这样:)

2guxujil

2guxujil5#

如果您正在寻找新特性viewBinding,请尝试Groovy

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

这个是Kotlin

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

但是,要使用默认的android数据绑定

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

另外,请注意使用

kapt "com.android.databinding:compiler:4.0.0"
kkih6yb8

kkih6yb86#

1-在build下添加dataBinding功能如下:

android {
...
buildFeatures {
        dataBinding true
    }
...
}

2-将dagger版本更改为2.31.2:

annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.google.dagger:dagger:$daggerVersion"

3-还将butterKnife版本更改为10.2.3:

implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
smdncfj3

smdncfj37#

buildFeatures {

        //just for dataBinding ,It has nothing to do with viewBinding 
        dataBinding = true

        //just for viewBinding ,It has nothing to do with dataBinding
        viewBinding = true
    }

看看上面的注解,应该很清楚了

5gfr0r5j

5gfr0r5j8#

以下作品:
安卓系统{编译Sdk版本30构建工具版本"30.0.3"

defaultConfig {
    applicationId "com.poet.navviewmodeljave"
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
    //dataBinding.enabled true
    buildFeatures.dataBinding
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
k3fezbri

k3fezbri9#

您必须在Gradle模块中添加buildFeatures

android {
    buildFeatures{
        dataBinding true
    }
}

示例:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.demo"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures{
        dataBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'org.jetbrains:annotations:15.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

相关问题