回答

2

在你gradle這個文件中設置trueminifyEnabled

如果proguard的是在調試,發佈啓用或兩者

buildTypes { 
     release { 
      minifyEnabled true 
      proguardFiles 'proguard-rules.pro' 
     } 
     debug { 
      minifyEnabled false 
      proguardFiles 'proguard-rules.pro' 
     } 
    } 

您還可以設置proguardFiles來配置他可以定義,檢查此site到看看這個文檔,看看這個例子:

# Add project specific ProGuard rules here. 
# By default, the flags in this file are appended to flags specified 
# in /Users/balysv/Documents/Android/sdk/tools/proguard/proguard-android.txt 
# You can edit the include path and order by changing the ProGuard 
# include property in project.properties. 
# 
# For more details, see 
# http://developer.android.com/guide/developing/tools/proguard.html 

# Add any project specific keep options here: 

# If your project uses WebView with JS, uncomment the following 
# and specify the fully qualified class name to the JavaScript interface 
# class: 
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { 
# public *; 
#} 

-optimizationpasses 5 
-dontskipnonpubliclibraryclasses 
-dontskipnonpubliclibraryclassmembers 
-dontpreverify 
-verbose 

如果你想使用自定義字典爲代碼混淆,設置這個配置你的字典文件:

-obfuscationdictionary proguard-dic.txt 
-classobfuscationdictionary proguard-dic.txt 
-packageobfuscationdictionary proguard-dic.txt 

字典文件是要使用混淆你的代碼,每行1個標籤標籤一個簡單的文本文件。

+0

感謝又是怎樣的內容? – nuhkoca

+0

更新我的回答 –

+0

如何將這個配置應用於我的依賴關係? – nuhkoca

3

爲了使您的APK文件儘可能小,您應該啓用縮小以刪除發佈版本中未使用的代碼和資源。

ProGuard可用於檢測並從打包的應用程序中檢測並刪除未使用的類,字段,方法和屬性(包括來自包含的代碼庫中的未使用的類,字段,方法和屬性(使其成爲解決64k參考限制的重要工具))。

ProGuard還優化了字節碼,刪除了未使用的代碼指令,並用短名稱混淆了剩餘的類,字段和方法。混淆代碼使得APK很難進行反向工程,當您的應用使用安全敏感功能(如許可證驗證)時,這非常有價值。

例如,從文件的build.gradle下面的代碼片段啓用代碼萎縮發佈版本:

android { 
    buildTypes { 
     release { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile(‘proguard-android.txt'), 
        'proguard-rules.pro' 
     } 
    } 
    ... 
} 

Example of use from Proguard, from Android Studio

+0

感謝兄弟,我會看看它。好的,我如何使用proguard來處理我的依賴關係? – nuhkoca

相關問題