解决AS报错Supertypes of the following classes cannot be resolved.

937 阅读1分钟

解决AS报错Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath

一、问题:

下列类的父类找不到,请确保添加了必要的依赖。也就是说,类X找不到类Y

二、分析:

首先要想为什么类X找不到类Y?举例:

  1. 假设现在有3个module,moduleA、moduleB、moduleC。
  2. 你在moduelB里定义了类X,在moduleC里定义了类Y,类X的父类是Y,所以你在module的build.gradle里写了implementation "moduleC",这样moduleB就依赖上了moduleC。
  3. 如果你在moduleA想使用类X,你会在moduleA的build.gradle里加上implementation "moduleB",这样moduleA就依赖上了moduleB。
  4. 此时编译报错了,在moduleA中,找不到X的父类Y了。

要知道原因,首先要明白implementation和api的区别

  • 实现A依赖于B(implementation、api都可以)
  • 如果B implementation C,A 不能调用 C 的方法
  • 如果B api C,A可以调用C的方法(同compile)

所以,虽然你moduleA implementation "modlueB", module B implementation "moduleC",A中虽然可以找到定义在B中的类X,但你在A中无法调用定义在C中类Y的方法,所以报错。

3、处理方式

方法1:把moduleB的build.gralde中的implementation "moduleC"改为api (moduleC)

方法2:把moduleA中implementation "module B" 改为 implementation "module C"