简体   繁体   English

在APK中包含具有相同软件包名称和名称的类

[英]Including classes with the same package name and name in an APK

When building an APK for Android, the DX tool fails in case you attempt to add the same library twice with an error such as this: 在为Android构建APK时,如果您尝试两次添加同一库,但出现以下错误,则DX工具会失败:

UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.IllegalArgumentException: already added: Lcom/google/ads/AdRequest$Gender; 意外的最高级别异常:java.lang.IllegalArgumentException:已添加:Lcom / google / ads / AdRequest $ Gender; at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122) at com.android.dx.dex.file.DexFile.add(DexFile.java:161) at com.android.dx.command.dexer.Main.processClass(Main.java:685) at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634) at com.android.dx.command.dexer.Main.access$600(Main.java:78) at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572) at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166) at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158) at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144) at com.android.dx.command.dexer.Main.processOne(Main.java:596) at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498) at com.android.dx.command.dexer.Main.runMonoDex(Main.java: 在com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)在com.android.dx.dex.file.DexFile.add(DexFile.java:161)在com.android.dx.command .dexer.Main.processClass(Main.java:685)在com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)在com.android.dx.command.dexer.Main.access $ 600(位于com.android.dx.command.dexer.Main $ 1.processFileBytes(Main.java:572)处com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)处的Main.java:78) com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)上的com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)。在com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)上的com.android.dx.command.dexer.Main.processOne(Main.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)。 com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)处com.android.dx.command.dexer.Main.runMonoDex(Main.java: 264) at com.android.dx.command.dexer.Main.run(Main.java:230) at com.android.dx.command.dexer.Main.main(Main.java:199) at com.android.dx.command.Main.main(Main.java:103) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at SDKMain.main(SDKMain.java:129) 1 error; 264),位于com.android.dx.command.dexer.Main.run(Main.java:230),位于com.android.dx.command.dexer.Main.main(Main.java:199),com.android.dx .command.Main.main(Main.java:103)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl SDKMain.main(SDKMain.java:129)处java.lang.reflect.Method.invoke(Method.java:483)处的.java:43)1错误; aborting 中止

What if i have 2 different libraries (jars) that contain classes with the same package name and name ? 如果我有2个不同的库(jar),它们包含具有相同包名和名称的类,该怎么办?

How can i add these into my APK? 如何将这些添加到我的APK中? Is there any way to do this ? 有什么办法吗?

First please note that the "already added" error may pop up even if the two jars are not in the same project. 首先请注意,即使两个罐子不在同一个项目中,也会弹出“已经添加”错误。 For example: linking your project to a library project containing a jar with same classes. 例如:将项目链接到包含具有相同类的jar的库项目。

Secondly: if you do have two jars with the same class file the build system will simply use the first one that it sees. 其次:如果您确实有两个具有相同类文件的jar,则构建系统将仅使用它所看到的第一个。 So, if you know which of them you want your runtime to use, the solution is simple: just place it earlier in the classpath. 因此,如果您知道要使用运行时中的哪一个,则解决方案很简单:只需将其放在类路径中的较早位置即可。 Or visually speaking - place it higher in the Order and Export list : 或从视觉上讲- 将其放在“订购和出口”列表的上方

在此处输入图片说明


Thirdly: if you really have no other way, you can always open a jar file (ee using WinRAR ) and delete some of its class files. 第三:如果您确实没有其他方法,则始终可以打开一个jar文件(使用WinRAR的 ee)并删除其某些类文件。

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

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