android - 无法解析库模块中的 Kotlin Android 扩展布局

标签 android gradle kotlin kotlin-android-extensions

我有我的 buyerseller 模块和一个 common 模块。 buyerseller 模块中使用的少数布局放在 common 模块中。

common -> layout_toolbar.xml
buyer -> activity_sell.xml ->

<LinearLayout>
    <include layout="@layout/layout_toolbar" /> <!-- layout_toolbar.xml is from common module -->
</LinearLayout>

seller -> activity_buy.xml ->

<RelativeLayout>
    <include layout="@layout/layout_toolbar" /> <!-- layout_toolbar.xml is from common module -->
</RelativeLayout>

buyer -> BuyActivity.kt
toolbar.title = "Buy"

seller -> SellActivity.kt
toolbar.title = "Sell"

在 IDE 中一切正常,

  1. 通过解析包含标记并在 IDE 的布局预览中膨胀,我的布局预览正确显示。
  2. 我的 kotlin 文件也正确解析了工具栏引用,并且没有显示任何问题。

但每当我尝试构建应用程序时,都会出现编译器错误:

Unresolved reference: toolbar <-- Id of the toolbar inside layout_toolbar.xml

如果IDE可以正确解决依赖关系,为什么gradle不能构建?是不是我做错了什么?

请注意common 模块作为implementation 添加到其他两个模块中。但是我尝试过 api 也不起作用。

最佳答案

起初这似乎是一个好问题。我在当前项目中重现了同样的问题。

  1. 我已经通过

    添加了库模块

    实现项目(':mylibrary')

  2. 在项目中添加了库模块。

enter image description here

  1. 在库模块(即公共(public)模块)中创建了布局/ View 。

enter image description here

  1. 包含在主布局中

enter image description here

  1. 最后,我能够以编程方式更改它。

    导入 kotlinx.android.synthetic.main.activity_home.* 导入 kotlinx.android.synthetic.main.item_library.*

enter image description here

构建的环境可能存在一些问题。注意:我使用的是最新的构建版本以及 android 和 kotlin 插件。

请更新您的日志以进一步澄清您的问题。

关于android - 无法解析库模块中的 Kotlin Android 扩展布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179177/

相关文章:

java - 如何将图像保存到 Room Persistence Library

android - 如何让 Eclipse 在再次运行之前删除我的应用程序的用户数据

android - 将项目从 Eclipse 导入到 Android Studio 时出错

java - 将动画 Drawable 保存为 GIF 文件?

android - 如何从适配器Kotlin增加通知徽章的计数器

android - 是否可以让 Android 应用程序在特定设备上以不同方式工作?

java - 日期和时间格式

android - 使用发布构建类型构建 android studio 签名的 apk

java - list 合并失败,编译时出错

Kotlin Flow - 是否可以按需添加新值