Shrink your code and resources —資源與程式碼的瘦身

以下內容來自
https://developer.android.com/studio/build/shrink-code
僅供參考


————— 


To make your APK file as small as possible, you should enable shrinking to remove unused code and resources in your release build. This page describes how to do that and how to specify what code and resources to keep or discard during the build.

要使您的APK文件盡可能小,您應該啟用收縮以刪除發布版本中未使用的代碼和資源。 此頁面介紹瞭如何執行此操作以及如何指定在構建期間要保留或丟棄的代碼和資源。

要讓 APK 檔案體積儘可能的小,在以發佈為目的的建構中可以開啟精簡功能來移除未使用的程式碼與資源。本頁說明其操作的方式,以及在建構的過程中,如何指定要保留或捨棄的資源與程式碼。


—————


Code shrinking is available with ProGuard, which detects and removes unused classes, fields, methods, and attributes from your packaged app, including those from included code libraries (making it a valuable tool for working around the 64k reference limit). ProGuard also optimizes the bytecode, removes unused code instructions, and obfuscates the remaining classes, fields, and methods with short names.

ProGuard提供代碼縮減功能,可以從打包的應用程序中檢測並刪除未使用的類,字段,方法和屬性,包括來自所包含代碼庫的那些(使其成為處理64k參考限制的有用工具)。 ProGuard還優化字節碼,刪除未使用的代碼指令,並使用短名稱對剩餘的類,字段和方法進行模糊處理。

程式碼精簡的功能採用的是 ProGuard;其會偵測並移除在已封裝的 App 中未使用到的類別、場域值(field)、方法或屬性;包括來自所引用的程式碼函式庫(使其成為處理 64k 參照限制時非常好用的工具)。ProGuard 亦能將 bytecode 最佳化、移除未使用到的程式碼指令,並以簡短名稱來混淆剩餘的類別、場域值或方法。


 —————


Resource shrinking is available with the Android plugin for Gradle, which removes unused resources from your packaged app, including unused resources in code libraries. It works in conjunction with code shrinking such that once unused code has been removed, any resources no longer referenced can be safely removed as well.

Gradle的Android插件提供了資源縮減功能,可以從打包的應用程序中刪除未使用的資源,包括代碼庫中未使用的資源。 它與代碼收縮一起工作,這樣一旦刪除了未使用的代碼,也可以安全地刪除不再引用的任何資源。

Gradle 的 Android 插件提供了資源精簡的功能;其可從已封裝的 App 中移除未使用到的資源;包括在程式碼函式庫中未使用到的資源。它與程式碼精簡一併進行;當未使用的程式碼被移除時,任何不再被參照的資源即能安全地被移除。


—————


Features in this document depend on:

 本文檔中的功能取決於:

     SDK工具25.0.10或更高版本
     適用於Gradle 3.0.0或更高版本的Android插件


 本文件中的功能須使用:
  。SDK 工具 25.0.10 或更高的版本
  。Gradle 的 Android 插件 3.0.0 或更高的版本


—————


Shrink your code

收縮你的代碼

精簡程式碼


—————


To enable code shrinking with ProGuard, add minifyEnabled true to the appropriate build type in your build.gradle file.

要使用ProGuard啟用代碼收縮,請將minifyEnabled true添加到build.gradle文件中的相應構建類型。

要啟用 ProGuard 的程式碼精簡功能,在 build.gradle 檔案中將 minifyEnabled true  新增至適當的建構型別中。


—————


Be aware that code shrinking slows down the build time, so you should avoid using it on your debug build if possible. However, it's important that you do enable code shrinking on your final APK used for testing, because it might introduce bugs if you do not sufficiently customize which code to keep.

請注意,代碼縮小會減慢構建時間,因此如果可能,應避免在調試版本中使用它。 但是,重要的是你要在用於測試的最終APK上啟用代碼縮減,因為如果你沒有充分定制要保留的代碼,它可能會引入錯誤。

請注意,程式碼精簡會拖慢建構時間,所以儘可能避免使用在除錯建構時。但,最重要的是,記得在最後用來測試的 APK 啟用程式碼精簡;因為如果未適當地設置所要保留的程式碼,則會出現錯誤。



—————


For example, the following snippet from a build.gradle file enables code shrinking for the release build:

例如,build.gradle文件中的以下代碼段可以使發布版本的代碼縮小:

例如,以下位於 build.gradle 檔案中的程式碼片段可啟用發佈建構時的程式碼精簡:


—————

[程式碼區塊]

—————


Note: Android Studio disables ProGuard when using Instant Run. If you need code shrinking for incremental builds, try the experimental Gradle shrinker.

注意:使用Instant Run時,Android Studio會禁用ProGuard。 如果您需要縮小增量構建的代碼,請嘗試使用實驗性Gradle收縮器。

注意:當使用 Instant Run 時,Android Studio 會將 ProGuard 的功能關閉。如果需要在增量建構時進行程式碼精簡,請嘗試尚在試驗階段的 Gradle 精簡家。


—————



In addition to the minifyEnabled property, the proguardFiles property specifies the following ProGuard rules:

除了minifyEnabled屬性之外,proguardFiles屬性還指定了以下ProGuard規則:

除了 minifyEnabled 屬性外,proguardFiles 屬性明確規定了下列 ProGuard 規則:


—————


 The getDefaultProguardFile('proguard-android.txt') method gets the default ProGuard settings file that is packaged with the Android plugin. When you build your project, the plugin creates a copy of the settings file in project-dir/build/intermediates/proguard-files/.

getDefaultProguardFile('proguard-android.txt')方法獲取與Android插件一起打包的默認ProGuard設置文件。 在構建項目時,插件會在project-dir / build / intermediates / proguard-files /中創建設置文件的副本。

getDefaultProguardFile('proguard-android.txt') 方法能取得 Android 插件隨附預設的 ProGuard 設定值檔案。當專案建構時,插件會在 project-dir/build/intermediates/proguard-files/ 目錄下建立一份該設定值檔案的副本。


—————



Tip: For even more code shrinking, try the proguard-android-optimize.txt file located in the same directory. It includes the same ProGuard rules, but with other optimizations that perform analysis at the bytecode level—inside and across methods—to reduce your APK size further and help it run faster.

提示:要獲得更多代碼縮減,請嘗試位於同一目錄中的proguard-android-optimize.txt文件。 它包含相同的ProGuard規則,但其他優化功能可在字節碼級別(內部和跨方法)執行分析,從而進一步降低APK大小並幫助其更快地運行。

提示:如程式碼要更精簡,可使用相同路徑中的 proguard-android-optimize.txt 檔案。除了包含相同的 ProGuard 規則外,還具有其它最佳化程序,如在 bytecode 層級進行分析—於方法內部與跨方法—將 APK 減至更小並幫助其執行的更快。

 

—————


The proguard-rules.pro file is where you can add custom ProGuard rules. By default, this file is located at the root of the module (next to the build.gradle file).

您可以在proguard-rules.pro文件中添加自定義ProGuard規則。 默認情況下,此文件位於模塊的根目錄(build.gradle文件旁邊)。

可以在 proguard-rules.pro 檔案中加入自定義的 ProGuard 規則。在預設的情形下,該檔案位於模組(module)的根目錄下(就在 build.gradle 檔案旁)。


—————


To add more ProGuard rules that are specific to each build variant, add another proguardFiles property in the corresponding productFlavor block. For example, the following Gradle file adds flavor2-rules.pro to the flavor2 product flavor. Now flavor2 uses all three ProGuard rules because those from the release block are also applied.

要添加特定於每個構建變體的更多ProGuard規則,請在相應的productFlavor塊中添加另一個proguardFiles屬性。 例如,以下Gradle文件將flavor2-rules.pro添加到flavor2產品風格中。 現在,flavor2使用了所有三個ProGuard規則,因為還應用了發布塊中的那些規則。

要針對每一個不同的建構加入額外的 ProGuard 規則, 可在對應的 productFlavor 區塊中,新增其它的 proguardFiles 屬性。例如,下面的 Gradle 檔案將 flavor2-rules.pro 新增至 flavor2 產品風格。這樣 flavor2 採用了全部三個 ProGuard 規則,因為那些來自
release 區塊的規則亦適用。





—————




[程式碼區塊]
 
—————



----- 2018/07/20(五) -----


With each build, ProGuard outputs the following files:

每次構建時,ProGuard都會輸出以下文件:

在每一次的建構,ProGuard 會產生以下的檔案:


—————



dump.txt
 
Describes the internal structure of all the class files in the APK.

描述APK中所有類文件的內部結構。

記載 APK 中所有類別檔案的內部結構。


—————



mapping.txt
 

Provides a translation between the original and obfuscated class, method, and field names.

提供原始類和混淆類,方法和字段名稱之間的轉換。

記載原始與混淆後的類別、方法與場域值名稱之間的轉換。 
  

—————




seeds.txt

Lists the classes and members that were not obfuscated.


列出未混淆的類和成員。

未混淆的類別與成員列表。


—————


usage.txt

 

Lists the code that was removed from the APK.

列出從APK中刪除的代碼。

自 APK 中移除的程式碼列表。 



—————


These files are saved at <module-name>/build/outputs/mapping/release/.

這些文件保存在<module-name> / build / outputs / mapping / release /中。

這些檔案存放於  <module-name>/build/outputs/mapping/release/。 


—————


 Customize which code to keep

自定義要保留的代碼

自定義要保留的程式碼


—————



For some situations, the default ProGuard configuration file (proguard-android.txt) is sufficient and ProGuard removes all—and only—the unused code. However, many situations are difficult for ProGuard to analyze correctly and it might remove code your app actually needs. Some examples of when it might incorrectly remove code include:
 
在某些情況下,默認的ProGuard配置文件(proguard-android.txt)就足夠了,ProGuard會刪除所有未使用的代碼。 但是,ProGuard很難正確分析許多情況,它可能會刪除您的應用實際需要的代碼。 可能錯誤刪除代碼的一些示例包括:

在某些情況下,可以直接使用預設的 ProGuard 配置檔案(proguard-android.txt);在預設的規則下,ProGuard 移除所有—且只移除—未用到的程式嗎。但在許多的情況下,對 ProGuard 來說,正確地分析是有困難的,App 實際需要的程式碼被移除是有可能發生的。有可能發生不正確地移除程式碼的一些實例包括:


—————


When your app references a class only from the AndroidManifest.xml file

當您的應用僅從AndroidManifest.xml文件引用類時

當 App 中有類別僅參照自 AndroidManifest.xml 檔案



—————


When your app calls a method from the Java Native Interface (JNI)

當您的應用程序從Java本機接口(JNI)調用方法時

當 App 中有方法是由 Java Native Interface (JNI) 呼叫


—————


When your app manipulates code at runtime (such as with reflection or introspection)

當您的應用程序在運行時操作代碼時(例如使用反射或內省)

當 App 在執行時期對程式碼進行操控(像是反射 (reflection) 或內省 (introspection))


—————


Testing your app should reveal any errors caused by inappropriately removed code, but you can also inspect what code was removed by reviewing the usage.txt output file saved in <module-name>/build/outputs/mapping/release/


測試您的應用程序應該顯示由於不正確刪除的代碼導致的任何錯誤,但您還可以通過查看保存在<module-name> / build / outputs / mapping / release /中的usage.txt輸出文件來檢查刪除的代碼。

App 測試可以發現任何因程式碼不適當的移除所造成的錯誤;但亦可檢視儲存在 <module-name>/build/outputs/mapping/release/ 所產生的 usage.txt 檔案來查驗被移除的程式碼。


—————



To fix errors and force ProGuard to keep certain code, add a -keep line in the ProGuard configuration file. For example:

要修復錯誤並強制ProGuard保留某些代碼,請在ProGuard配置文件中添加-keep行。 例如:

要修正錯誤並且強制 ProGuard 保留某些程式碼,可在 ProGuard 配置檔中加入 -keep。例如:


—————

[程式碼]

—————


Alternatively, you can add the @Keep annotation to the code you want to keep. Adding @Keep on a class keeps the entire class as-is. Adding it on a method or field will keep the method/field (and it's name) as well as the class name intact. Note that this annotation is available only when using the Annotations Support Library.

或者,您可以將@Keep註釋添加到要保留的代碼中。 在類上添加@Keep會使整個類保持原樣。 在方法或字段上添加它將保持方法/字段(及其名稱)以及類名完整。 請注意,僅在使用“註釋支持庫”時才能使用此註釋。

或者,可在想要保留的程式碼處加上 @Keep 註解。於類別處加上 @Keep 能將整個類別完整保留。而加在方法或場域值處則會原封不動地保留該方法/場域值(與其名稱)與類別名稱。請注意,只有在使用 Annotations Support Library 時,此註解方式才有作用。


—————



There are many considerations you should make when using the -keep option; for more information about customizing your configuration file, read the ProGuard Manual. The Troubleshooting section outlines other common problems you might encounter when your code gets stripped away.

使用-keep選項時應該考慮許多因素; 有關自定義配置文件的更多信息,請閱讀ProGuard手冊。 “故障排除”部分概述了在代碼被剝離後可能遇到的其他常見問題。

當使用 -keep 選項時必須多方考量;更多關於自定義配置檔的資訊,可參閱 ProGuard 手冊。問題排除一節中略述在程式碼被移除時可能會遇上其它常見問題。


—————
 

Decode an obfuscated stack trace

解碼混淆的堆棧跟踪

解碼混淆過的堆疊追踪


—————


After ProGuard shrinks your code, reading a stack trace is difficult (if not impossible) because the method names are obfuscated. Fortunately, ProGuard creates a mapping.txt file each time it runs, which shows the original class, method, and field names mapped to the obfuscated names. ProGuard saves the file in the app <module-name>/build/outputs/mapping/release/ directory.

在ProGuard縮小代碼之後,讀取堆棧跟踪很困難(如果不是不可能),因為方法名稱被混淆了。 幸運的是,ProGuard每次運行時都會創建一個mapping.txt文件,該文件顯示映射到模糊名稱的原始類,方法和字段名稱。 ProGuard將文件保存在app <module-name> / build / outputs / mapping / release /目錄中。

在 ProGuard 將程式碼精簡後,因為方法名稱已被混淆,以至於堆疊追踪的檢閱是一件困難的事(未必辦不到)。幸運的是,ProGurad 在每次執行時,都會建立 mapping.txt 檔案,該檔案記載原始的類別、方法與場域值名稱與混淆後的名稱之間的對應。ProGuard 將該檔案儲存在 <module-name>/build/outputs/mapping/release/ 目錄下。


—————


----- 2018/07/23(一) -----


Be aware that the mapping.txt file is overwritten every time you create a release build with ProGuard, so you must carefully save a copy each time you publish a new release. By retaining a copy of the mapping.txt file for each release build, you'll be able to debug a problem if a user submits an obfuscated stack trace from an older version of your app. 

請注意,每次使用ProGuard創建發布版本時都會覆蓋mapping.txt文件,因此每次發布新版本時都必須小心保存副本。 通過為每個發布版本保留mapping.txt文件的副本,如果用戶從舊版本的應用程序提交模糊堆棧跟踪,您將能夠調試問題。

請務必記得,在每次使用 ProGuard 建立發佈建構時,mapping.txt 檔案都會被覆寫;所以在每次發行新版本時必須保留一份。如果在每次發佈建構時都保留一份 mapping.txt 檔案,就能在使用者從較早的 App 版本提交混淆過的堆疊追踪時,進行除錯。



—————


When publishing your app on Google Play, you can upload the mapping.txt file for each version of your APK. Then Google Play will deobfuscate incoming stack traces from user-reported issues so you can review them in the Google Play Console. For more information, see the Help Center article about how to deobfuscate crash stack traces.

在Google Play上發布應用時,您可以為每個版本的APK上傳mapping.txt文件。 然後,Google Play會根據用戶報告的問題對傳入的堆棧跟踪進行反混淆處理,以便您可以在Google Play控制台中查看這些跟踪。 有關詳細信息,請參閱幫助中心文章,了解如何對崩潰堆棧跟踪進行反混淆處理。

當在 Google Play 上發行 App 時,每個版本的 APK 皆可上傳一份 mapping.txt。然後 Google Play 會反混淆使用者回報的堆疊追踪,這樣就可在 Google Play Console 檢視它們。關於更多的資訊,請參閱 Help Center 中關於反混淆當機堆疊追踪的文章。

 

—————
 




 
--- 待續








 

留言

這個網誌中的熱門文章

泰文子音與字形之間的變化

AUTOMAXX UP-5HX 使用心得