Android ViewBinding报"No signature of method"错误的原因与修复
ViewBinding配置触发Gradle构建报错排查
问题描述
在项目中尝试接入ViewBinding,已完成前置准备:
- 移除所有代码中
kotlinx.android.synthetic相关的导入语句 - 在应用模块的build.gradle配置中添加如下内容:
buildFeature { viewBinding true }
配置加载过程中抛出如下构建错误:
No signature of method: build_7jhh1700vj1t807398gidh9lp.android() is applicable for argument types: (build_7jhh1700vj1t807398gidh9lp$_run_closure1) values: [build_7jhh1700vj1t807398gidh9lp$_run_closure1@6885a244]
提供的应用模块完整build.gradle配置如下:
plugins { id 'com.android.application' id 'kotlin-android' } android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.aks.navigationsafeargs" minSdkVersion 19 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 } kotlinOptions { jvmTarget = '1.8' } buildFeature { viewBinding true } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.navigation:navigation-fragment-ktx:2.3.3' implementation 'androidx.navigation:navigation-ui-ktx:2.3.3' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
报错核心原因
问题本质是配置块名称拼写错误:
- Android Gradle Plugin从3.6.0版本开始,承载各类构建功能开关的官方配置节点为
buildFeatures(复数形式,末尾带s),不存在单数形式的buildFeature配置块。 - 由于拼写错误,Gradle在解析
android{}闭包时,无法匹配到buildFeature对应的方法签名,直接抛出类型不匹配的错误,连带导致整个android{}块的解析流程失效。 - 该配置块统一收纳ViewBinding、DataBinding、AIDL、RenderScript等构建功能的开关,没有单独的单数形式配置节点。
修复方案
- 将原配置块的错误名称
buildFeature修改为正确的buildFeatures,其余原有配置无需调整,修正后的配置片段如下:
android { // 原有compileSdk、defaultConfig、buildTypes等配置保持不变 buildFeatures { viewBinding true } }
- 修正后重新执行Gradle同步,待构建配置加载完成后即可正常使用ViewBinding生成的绑定类替代原synthetic的视图调用逻辑,无需额外引入第三方依赖。
内容的提问来源于stack exchange,提问作者Akr
相关产品推荐
相关产品推荐

