最近在优化项目看到多个 jar(A.jar B.jar C.jar) 包里面都创建了同样的 utils 类,里面代码都大同小异,于是乎就删除了这些utils类,创建了一个utils.jar 给其他jar(A.jar B.jar C.jar) 引用,然后再把 A.jar B.jar C.jar 提供给其他apk 使用,然后编译神奇的发现 A.jar B.jar C.jar 竟然找不到 utils 里面的类。
最后总结出一个结论如下:
A.jar 里面简单这样写
dependencies {
api files('lib/utils.jar')
}
那么 A 在生成 jar 包时是不会把utils.jar打包进去的,为啥没有深究,解决方案如下:
方案一:
M 应用依赖 A.jar 时,编译时警告找不到 utils.jar 里面的类,然后 error: Please correct the above warnings first. 报错如下
Warning: com.hsae.autosdk.vehicle.apahmi.ApaHmi: can’t find referenced class com.hsae.libutils.LogUtils
Warning: com.hsae.autosdk.vehicle.apahmi.ApaHmi: can’t find referenced class com.hsae.libutils.LogUtils
Warning: com.hsae.autosdk.vehicle.dualscreen.DualScreen: can’t find referenced class com.hsae.libutils.LogUtils
Warning: com.hsae.autosdk.vehicle.dualscreen.DualScreen: can’t find referenced class com.hsae.libutils.LogUtilsWarning: there were 52 unresolved references to classes or interfaces.
You may need to add missing library jars or update their versions.
If your code works fine without the missing classes, you can suppress
the warnings with ‘-dontwarn’ options.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Error: Please correct the above warnings first.
只需要在 M 应用的依赖关系下添加 utils.jar 的依赖即可。
这里有一点很神奇,这里警告的是在 A 里面找不到 utils 类,为啥在 M 应用在添加对 utils 的依赖,A 里面就可以找到utils?
方案二:
添加Jar包相关的索引文件,大概的意思就是通过索引文件 让 A.jar 找到utils.jar ,可以网上搜索下相关的文章,未亲自测试。