简体   繁体   English

获取错误:使用数据绑定作为泛型类型构建多模块项目和类时,包com.mydomain.databinding不存在

[英]Getting error: package com.mydomain.databinding does not exist when building multi-module project and class with databinding as generic type

I have a multi-module project where I set my databinding classes as a generic type requirement: 我有一个多模块项目,我将数据绑定类设置为泛型类型要求:

public abstract class BaseActivity<DataBinding extends ViewDataBinding> {
    //Rest of the code...
}

However, if I extend this activity on a submodule, sometimes I get the following build error: 但是,如果我在子模块上扩展此活动,有时会出现以下构建错误:

error: cannot find symbol public void myMethod(MyActivityBinding dataBinding) { 错误:找不到符号public void myMethod(MyActivityBinding dataBinding){

error: package com.mydomain.databinding does not exist import com.mydomain.databinding.MyActivityBinding; 错误:包com.mydomain.databinding不存在import com.mydomain.databinding.MyActivityBinding;

Sometimes, creating another activity that extends from MyActivity and putting it on the main app module fixes the issue. 有时,创建另一个从MyActivity扩展并将其放在主应用程序模块上的活动可以解决问题。 But in some cases I can't pinpoint the problem, and I consistently get the same error message. 但在某些情况下,我无法确定问题,并且我始终得到相同的错误消息。

I already tried cleaning and rebuilding the project and also invalidating Android Studio cache and restarting it without success. 我已经尝试过清理和重建项目,并使Android Studio缓存无效并重新启动它而没有成功。

Any ideas on how to at least investigate what could be causing this problem? 关于如何至少调查可能导致此问题的任何想法?

Not sure about the root cause of the problem but I have similar issue and posted answer here https://stackoverflow.com/a/47718646/3160214 不确定问题的根本原因,但我有类似的问题,并在此处发布答案https://stackoverflow.com/a/47718646/3160214

In few words, this is how solution looks like: 简而言之,这就是解决方案的样子:

class A<BINDING extends ViewDataBinding> extends BaseActivity<BINDING> {
    protected ABinding binding;

    void init(){
        binding = (ABinding) DataBindingUtil.setContentView(this, R.layout.a);
    }
}

and pass the same binding to the child class from the submodule 并将相同的绑定传递给子模块中的子类

class B<ABinding> {
    // you can use instance in this class
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM