2015-04-15 16 views
6

Tôi đang chơi với ProGuard trong dự án hiện tại của tôi và quyết định thử tối ưu hóa cấu hình android (với gradle):đúng ghi đè tùy chọn Proguard

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 

tôi không tìm thấy bất kỳ tài liệu rõ ràng về tối ưu hóa được thực hiện bởi Proguard và các phiên bản Android tương thích với chúng:

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* 

Chúng có được cập nhật nếu ứng dụng phiên bản sdk tối thiểu là 11?

Vì vậy, tôi quyết định ghi đè lên nó để cho nó một thử trong proguard-rules.pro:

-optimizations ** 
-printconfiguration "result.pro" 

Nhưng thức cấu hình không phải là như tôi mong đợi. Nó chứa tất cả các quy tắc được kết hợp:

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*,** 

Vậy làm thế nào tùy chọn có thể được ghi đè chính xác trong ProGuard? Hoặc có thể là dòng này bằng -optimizations **?

Trả lời

4

Tôi mất một chút thời gian thử và sai nhưng cuối cùng đã phát hiện ra. Để ghi đè tối ưu hóa ProGuard mặc định, ví dụ, áp dụng tất cả mọi thứ nhưng code/simplification/arithmetic thì:

  1. Thêm một dòng -optimizations đến file ProGuard bạn và sử dụng * để đại diện cho tất cả . Ví dụ: dòng sau:

    -optimizations !code/simplification/arithmetic,* 
    

    có nghĩa là "bật tất cả tối ưu hóa trừ code/simplification/arithmetic". Danh sách các tối ưu hóa khả dụng có sẵn trong the official website và bạn có thể sử dụng * để bật/tắt các lớp tối ưu hóa (ví dụ: !field/*).

  2. Bạn phải chắc chắn rằng tập tin quy tắc ProGuard của bạn được tải trước các tập tin ProGuard mặc định bằng cách trao đổi thứ tự của proguard-rules.progetDefaultProguardFile('proguard-android.txt') trong file Gradle để proguard-rules.pro xuất hiện đầu tiên:

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

Đầu ra sẽ trông giống như sau:

Optimizing... 
    Number of finalized classes:     68 
    Number of unboxed enum classes:    1 
    Number of vertically merged classes:   0 
    Number of horizontally merged classes:  3 
    Number of removed write-only fields:   0 (disabled) 
    Number of privatized fields:     58 
    Number of inlined constant fields:   375 
    Number of privatized methods:    13 
    Number of staticized methods:    37 
    Number of finalized methods:     210 
    Number of removed method parameters:   290 
    Number of inlined constant parameters:  236 
    Number of inlined constant return values: 239 
    Number of inlined short method calls:  35 
    Number of inlined unique method calls:  114 
    Number of inlined tail recursion calls:  0 
    Number of merged code blocks:    4 
    Number of variable peephole optimizations: 723 
    Number of arithmetic peephole optimizations: 10 
    Number of cast peephole optimizations:  0 
    Number of field peephole optimizations:  0 
    Number of branch peephole optimizations:  42 
    Number of string peephole optimizations:  35 
    Number of simplified instructions:   369 
    Number of removed instructions:    5019 
    Number of removed local variables:   154 
    Number of removed exception blocks:   0 
    Number of optimized local variable frames: 201 
Các vấn đề liên quan