(安卓)Proguard 找不到动态引用的类 javax.swing.*

10

我在使用Proguard和外部库时遇到了问题。我的项目在编译时没有错误,在真实设备上成功运行,但只有在从Eclipse运行它时才能正常工作。现在我想制作一个混淆的发布版本,所以我按照以下步骤操作:

运行“android update project -p ./”

添加

proguard.dir=/home/stoned/android-sdk-linux_86/proguard4.5.1/lib
proguard.config=/home/stoned/workspace/Fidoo/proguard.cfg

当我运行 "ant release" 时,我得到以下结果:
... skipped many irrelevant messages ...

-obfuscate:
    [mkdir] Created dir: /home/stoned/workspace/Fidoo/bin/proguard
      [jar] Building jar: /home/stoned/workspace/Fidoo/bin/proguard/original.jar
 [proguard] ProGuard, version 4.4
 [proguard] ProGuard is released under the GNU General Public License. The authors of all
 [proguard] programs or plugins that link to it (com.android.ant, ...) therefore
 [proguard] must ensure that these programs carry the GNU General Public License as well.
 [proguard] Reading input...
 [proguard] Reading program jar [/home/stoned/workspace/Fidoo/bin/proguard/original.jar]
 [proguard] Reading program jar [/home/stoned/workspace/Fidoo/libs/exapi-1.16.jar]
 [proguard] Reading program jar [/home/stoned/workspace/Fidoo/libs/libthrift.jar]
 [proguard] Reading program jar [/home/stoned/workspace/Fidoo/libs/log4j-1.2.14.jar]
 [proguard] Reading library jar [/home/stoned/android-sdk-linux_86/platforms/android-4/android.jar]
 [proguard] Initializing...
 [proguard] Warning: org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNodeRenderer: can't find superclass or interface javax.swing.tree.DefaultTreeCellRenderer
 [proguard] Warning: org.apache.log4j.lf5.viewer.categoryexplorer.TreeModelAdapter: can't find superclass or interface javax.swing.event.TreeModelListener
 [proguard] Warning: org.apache.log4j.lf5.viewer.LogBrokerMonitor$32: can't find superclass or interface java.awt.event.ActionListener
 [proguard] Warning: org.apache.log4j.lf5.viewer.LogBrokerMonitor$31: can't find superclass or interface java.awt.event.ActionListener

... skipped many lines of similar messages ...

 [proguard] Note: org.apache.log4j.lf5.viewer.categoryexplorer.CategoryAbstractCellEditor: can't find dynamically referenced class javax.swing.event.CellEditorListener
 [proguard] Note: org.apache.log4j.lf5.viewer.categoryexplorer.CategoryAbstractCellEditor: can't find dynamically referenced class javax.swing.event.CellEditorListener
 [proguard] Note: org.apache.log4j.lf5.viewer.categoryexplorer.CategoryAbstractCellEditor: can't find dynamically referenced class javax.swing.event.CellEditorListener
 [proguard] Note: org.apache.log4j.lf5.viewer.categoryexplorer.CategoryAbstractCellEditor: can't find dynamically referenced class javax.swing.event.CellEditorListener
 [proguard] Note: org.apache.log4j.spi.LocationInfo: can't find dynamically referenced class com.ibm.uvm.tools.DebugSupport
 [proguard] Note: there were 5 unresolved dynamic references to classes or interfaces.
 [proguard]       You should check if you need to specify additional program jars.
 [proguard] Warning: there were 1656 unresolved references to classes or interfaces.
 [proguard]          You may need to specify additional library jars (using '-libraryjars'),
 [proguard]          or perhaps the '-dontskipnonpubliclibraryclasses' option.

很明显,在Android应用程序中不需要javax.swing类和log4j类。我该如何让Proguard不搜索这些已引用的库?
3个回答

22
如果没有不包含Swing依赖的log4j版本,而且在构建过程中不能轻易地添加jar过滤器,那么最干净的解决方案是在proguard.cfg中关闭这些特定的警告:
-dontwarn org.apache.log4j.lf5.viewer.**
-dontnote org.apache.log4j.lf5.viewer.**

你将会收到任何其他相关的警告。


2
没事了,刚刚添加了。
-dontwarn
-dontnote

转换为 proguard.cfg


3
你建议不再警告/注明任何事情。那可能不是你想要的。 - JKaan

2

好的,您将log4j放入了您的类路径中(libs/log4j-1.2.14.jar)。这就是为什么log4j被包含在内的原因。它还会引用javax.swing。


我的项目使用外部库,该库使用libthrift.jar,而libthrift.jar又使用log4j。因此,我不能简单地删除log4j。此外,编译器不会抱怨不存在的库(我认为是因为它们在代码中没有被使用),但是proguard会。 - Renat
我明白了。但是,这是否会影响应用程序的运行时行为呢(尽管Proguard可能无法完全优化代码)? - Thilo-Alexander Ginkel
它根本不创建apk文件,只是写了“构建失败” /home/stoned/android-sdk-linux_86/tools/ant/main_rules.xml:430: 请先纠正以上警告。 - Renat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接