owned this note changed 7 years ago
Linked with GitHub

有效精簡 ProGuard 規則以達縮小 APK 之目的

tags: 2018, R1
位置: R1
講者:林容瑋



有效使用 ProGuard 就能有效縮小 APK

Shrink apk by removing unused code at run-time.
Obfuscate code to provide minimal protections against

What is ProGuard?

-keep class * e ClassA

Refinement steps & reasons

  1. 刪除重複的規則
  2. 刪除不必要的規則
  3. 使用"-whyareyoukeeping"
    • message from "-whyareyoutkeeping"
      1. is a library class/method/field
      2. is not been kept
      3. nothing
      4. is kept by a directive in the configuration

Reuslt

購物中心

  • APK Size: 16.3M -> 15.4M (-0.9M / -5.52%)

  • Method count: 114,476 ->

Recap

Refine ProGuard Configuration

Trouble shoot

What should be carefule?

  • 3-rd Library implements with reflection. f
    Glide: initialization

  • parsing data with field names
    Gson + Retorfit: JSON model

  • Anything implementation with static name

  • Add "-addconfigurationdebugging"
    (Only use in debug version)

    • message from "-addconfigurationdebugging"
      • ClassNotFoundException
      • NoSuchFieldException
      • NoSuchMethodException

R8

  • Porwered by Google
  • Default enable after Android Studio (3.3)
  • Java bytescode(.class) -> Dalvik bytecode(.dex)
  • ProGuard configuration is acceptable
  • Changes:
    • "-addconfigurationdebugging" will be ignored by default
    • Error detection while compiling

Q & A

Q:是否能在Kotlin用ProGuard? (大概是這意思吧?)

A:ProGuard主要針對Java處理

Select a repo