android-fragments Android视图绑定-多模块项目中存在未解析的引用

7xzttuei  于 2022-11-13  发布在  Android
关注(0)|答案(2)|浏览(138)

问题

  • Crypto Tweets * 示例应用root分支为三个应用模块appapp-rxapp-simple模块实现了 * Android View Binding *。所有三个模块都按预期运行。但是,在appapp-rx模块中,Unresolved reference存在Lint错误。

例如,在appapp-rx模块中,fragment_feed.xml是未解析的,而在app-simple中,引用显示没有导入错误。
IssueTracker * bug has been filed *。请用星号星星该问题以提高其关注度。

配置

  • 安卓工作室4.1.1
  • 构建编号AI-201.8743.12.41.6953283,构建日期:2020年11月4日
  • 执行阶段版本:1.8.0_242-发布版-1644B3 - 6915495x86_64
  • 虚拟机:OpenJDK 64位服务器虚拟机,由JetBrains s.r.o提供
  • macOS 10.16操作系统
  • GC:ParNew,并发标记清除
  • 内存:1979 M
  • 内核数:16
  • 注册表:ide.new.welcome.screen.force=真,外部.系统.自动.导入.禁用=真
  • 非捆绑插件:请访问:www.example.com,cn.wjdghd.unique.plugin.idmobi.hsz.idea.gitignore,com.developerphil,adbidea,com.google,mad-记分卡

架构

  • 设置.gradle*
rootProject.name='CryptoTweets'
include ':app', ':app-rx', ':app-simple'
  • 项目导航视图 *
  • app:* 加密推文 应用 java 应用 加密推文 摘要 摘要片段 *
  • app-rx:* 加密推文 应用程序接收 源代码 主代码 java 应用程序 加密推文 摘要 摘要片段 *
  • app-simple:* 加密推文 简单应用 java 应用 加密推文 摘要 摘要片段 *

实施

  • build.gradle*(项目)
buildscript {
    ext.gradle_version = '4.1.1'
    ext.kotlin_version = '1.4.21'
    ext.kotlin_coroutines_version = '1.3.9'
    ext.appcompat = '1.2.0'
    ext.legacy_support = '1.0.0'
    ext.core_ktx = '1.3.2'
    ext.constraint_layout = '2.0.4'
    ext.navigation_version = '2.3.2'
    ext.navigation_safe_args_version = '2.3.1'
    ext.dagger_version = '2.28.1'
    ext.retrofit_version = '2.9.0'
    ext.lifecycle_livedata_ktx = '2.2.0'
    ext.room_version = '2.2.5'
    ext.room_alpha_version = '2.3.0-alpha03'
    ext.paging_version = '2.1.2'
    ext.paging_version_alpha = '3.0.0-alpha10'
    ext.coil_version = '0.13.0'
    ext.glide_version = '4.11.0'
    ext.junit = '4.13.1'
    ext.androidx_junit = '1.1.2'
    ext.espresso_core = '3.3.0'
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_safe_args_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

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

app-simple的第一个字符

此模块不包含FragmentFeedBindingFeedFragment.ktUnresolved reference的View Binding Lint错误。

  • build.gradle(:简单应用程序)*
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'androidx.navigation.safeargs.kotlin'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "app.cryptotweets"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    compileOptions { targetCompatibility JavaVersion.VERSION_1_8 }

    kotlinOptions.jvmTarget = '1.8'

    buildFeatures.viewBinding = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.21"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.2"
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.2"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha10"
    implementation "io.coil-kt:coil:0.13.0"

    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
  • 馈送片段.kt*
package app.cryptotweets.feed

import androidx.fragment.app.Fragment
import app.cryptotweets.R
import app.cryptotweets.databinding.FragmentFeedBinding
import app.cryptotweets.feed.adapter.FeedAdapter

class FeedFragment : Fragment(R.layout.fragment_feed) {

    private val viewModel: FeedViewModel by viewModels()
    lateinit var adapter: FeedAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // The View Binding reference for 'FragmentFeedBinding' imports as expected.
        val binding = FragmentFeedBinding.bind(view)
        initViewStates(binding)
    }
}

app的第一个字符

此模块包含FragmentFeedBindingFeedFragment.ktUnresolved reference的视图绑定Lint错误。

  • 编译.gradle(:应用程序)*
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'androidx.navigation.safeargs.kotlin'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "app.cryptotweets"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    compileOptions { targetCompatibility JavaVersion.VERSION_1_8 }

    kotlinOptions.jvmTarget = '1.8'

    buildFeatures.viewBinding = true

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "androidx.appcompat:appcompat:$appcompat"
    implementation "androidx.legacy:legacy-support-v4:$legacy_support"
    implementation "androidx.core:core-ktx:$core_ktx"
    implementation "androidx.constraintlayout:constraintlayout:$constraint_layout"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_livedata_ktx"
    implementation "androidx.room:room-runtime:$room_alpha_version"
    implementation "androidx.room:room-ktx:$room_alpha_version"
    kapt "androidx.room:room-compiler:$room_alpha_version"
    implementation "androidx.paging:paging-runtime-ktx:$paging_version_alpha"
    implementation "io.coil-kt:coil:$coil_version"

    testImplementation "junit:junit:$junit"
    testImplementation "androidx.room:room-testing:$room_version"
    androidTestImplementation "androidx.test.ext:junit:$androidx_junit"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_core"
}
  • 馈送片段.kt*
package app.cryptotweets.feed

import androidx.fragment.app.Fragment
import app.cryptotweets.R
import app.cryptotweets.databinding.FragmentFeedBinding
...

class FeedFragment : Fragment(R.layout.fragment_feed) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Fixme: Unresolved reference for 'FragmentFeedBinding'
        val binding = FragmentFeedBinding.bind(view)
        initViewStates(binding)
    }
}

app-rx的第一个字符

此模块包含FragmentFeedBindingFeedFragment.ktUnresolved reference的视图绑定Lint错误。

  • 构建.gradle(:应用程序-rx)*
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'androidx.navigation.safeargs.kotlin'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    kotlinOptions { jvmTarget = '1.8' }

    defaultConfig {
        applicationId "app.cryptotweets"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    compileOptions { targetCompatibility JavaVersion.VERSION_1_8 }

    buildFeatures.viewBinding = true

}

dependencies {
    def rx_version = '3.0.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$appcompat"
    implementation "androidx.legacy:legacy-support-v4:$legacy_support"
    implementation "androidx.core:core-ktx:$core_ktx"
    implementation "androidx.constraintlayout:constraintlayout:$constraint_layout"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_livedata_ktx"
    implementation "androidx.room:room-runtime:$room_alpha_version"
    implementation "androidx.room:room-ktx:$room_alpha_version"
    kapt "androidx.room:room-compiler:$room_alpha_version"
    implementation "androidx.paging:paging-runtime-ktx:$paging_version"
    implementation "androidx.paging:paging-rxjava2-ktx:$paging_version"
    implementation "io.coil-kt:coil:$coil_version"
    implementation "io.reactivex.rxjava3:rxkotlin:$rx_version"
    implementation "io.reactivex.rxjava3:rxandroid:$rx_version"
    implementation "com.squareup.retrofit2:adapter-rxjava3:$retrofit_version"

    testImplementation "junit:junit:$junit"
    testImplementation "androidx.room:room-testing:$room_version"
    androidTestImplementation "androidx.test.ext:junit:$androidx_junit"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_core"
}
  • 馈送片段.kt*
package app.cryptotweets.feed

import androidx.fragment.app.Fragment
import app.cryptotweets.R
import app.cryptotweets.databinding.FragmentFeedBinding
...

class FeedFragment : Fragment(R.layout.fragment_feed) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Fixme: Unresolved reference for 'FragmentFeedBinding'
        val binding = FragmentFeedBinding.bind(view)
        viewModel.launchViewEvents(this)
        initViewStates(binding)
    }

}

尝试的解决方案

1.* 文件 将项目与Gradle文件同步 *
1.* 生成 清理项目 *
1.* 生成 重建项目 *
1.* 清除生成缓存 *
1.* 文件 使缓存无效/重新启动... *
1.更新依赖关系库版本。
1.从GitHub重新克隆 * Crypto Tweets * 示例应用存储库。

yrdbyhpb

yrdbyhpb1#

有相同的问题,添加

implementation "androidx.compose.ui:ui-viewbinding:1.2.1"

module Gradle文件,import androidx.compose.ui.viewinterop.AndroidViewBinding上的错误消失

swvgeqrz

swvgeqrz2#

它肯定会晚,但只是以防万一其他人也面临同样的问题,这是我如何解决它:
在我的具体案例中,我在模块Gradle文件中有下一个实现:

implementation "androidx.core:core-ktx:+"

在更改为Kotlin特定版本后,如下所示:

implementation "androidx.core:core-ktx:1.8.0"

“未解析的引用”构建错误已消失。

相关问题