Unresolved reference when importing views from a submodule using kotlin-android-extensions
Asked Answered
F

1

7

When I try to use kotlin-android-extendions' view injection in a multi-module application I get an error injecting a view from an android.library submodule:

Unresolved reference: component_xyz_user_name

We have a main app module and an android.library submodule subm. App module references subm. Both of these modules use databinding, kapt and android-extensions.

In both modules gradle files contain:

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
}

In subm library we define component_user_info.xml view defined like so:

<?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>

where component_xyz_user_name is the view that we're gonna inject in the next step.

In main app we define a fragment_main.xml view like so:

<?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>

with a ViewModel MainFragmentViewModel defined in the following manner:

import kotlinx.android.synthetic.main.component_user_info.*

class MainFragment : Fragment() {

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

Compilation FAILS with the following error:

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

Why do I get Unresolved reference: component_xyz_user_name. Is there any workaround for this?

EDIT:

As a temporary workaround I've written the following extension function for 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)

so that I can :

fun updateUserInfo() {
    v<TextView>(R.id.component_xyz_user_name).text = "ABCDEF"
}
Farrel answered 20/9, 2018 at 12:5 Comment(3)
Possible duplicate of Kotlin Android Extension layouts from library module cannot be resolvedDecennial
Have you tried walking the hierarchy. Typically when you have an include you have to access it via dot notation. mainFragBinding.childLayout.txtBox. I understand synthetic imports should work, but it's only saving you a couple dot walks, so I would try without the synthetic import to ensure there aren't issues there. Also you may need to give your include an ID to access it by name.Losse
thanks problem was in kotlin extension, if use findViewById, it's work and can see views between modules.Brabazon
D
0

I had a similar problem - I added a submodule using git and I was not able to access it in the kotlin files, so what I had to add in the build.gradle was this part:

sourceSets {

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

and this had to be in the main android object

Daffie answered 25/11, 2019 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.