2014-08-29 113 views
1

我想在Eclipse中混淆使用Dropbox和Proguard的Android 4+應用程序。到目前爲止,我有使用ProGuard沒有經驗,因此我用不同的教程和答案在這裏SO配置我proguard-project.txtProguard抱怨外部JAR中缺少類。如何正確解決這個問題?

-optimizationpasses 5 
-dontusemixedcaseclassnames 
-dontskipnonpubliclibraryclasses 
-dontpreverify 
-verbose 
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 

-keep public class * extends android.app.Activity 
-keep public class * extends android.app.Application 
-keep public class * extends android.app.Service 
-keep public class * extends android.content.BroadcastReceiver 
-keep public class * extends android.content.ContentProvider 
-keep public class * extends android.app.backup.BackupAgentHelper 
-keep public class * extends android.preference.Preference 
-keep public class com.android.vending.licensing.ILicensingService 

當我嘗試使用File/Export.../Export Android Application/...對話框的創建失敗的Proguard的錯誤導出APK:

[2014-08-29 09:50:30 - MyApp] Proguard returned with error code 1. See console 
[2014-08-29 09:50:30 - MyApp] Note: there were 367 duplicate class definitions. 
[2014-08-29 09:50:30 - MyApp] Warning: org.apache.commons.logging.impl.ServletContextCleaner: can't find superclass or interface javax.servlet.ServletContextListener 
[2014-08-29 09:50:30 - MyApp] Warning: org.apache.http.entity.mime.FormBodyPart: can't find superclass or interface org.apache.james.mime4j.message.BodyPart 
... 
[2014-08-29 09:50:30 - MyApp] Warning: library class android.net.http.AndroidHttpClient extends or implements program class org.apache.http.client.HttpClient 
... 
[2014-08-29 09:50:30 - MyApp] Warning: org.bouncycastle.x509.util.LDAPStoreHelper: can't find referenced class javax.naming.directory.DirContext 
[2014-08-29 09:50:30 - MyApp]  You should check if you need to specify additional program jars. 
[2014-08-29 09:50:30 - MyApp] Warning: there were 223 unresolved references to classes or interfaces. 
[2014-08-29 09:50:30 - MyApp]   You may need to specify additional library jars (using '-libraryjars'). 
[2014-08-29 09:50:30 - MyApp] Warning: there were 1 instances of library classes depending on program classes. 
[2014-08-29 09:50:30 - MyApp]   You must avoid such dependencies, since the program classes will 
[2014-08-29 09:50:30 - MyApp]   be processed, while the library classes will remain unchanged. 
[2014-08-29 09:50:30 - MyApp] Warning: there were 13 unresolved references to program class members. 
[2014-08-29 09:50:30 - MyApp]   Your input classes appear to be inconsistent. 
[2014-08-29 09:50:30 - MyApp]   You may need to recompile them and try again. 
[2014-08-29 09:50:30 - MyApp]   Alternatively, you may have to specify the option 
[2014-08-29 09:50:30 - MyApp]   '-dontskipnonpubliclibraryclassmembers'. 
[2014-08-29 09:50:30 - MyApp] java.io.IOException: Please correct the above warnings first. 
[2014-08-29 09:50:30 - MyApp] at proguard.Initializer.execute(Initializer.java:321) 
[2014-08-29 09:50:30 - MyApp] at proguard.ProGuard.initialize(ProGuard.java:211) 
[2014-08-29 09:50:30 - MyApp] at proguard.ProGuard.execute(ProGuard.java:86) 
[2014-08-29 09:50:30 - MyApp] at proguard.ProGuard.main(ProGuard.java:492) 

我想第一件事情就是添加一些-libraryjars ...命令:

-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/bcprov-jdk16-146.jar' 
-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/commons-logging-1.1.1.jar' 
-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/dropbox-android-sdk-1.6.1.jar' 
-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/httpclient-4.0.3.jar' 
-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/httpcore-4.0.1.jar' 
-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/httpmime-4.0.3.jar' 
-libraryjars 'F:/Path/To/Workspace/Third Party/Dropbox/dropbox-android-sdk-1.6.1/lib/json_simple-1.1.jar' 

這並不能解決問題。其實現在有2468所重複的類定義(前367 ...)

我試了下是忽略警告:

-dontwarn org.bouncycastle.** 
-dontwarn org.apache.** 

這解決了問題!現在,APK的創建沒有任何錯誤。很好......但我對此沒有很好的感覺。 是否真的忽略了警告是解決此問題的最佳方法?這是處理這些問題的「同類」方式嗎?

當我在我的設備上運行APK並嘗試使用Dropbox功能時,應用程序崩潰。但這可以通過添加以下內容來解決:

-keep class org.** { *; } 
-keep class com.dropbox.** {*;} 

這可以保護Dropbox代碼不被obfruscated。但是我不確定這是否是正確的處理方法。

到目前爲止,該應用似乎對此配置正常工作。但有什麼辦法,我可以肯定的Proguard的沒有爆發什麼

感謝您非常(運行的所有應用功能的完整的beta測試每次應用程序創建?旁邊)!

回答

1

您的第三方庫正在處理兩次。在這種情況下將它們添加到另一個文件夾lib 並將以下行添加到proguard.cfg文件。

-injars lib/dropbox.jar 

Pro Guard Troubleshooting

Note: duplicate definition of program/library class

Your program jars or library jars contain multiple definitions of the listed classes. ProGuard continues processing as usual, only considering the first definitions. The warning may be an indication of some problem though, so it's advisable to remove the duplicates. A convenient way to do so is by specifying filters on the input jars or library jars. You can switch off these notes by specifying the -dontnote option.

正如在控制檯日誌的鏈接ProGuard Troubleshooting page表示,你應該在project.properties改變目標,以包含缺少的類的對象。在這種情況下:「android-18」或者「Google Inc.:Google APIs:18」。

您仍然可以在AndroidManifest.xml指定不同的minSdkVersion。

你應該指定任何-libraryjars選項,因爲Android編譯過程中就已經規定了你的選擇 - 你只看到重複類的警告了。實際上,Android構建過程也爲您指定了大多數其他選項。