Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout文件有依赖关系,改变子文件编译会报Unresolved reference #24

Closed
MarkMjw opened this issue Dec 28, 2021 · 16 comments
Closed

Comments

@MarkMjw
Copy link

MarkMjw commented Dec 28, 2021

依赖关系:app->module和base,module->base,各模块都开启viewBinding
base中有layout如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:descendantFocusability="beforeDescendants"
        android:fadingEdge="none"
        android:fadingEdgeLength="0dp"
        android:listSelector="@android:color/transparent"
        android:overScrollMode="never"
        android:scrollbars="none" />

    <FrameLayout
        android:id="@+id/state_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

在module中有layout如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/list"
        layout="@layout/layout_recycler" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="Hello Rocketx!"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

module中有Activity如下:

class FeatureActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityFeatureBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val recycler = binding.list.recyclerView
        recycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    }
}

首次可以成功run,此时改变module中layout文件(如:增加一个TextView)
再次run:binding.list.recyclerView这里报错:Unresolved reference: recyclerView

@trycatchx
Copy link
Owner

@MarkMjw app module是否有 加入 viewBinding = true

@MarkMjw
Copy link
Author

MarkMjw commented Dec 28, 2021

@MarkMjw app module是否有 加入 viewBinding = true

有的。

@trycatchx
Copy link
Owner

@MarkMjw 这个是运行时报错?第二次编译启动 app npe ,你尝试第二次手动打开 app 是否还会报错?之前我这边也遇到这个问题。确认一下是不是同个问题?

@MarkMjw
Copy link
Author

MarkMjw commented Dec 28, 2021

@MarkMjw 这个是运行时报错?第二次编译启动 app npe ,你尝试第二次手动打开 app 是否还会报错?之前我这边也遇到这个问题。确认一下是不是同个问题?

不是APP启动而是编译报错。

我在官方项目中添加两个module能够复现此问题。

完整demo RocketXPlugin.zip

@trycatchx
Copy link
Owner

trycatchx commented Dec 28, 2021

好的,感谢,我这边尝试一下哈。

@trycatchx
Copy link
Owner

@MarkMjw 大概知道问题,include 了base 模块的xml ,在 base 打成aar 后,这个include 布局被转成 View (正确应该是 baseBinding) 导致引用得不到recycleview

@trycatchx
Copy link
Owner

@MarkMjw 需要时间研究一下怎么解决。你急的话可以改掉跨模块使用 include

@MarkMjw
Copy link
Author

MarkMjw commented Dec 29, 2021

@MarkMjw 需要时间研究一下怎么解决。你急的话可以改掉跨模块使用 include

不急,辛苦研究,我也看看

@LatlngV
Copy link

LatlngV commented Dec 29, 2021

rebuild 就可以了

@trycatchx
Copy link
Owner

@MarkMjw @LatlngV clean+ rockectClean 一下然后再编译就可以了。list 的类型就正常了

image

@LatlngV
Copy link

LatlngV commented Dec 29, 2021

是不是要从源头上解决一下,这样操作太麻烦了

@trycatchx
Copy link
Owner

@LatlngV 遇到这个问题,应该只需要clean 一次。后面都正常了。

xxxBinding 生成的操作,不是 gradle 做动作,是一个 intellij plugin 内置在 AS 里面,类似本项目的 rocketX-studio-plugin ,无法干预。 只能看看有无其他办法。

@MarkMjw
Copy link
Author

MarkMjw commented Dec 29, 2021

@MarkMjw @LatlngV clean+ rockectClean 一下然后再编译就可以了。list 的类型就正常了

image

没有开启gradle缓存的话,这样确实可以,如果gradle.properties里面配置了开启缓存

org.gradle.caching=true

clean之后仍然有问题,感觉不太好解决。

@trycatchx
Copy link
Owner

trycatchx commented Dec 29, 2021

@MarkMjw org.gradle.caching=true 有无设置都一样。只要修改了 xml 就会重新生成对应的 layoutbinding,如果刚好使用 include 了其他module 的xml (其他module 变成 aar)。这个include 就会被 viewbind 转成 VIEW 。目前有两种缓解的做法:

1、如果非常多的模块 都是使用 include 了 baseui 中的 xml ,可以通过 excludeModule = [":baseui"] 强制不打成 aar (就多一个模块不打 aar ,整体速度影响不大)
2、如果就较少的模块 跨模块 include ,直接改掉即可

这一块问题可能需要持续的研究。目前没想到好的办法解决呢。

@MarkMjw
Copy link
Author

MarkMjw commented Dec 29, 2021

@MarkMjw org.gradle.caching=true 有无设置都一样。只要修改了 xml 就会重新生成对应的 layoutbinding,如果刚好使用 include 了其他module 的xml (其他module 变成 aar)。这个include 就会被 viewbind 转成 VIEW 。目前有两种缓解的做法:

1、如果非常多的模块 都是使用 include 了 baseui 中的 xml ,可以通过 excludeModule = [":baseui"] 强制不打成 aar (就多一个模块不打 aar ,整体速度影响不大) 2、如果就较少的模块 跨模块 include ,直接改掉即可

这一块问题可能需要持续的研究。目前没想到好的办法解决呢。

OK,暂时也只能先exclude了。

@trycatchx
Copy link
Owner

trycatchx commented Dec 29, 2021

@MarkMjw 如果有想到好的办法,欢迎提PR ,感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants