我在动态功能模块中使用 DataBinding 时遇到问题。
我无法生成与我的片段相关的绑定对象isMinifyEnabled = true
通常,我尝试使用此代码来执行此操作:
val viewDataBinding: FragmentFeature1Binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
但返回的inflate()
值始终为 null 而没有任何附加消息。我尝试FragmentFeature1Binding
直接使用膨胀布局,但我得到了相同的结果。
当我将片段移动到app
模块中时,一切正常。
我的应用程序build.gradle.kts
:
android {
compileSdkVersion(AndroidVersions.compileSdk)
buildToolsVersion(AndroidVersions.buildTools)
dataBinding.isEnabled = true
defaultConfig {
applicationId = ApplicationConfig.id
minSdkVersion(AndroidVersions.minSdk)
targetSdkVersion(AndroidVersions.targetSdk)
versionCode = ReleaseVersions.versionCode
versionName = ReleaseVersions.versionName
}
buildTypes {
getByName(BuildTypes.debug) {
isMinifyEnabled = true
isShrinkResources = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
buildConfigField("Long", "API_TIMEOUT_IN_SECONDS", "30l")
}
}
compileOptions {
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
}
dynamicFeatures = mutableSetOf(Modules.feature1)
}
我的特点1 build.gradle.kts
:
android {
compileSdkVersion(AndroidVersions.compileSdk)
dataBinding.isEnabled = true
defaultConfig {
minSdkVersion(AndroidVersions.minSdk)
targetSdkVersion(AndroidVersions.targetSdk)
}
compileOptions {
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
create(BuildTypes.instrumentation) {
buildConfigField("String", "API_ENDPOINT", "\"https://api.github.com/\"")
}
getByName(BuildTypes.debug) {
buildConfigField("String", "API_ENDPOINT", "\"https://api.github.com/\"")
}
getByName(BuildTypes.release) {
buildConfigField("String", "API_ENDPOINT", "\"https://api.github.com/\"")
}
}
}
我的proguard-rules.pro
-dontwarn android.databinding.**
-keep class android.databinding.** { *; }
-keep class kotlin.Metadata {
*;
}
-keepattributes InnerClasses
-keep class **.R
-keep class **.R$* {
<fields>;
}
如果没有 R8,一切都会正常工作,所以我认为缺少一些规则,但我尝试了一些配置,遗憾的是谷歌没有官方配置。