绑定到子包中的 class 时如何解决 "Cannot access class" 错误?

How to address "Cannot access class" error when binding to a class in a subpackage?

从Android 3.2.1开始,绑定到子包中定义的类(例如com.example中的Sub.Thing)导致错误:

Cannot access class 'Sub.Thing'. Check your module classpath for missing or conflicting dependencies

出现此错误的原因是什么?如何解决?

示例代码:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".MainActivity"
    >

    <data>
        <import type="com.example.Sub.Thing"/>
        <variable name="data" type="Thing"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:text="@{data.name}"/>

        <TextView
                android:id="@+id/value"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:text="@{data.value}"/>

    </LinearLayout>
</layout>

Thing.kt:

package com.example.Sub

data class Thing(
    var name:String,
    var value:String
)

MainActivity.kt:

package com.example

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.Sub.Thing
import com.example.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        val activityMain:ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        activityMain.data = Thing("bam", "bug-AWWK!")
    }
}

按照惯例(对于 good reason), only class names are supposed to start uppercase. It appears newer versions of the compiler assume (enforce?) this convention (uppercase letters later in the name are accepted, though this may cause other issues)。

解决方法很简单:将子包重命名为小写,可以在Android项目视图中右键点击Refactor → Rename,或者点击打开Refactor →重命名菜单项。