android - 使用 kotlin-android-extensions 从子模块导入 View 时 Unresolved 引用

标签 android module kotlin inject kotlin-android-extensions

当我尝试在多模块应用程序中使用 kotlin-android-extendions 的 View 注入(inject)时,从 android.library 子模块注入(inject) View 时出现错误:

Unresolved reference: component_xyz_user_name

我们有一个主要的应用 模块和一个 android.library 子模块 潜艇 .应用模块引用 subm。这两个模块都使用数据绑定(bind)、kapt 和 android-extensions。

在这两个模块中,gradle 文件包含:
apply plugin: 'com.android.library' //or com.android.application
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    [...]

    dataBinding.enabled = true
    androidExtensions.experimental = true
}

在 subm 库中,我们定义 component_user_info.xml View 定义如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

    <data> [...] </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/component_xyz_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </android.support.constraint.ConstraintLayout>
</layout>

在哪里 component_xyz_user_name是我们将在下一步中注入(inject)的 View 。

在主应用程序中,我们定义了 fragment_main.xml 像这样查看:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>[...]</data>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/news_details_coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <include layout="@layout/component_user_info"
            />

    </android.support.design.widget.CoordinatorLayout>
</layout>

使用 ViewModel MainFragmentViewModel 以下列方式定义:
import kotlinx.android.synthetic.main.component_user_info.*

class MainFragment : Fragment() {

    fun updateUserInfo() {
        component_xyz_user_name.text = "ABCDEF"
    }
}

编译失败并出现以下错误:
e: /Users/user/repos/project/app/src/main/java/com/company/users/MainFragment.kt: (108, 9): Unresolved reference: component_xyz_user_name
e: /Users/user/repos/project/app/src/main/java/com/company/users/MainFragment.kt: (109, 9): Unresolved reference: component_xyz_user_name

为什么我会收到 Unresolved reference :component_xyz_user_name .有什么解决方法吗?

编辑:

作为 临时解决方法 我为 Activity & Fragment 编写了以下扩展函数:
/**
 * Find view in an activity
 */
fun <T : View> Activity.v(@IdRes resId: Int): T = findViewById(resId)

/**
 * Find view in a fragment
 */
fun <T : View> Fragment.v(@IdRes resId: Int): T = activity.findViewById(resId)

这样我就可以:
fun updateUserInfo() {
    v<TextView>(R.id.component_xyz_user_name).text = "ABCDEF"
}

最佳答案

我有一个类似的问题 - 我使用 git 添加了一个子模块,但我无法在 kotlin 文件中访问它,所以我必须在 build.gradle 中添加这部分:

sourceSets {

    main {
        java {
            srcDir 'src/main/java/module_folder'
        }
    }
}

这必须是主要的android目的

关于android - 使用 kotlin-android-extensions 从子模块导入 View 时 Unresolved 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52424817/

相关文章:

android - 尝试从 Google 相册中选择视频时出现空值

java - 桌面时钟可滚动标签栏 - 它叫什么?

kotlin - 几周而不是几天

Android - 如何在 onPause 中关闭所有对话框

android - 我如何解析器numberformatexception错误

testing - 将模块与 makefile (Ocaml) 结合使用时出现问题

apache-flex - Flex 加载模块 READY 事件未触发,为什么?

node.js - 直接调用全局安装的 Node.js 模块

android - 状态栏不是透明的而是白色的

flutter - GetStream.io获取提要中帖子总数