搬砖的鱼 發表於 2020-9-25 11:02:00

【填坑往事】Android开发中导入第三方库所遇问题记录——Program type already present

<p>&nbsp; &nbsp; &nbsp; &nbsp;最近做项目部署时由于一些特别的需求,导致在导入jar包问题上出现不少问题。由于这些问题花了不少时间才找到解决方案,这里特地记录一下,希望能帮到碰到相同困扰的你!</p>
<p><strong>1、重复循环依赖的问题</strong></p>
<p>(1)需求</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;如下图所示:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/472002/202009/472002-20200925103848674-951839129.png"></p>
<p>&nbsp;在Android 项目中,采用模块化开发,一个是主跑application——Mudule A,另外一个是library——Library B</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)Module A,其中libs中有个x.jar,该Module中需要用到此jar包。Module A由其他团队维护,由于某些原因,不可改动其结构。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)Library B,也需要使用x.jar,且Module A依赖该Library B。该Library由我维护。</p>
<p>(2)错误的方案</p>
<p style="text-align: center">&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/472002/202009/472002-20200925105700067-436067038.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 如上图,是我尝试的一种方案,在Library B中的libs目录下也添加了x.jar并依赖。当然这种方案后来报错了,报错信息为(这里使用的x.jar包其实就是google的protobuf库):</p>
<div class="cnblogs_code">
<pre>Program type already present: com.google.protobuf.BlockingRpcChannel</pre>
</div>
<p>网上查的资料显示,这是由于重复依赖导致的。Module A中依赖了x.jar,又依赖了 Library B,而Library B又依赖了x.jar,不难发现这里存在重复依赖的问题。</p>
<p>(3)困难点</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;这里因为特定的需求,不能改变Module&nbsp; A的结构,不然我们可以把 x.jar单独放入一个Module中,然后Module A和 Library B都依赖该新Module。又由于Module A是依赖于Library B的,所以不能再让Library B来依赖Module A来达到使用x.jar的目的,否则会出现循环依赖的问题。</p>
<p>(4)最终解决方案</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;整体的项目结构没有改变,还是</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/472002/202009/472002-20200925103848674-951839129.png"></p>
<p>只是在Library B的build.gradle文件中添加了如下依赖</p>
<div class="cnblogs_code">
<pre>implementation files('../Module A/libs/x.jar')</pre>
</div>
<p>这样问题就解决了。</p>
<p>(5)总结</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;其实该问题的思路很简单,就是让Library B直接引用 Module A/libs/x.jar。之所以最开始没想到这一点,主要还是因为在这个依赖需要手写,而不能在Android Studio中配置后自动生成,一般配置路径为File &gt; Project Structure &gt; Dependencies &gt; Library B &gt; Jar Dependency,如下图:</p>
<p><img src="https://img2020.cnblogs.com/blog/472002/202009/472002-20200925112748544-1611484458.png"></p>
<p>&nbsp;而此时在这里Android Studio无法自动给出路径提示,不能直接找到 Module A/libs/x.jar。<br>&nbsp; &nbsp; &nbsp; &nbsp;这也就是折腾很长时间后才找到这个方法的原因,所以这里特别记录一下,也许能帮到碰到相同困扰的同仁。</p>
<p>&nbsp;</p>
<p>2、framework.jar造成的 Caused by: java.lang.ArrayIndexOutOfBoundsException: 65535问题</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/andy-songwei/p/13728838.html
頁: [1]
查看完整版本: 【填坑往事】Android开发中导入第三方库所遇问题记录——Program type already present