Hàng sọc trên google sheet

Để làm cho ứng dụng của bạn nhỏ nhất có thể, bạn nên kích hoạt tính năng thu nhỏ trong bản dựng phát hành của mình để xóa mã và tài nguyên không sử dụng. Khi kích hoạt tính năng thu nhỏ, bạn cũng được hưởng lợi từ việc che giấu, giúp rút ngắn tên của các lớp và thành viên của ứng dụng cũng như tối ưu hóa, áp dụng các chiến lược tích cực hơn để giảm thêm kích thước ứng dụng của bạn. Trang này mô tả cách R8 thực hiện các tác vụ thời gian biên dịch này cho dự án của bạn và cách bạn có thể tùy chỉnh chúng

Khi bạn xây dựng dự án của mình bằng plugin Android Gradle 3. 4. 0 trở lên, plugin không còn sử dụng ProGuard để thực hiện tối ưu hóa mã thời gian biên dịch. Thay vào đó, plugin hoạt động với trình biên dịch R8 để xử lý các tác vụ thời gian biên dịch sau

  • Thu hẹp mã (hoặc rung cây). phát hiện và loại bỏ một cách an toàn các lớp, trường, phương thức và thuộc tính không sử dụng khỏi ứng dụng của bạn cũng như các phần phụ thuộc vào thư viện của ứng dụng (làm cho ứng dụng này trở thành một công cụ có giá trị để làm việc trong giới hạn tham chiếu 64k). Ví dụ: nếu bạn chỉ sử dụng một số API phụ thuộc vào thư viện, việc thu nhỏ có thể xác định mã thư viện mà ứng dụng của bạn không sử dụng và chỉ xóa mã đó khỏi ứng dụng của bạn. Để tìm hiểu thêm, hãy chuyển đến phần về cách thu nhỏ mã của bạn
  • Thu hẹp tài nguyên. xóa các tài nguyên không sử dụng khỏi ứng dụng đóng gói của bạn, bao gồm các tài nguyên không sử dụng trong phần phụ thuộc thư viện của ứng dụng. Nó hoạt động cùng với việc thu nhỏ mã sao cho một khi mã không sử dụng đã bị xóa, mọi tài nguyên không còn được tham chiếu cũng có thể được xóa một cách an toàn. Để tìm hiểu thêm, hãy chuyển đến phần về cách thu nhỏ tài nguyên của bạn
  • che giấu. rút ngắn tên của các lớp và thành viên, dẫn đến giảm kích thước tệp DEX. Để tìm hiểu thêm, hãy chuyển đến phần về cách xáo trộn mã của bạn
  • Tối ưu hóa. kiểm tra và viết lại mã của bạn để tiếp tục giảm kích thước tệp DEX của ứng dụng. Ví dụ, nếu R8 phát hiện ra rằng nhánh
    // You can specify any path and filename.
    -printconfiguration ~/tmp/full-r8-config.txt
    
    4 cho câu lệnh if/else đã cho không bao giờ được sử dụng, R8 sẽ xóa mã cho nhánh
    // You can specify any path and filename.
    -printconfiguration ~/tmp/full-r8-config.txt
    
    4. Để tìm hiểu thêm, hãy chuyển đến phần về tối ưu hóa mã

Theo mặc định, khi xây dựng phiên bản phát hành của ứng dụng, R8 sẽ tự động thực hiện các tác vụ thời gian biên dịch được mô tả ở trên cho bạn. Tuy nhiên, bạn có thể vô hiệu hóa một số tác vụ hoặc tùy chỉnh hành vi của R8 thông qua các tệp quy tắc ProGuard. Trên thực tế, R8 hoạt động với tất cả các tệp quy tắc ProGuard hiện có của bạn, vì vậy, việc cập nhật plugin Android Gradle để sử dụng R8 không yêu cầu bạn thay đổi các quy tắc hiện có của mình

Cho phép thu nhỏ, che giấu và tối ưu hóa

Khi bạn sử dụng Android Studio 3. 4 hoặc plugin Android Gradle 3. 4. 0 trở lên, R8 là trình biên dịch mặc định chuyển đổi mã byte Java của dự án thành định dạng DEX chạy trên nền tảng Android. Tuy nhiên, khi bạn tạo một dự án mới bằng Android Studio, tính năng thu nhỏ, làm xáo trộn và tối ưu hóa mã không được bật theo mặc định. Đó là bởi vì những tối ưu hóa thời gian biên dịch này làm tăng thời gian xây dựng dự án của bạn và có thể gây ra lỗi nếu bạn không tùy chỉnh đầy đủ mã nào sẽ giữ lại

Vì vậy, tốt nhất là bật các tác vụ thời gian biên dịch này khi xây dựng phiên bản cuối cùng của ứng dụng mà bạn kiểm tra trước khi xuất bản. Để kích hoạt tính năng thu nhỏ, che giấu và tối ưu hóa, hãy bao gồm các nội dung sau trong tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6 cấp dự án của bạn

hấp dẫn

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

Kotlin

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}

Tệp cấu hình R8

R8 sử dụng các tệp quy tắc ProGuard để sửa đổi hành vi mặc định của nó và hiểu rõ hơn về cấu trúc ứng dụng của bạn, chẳng hạn như các lớp đóng vai trò là điểm nhập vào mã ứng dụng của bạn. Mặc dù bạn có thể sửa đổi một số tệp quy tắc này, nhưng một số quy tắc có thể được tạo tự động bởi các công cụ thời gian biên dịch, chẳng hạn như AAPT2 hoặc được kế thừa từ các thư viện phụ thuộc của ứng dụng của bạn. Bảng bên dưới mô tả các nguồn tệp quy tắc ProGuard mà R8 sử dụng

SourceLocationDescriptionAndroid Studio
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
7Khi bạn tạo một mô-đun mới bằng Android Studio, IDE sẽ tạo một tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
8 trong thư mục gốc của mô-đun đó

Theo mặc định, tệp này không áp dụng bất kỳ quy tắc nào. Vì vậy, hãy bao gồm các quy tắc ProGuard của riêng bạn ở đây, chẳng hạn như quy tắc giữ tùy chỉnh của bạn

Plugin Android GradleĐược tạo bởi plugin Android Gradle tại thời điểm biên dịch. Plugin Android Gradle tạo
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
9, bao gồm các quy tắc hữu ích cho hầu hết các dự án Android và cho phép chú thích
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
0

Theo mặc định, khi tạo một mô-đun mới bằng Android Studio, tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6 cấp mô-đun sẽ bao gồm tệp quy tắc này trong bản phát hành dành cho bạn

Ghi chú. Plugin Android Gradle bao gồm các tệp quy tắc ProGuard được xác định trước bổ sung, nhưng bạn nên sử dụng

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
9

Thư viện phụ thuộc thư viện AAR thư viện.
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
3

thư viện JAR.

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
4

Nếu một thư viện AAR được xuất bản với tệp quy tắc ProGuard của riêng nó và bạn bao gồm AAR đó dưới dạng phụ thuộc thời gian biên dịch, thì R8 sẽ tự động áp dụng các quy tắc của nó khi biên dịch dự án của bạn

Việc sử dụng các tệp quy tắc được đóng gói cùng với thư viện AAR sẽ hữu ích nếu cần có một số quy tắc lưu giữ nhất định để thư viện hoạt động bình thường—nghĩa là nhà phát triển thư viện đã thực hiện các bước khắc phục sự cố cho bạn

Tuy nhiên, bạn nên lưu ý rằng vì các quy tắc ProGuard là phần bổ sung nên không thể xóa một số quy tắc nhất định mà phần phụ thuộc thư viện AAR bao gồm và có thể ảnh hưởng đến quá trình biên dịch các phần khác trong ứng dụng của bạn. Ví dụ: nếu thư viện bao gồm quy tắc tắt tính năng tối ưu hóa mã, thì quy tắc đó sẽ tắt tính năng tối ưu hóa cho toàn bộ dự án của bạn

Android Asset Package Tool 2 (AAPT2)Sau khi xây dựng dự án của bạn với
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
5.
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
6AAPT2 tạo các quy tắc giữ dựa trên các tham chiếu đến các lớp trong tệp kê khai, bố cục và các tài nguyên ứng dụng khác của ứng dụng của bạn. Ví dụ: AAPT2 bao gồm quy tắc lưu giữ cho từng Hoạt động mà bạn đăng ký trong bảng kê khai của ứng dụng dưới dạng điểm vào. Các tệp cấu hình tùy chỉnh Theo mặc định, khi bạn tạo một mô-đun mới bằng Android Studio, IDE sẽ tạo
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
7 để bạn thêm các quy tắc của riêng mình. Bạn có thể bao gồm các cấu hình bổ sung và R8 áp dụng chúng tại thời điểm biên dịch

Khi bạn đặt thuộc tính

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
8 thành
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
9, R8 sẽ kết hợp các quy tắc từ tất cả các nguồn có sẵn được liệt kê ở trên. Đây là điều quan trọng cần nhớ khi bạn khắc phục sự cố với R8, vì các phụ thuộc thời gian biên dịch khác, chẳng hạn như phụ thuộc thư viện, có thể đưa ra các thay đổi đối với hành vi của R8 mà bạn không biết.

Để xuất một báo cáo đầy đủ về tất cả các quy tắc mà R8 áp dụng khi xây dựng dự án của bạn, hãy bao gồm phần sau trong tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
8 của mô-đun của bạn

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt

Bao gồm các cấu hình bổ sung

Khi bạn tạo một dự án hoặc mô-đun mới bằng Android Studio, IDE sẽ tạo một tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
7 để bạn đưa vào các quy tắc của riêng mình. Bạn cũng có thể bao gồm các quy tắc bổ sung từ các tệp khác bằng cách thêm chúng vào thuộc tính
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
12 trong tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6 của mô-đun của bạn

Ví dụ: bạn có thể thêm các quy tắc dành riêng cho từng biến thể bản dựng bằng cách thêm một thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
12 khác vào khối
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
15 tương ứng. Tệp Gradle sau đây thêm
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
16 vào hương vị sản phẩm
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
17. Bây giờ,
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
17 sử dụng cả ba quy tắc ProGuard vì những quy tắc từ khối
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
19 cũng được áp dụng

hấp dẫn

________số 8

Kotlin

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
1

Thu nhỏ mã của bạn

Thu nhỏ mã với R8 được bật theo mặc định khi bạn đặt thuộc tính

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
8 thành
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
9

Thu nhỏ mã (còn được gọi là lắc cây), là quá trình loại bỏ mã mà R8 xác định là không cần thiết trong thời gian chạy. Quá trình này có thể làm giảm đáng kể kích thước ứng dụng của bạn, ví dụ: nếu ứng dụng của bạn bao gồm nhiều thư viện phụ thuộc nhưng chỉ sử dụng một phần nhỏ chức năng của chúng

Để thu nhỏ mã ứng dụng của bạn, trước tiên R8 xác định tất cả các điểm nhập vào mã ứng dụng của bạn dựa trên tập hợp các tệp cấu hình được kết hợp. Các điểm vào này bao gồm tất cả các lớp mà nền tảng Android có thể sử dụng để mở Hoạt động hoặc dịch vụ của ứng dụng của bạn. Bắt đầu từ mỗi điểm vào, R8 kiểm tra mã ứng dụng của bạn để xây dựng biểu đồ về tất cả các phương thức, biến thành viên và các lớp khác mà ứng dụng của bạn có thể truy cập khi chạy. Mã không được kết nối với biểu đồ đó được coi là không thể truy cập được và có thể bị xóa khỏi ứng dụng

Hình 1 cho thấy một ứng dụng có phần phụ thuộc vào thư viện thời gian chạy. Trong khi kiểm tra mã của ứng dụng, R8 xác định rằng các phương pháp

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
32,
android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
33 và
android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
34 có thể truy cập được từ điểm nhập
android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
35. Tuy nhiên, lớp
android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
36 hoặc phương thức của nó
android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
37 không bao giờ được ứng dụng của bạn sử dụng trong thời gian chạy và R8 sẽ xóa mã đó khi thu nhỏ ứng dụng của bạn

Hàng sọc trên google sheet

Hình 1. Tại thời điểm biên dịch, R8 xây dựng biểu đồ dựa trên các quy tắc giữ kết hợp của dự án của bạn để xác định mã không thể truy cập

R8 xác định điểm vào thông qua quy tắc

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
38 trong tệp cấu hình R8 của dự án. Nghĩa là, giữ các quy tắc chỉ định các lớp mà R8 không nên loại bỏ khi thu nhỏ ứng dụng của bạn và R8 coi các lớp đó là điểm vào có thể có trong ứng dụng của bạn. Plugin Android Gradle và AAPT2 tự động tạo các quy tắc theo yêu cầu của hầu hết các dự án ứng dụng cho bạn, chẳng hạn như các hoạt động, chế độ xem và dịch vụ của ứng dụng. Tuy nhiên, nếu bạn cần tùy chỉnh hành vi mặc định này bằng các quy tắc giữ bổ sung, hãy đọc phần về cách tùy chỉnh mã nào sẽ giữ lại

Thay vào đó, nếu bạn chỉ quan tâm đến việc giảm kích thước tài nguyên của ứng dụng, hãy bỏ qua phần về cách thu nhỏ tài nguyên của bạn

Tùy chỉnh mã nào sẽ giữ lại

Đối với hầu hết các trường hợp, tệp quy tắc ProGuard mặc định (

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
39) là đủ để R8 chỉ xóa mã không sử dụng. Tuy nhiên, một số tình huống khiến R8 khó phân tích chính xác và nó có thể xóa mã mà ứng dụng của bạn thực sự cần. Một số ví dụ về thời điểm nó có thể xóa mã không chính xác bao gồm

  • Khi ứng dụng của bạn gọi một phương thức từ Giao diện gốc Java (JNI)
  • Khi ứng dụng của bạn tra cứu mã trong thời gian chạy (chẳng hạn như với sự phản chiếu)

Việc kiểm tra ứng dụng của bạn sẽ cho thấy bất kỳ lỗi nào do mã bị xóa không phù hợp gây ra, nhưng bạn cũng có thể kiểm tra xem mã nào đã bị xóa bằng cách tạo báo cáo về mã đã xóa

Để sửa lỗi và buộc R8 giữ một số mã nhất định, hãy thêm một dòng

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
38 vào tệp quy tắc ProGuard. Ví dụ

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
3

Ngoài ra, bạn có thể thêm chú thích

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
31 vào mã mà bạn muốn giữ lại. Việc thêm
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
31 vào một lớp sẽ giữ nguyên trạng của toàn bộ lớp. Thêm nó vào một phương thức hoặc trường sẽ giữ nguyên phương thức/trường (và tên của nó) cũng như tên lớp. Lưu ý rằng chú thích này chỉ khả dụng khi sử dụng Thư viện chú thích AndroidX và khi bạn bao gồm tệp quy tắc ProGuard được đóng gói cùng với plugin Android Gradle, như được mô tả trong phần về cách bật tính năng thu nhỏ

Bạn nên cân nhắc nhiều điều khi sử dụng tùy chọn

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}
38; . Phần Khắc phục sự cố phác thảo các vấn đề phổ biến khác mà bạn có thể gặp phải khi mã của bạn bị loại bỏ

Tách thư viện bản địa

Theo mặc định, các thư viện mã gốc bị loại bỏ trong các bản phát hành của ứng dụng của bạn. Quá trình loại bỏ này bao gồm việc xóa bảng biểu tượng và thông tin gỡ lỗi có trong bất kỳ thư viện gốc nào được ứng dụng của bạn sử dụng. Việc loại bỏ các thư viện mã gốc giúp tiết kiệm kích thước đáng kể;

Hỗ trợ sự cố gốc

Google Play Console báo cáo sự cố tự nhiên trong Android Vitals. Với một vài bước, bạn có thể tạo và tải lên tệp biểu tượng gỡ lỗi gốc cho ứng dụng của mình. Tệp này bật dấu vết ngăn xếp sự cố gốc được tượng trưng (bao gồm tên lớp và hàm) trong Android Vitals để giúp bạn gỡ lỗi ứng dụng của mình trong sản xuất. Các bước này khác nhau tùy thuộc vào phiên bản plugin Android Gradle được sử dụng trong dự án của bạn và đầu ra bản dựng của dự án của bạn

Ghi chú. Để tự khôi phục tên ký hiệu trong các báo cáo sự cố, hãy sử dụng công cụ ndk-stack đi kèm với NDK của Android

Plugin Android Gradle phiên bản 4. 1 hoặc muộn hơn

Nếu dự án của bạn xây dựng Android App Bundle, thì bạn có thể tự động đưa tệp biểu tượng gỡ lỗi gốc vào đó. Để đưa tệp này vào bản phát hành, hãy thêm phần sau vào tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6 của ứng dụng của bạn

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
35

Chọn cấp biểu tượng gỡ lỗi từ các mục sau

  • Sử dụng
    android {
        buildTypes {
            getByName("release") {
                // Enables code shrinking, obfuscation, and optimization for only
                // your project's release build type.
                isMinifyEnabled = true
    
                // Enables resource shrinking, which is performed by the
                // Android Gradle plugin.
                isShrinkResources = true
    
                // Includes the default ProGuard rules files that are packaged with
                // the Android Gradle plugin. To learn more, go to the section about
                // R8 configuration files.
                proguardFiles(
                    getDefaultProguardFile("proguard-android-optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }
        ...
    }
    
    36 để lấy tên hàm trong dấu vết ngăn xếp tượng trưng của Play Console. Cấp độ này hỗ trợ bia mộ
  • Sử dụng
    android {
        buildTypes {
            getByName("release") {
                // Enables code shrinking, obfuscation, and optimization for only
                // your project's release build type.
                isMinifyEnabled = true
    
                // Enables resource shrinking, which is performed by the
                // Android Gradle plugin.
                isShrinkResources = true
    
                // Includes the default ProGuard rules files that are packaged with
                // the Android Gradle plugin. To learn more, go to the section about
                // R8 configuration files.
                proguardFiles(
                    getDefaultProguardFile("proguard-android-optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }
        ...
    }
    
    37 để lấy tên hàm, tệp và số dòng trong dấu vết ngăn xếp tượng trưng của Play Console
Ghi chú. Có giới hạn 300 MB cho tệp biểu tượng gỡ lỗi gốc. Nếu dấu vết biểu tượng gỡ lỗi của bạn quá lớn, hãy sử dụng
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
36 thay vì
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
37 để giảm kích thước tệp

Nếu dự án của bạn tạo APK, hãy sử dụng cài đặt bản dựng

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6 được hiển thị trước đó để tạo riêng tệp biểu tượng gỡ lỗi gốc. Tải thủ công tệp biểu tượng gỡ lỗi gốc lên Google Play Console. Là một phần của quá trình xây dựng, plugin Android Gradle xuất tệp này ở vị trí dự án sau

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
31

Plugin Android Gradle phiên bản 4. 0 hoặc cũ hơn (và các hệ thống xây dựng khác)

Là một phần của quá trình xây dựng, plugin Android Gradle giữ một bản sao của các thư viện chưa bị xóa trong thư mục dự án. Cấu trúc thư mục này tương tự như sau

______43Ghi chú. Nếu bạn sử dụng một hệ thống bản dựng khác, bạn có thể sửa đổi hệ thống đó để lưu trữ các thư viện chưa bị xóa trong một thư mục có cấu trúc bắt buộc
  1. Nén nội dung của thư mục này

    // You can specify any path and filename.
    -printconfiguration ~/tmp/full-r8-config.txt
    
    3
  2. Tải tệp

    // You can specify any path and filename.
    -printconfiguration ~/tmp/full-r8-config.txt
    
    32 lên Google Play Console theo cách thủ công

Ghi chú. Có giới hạn 300 MB cho tệp biểu tượng gỡ lỗi. Nếu tệp của bạn quá lớn, thì có thể là do tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
33 của bạn chứa bảng ký hiệu (tên hàm) và cả thông tin gỡ lỗi DWARF (tên tệp và dòng mã). Chúng không cần thiết để tượng trưng cho mã của bạn. Bạn có thể loại bỏ chúng bằng cách chạy lệnh sau.
______534
trong đó
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
35 trỏ đến phiên bản cụ thể cho ABI mà bạn đang loại bỏ (ví dụ:
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
36).

Thu hẹp tài nguyên của bạn

Thu hẹp tài nguyên chỉ hoạt động cùng với thu hẹp mã. Sau khi trình thu gọn mã loại bỏ tất cả mã không sử dụng, trình thu hẹp tài nguyên có thể xác định tài nguyên nào mà ứng dụng vẫn sử dụng. Điều này đặc biệt đúng khi bạn thêm các thư viện mã bao gồm các tài nguyên—bạn phải xóa mã thư viện không sử dụng để các tài nguyên thư viện trở thành không được ước tính và do đó, có thể loại bỏ bằng trình thu hẹp tài nguyên

Để kích hoạt thu hẹp tài nguyên, hãy đặt thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
37 thành
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
9 trong tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6 của bạn (cùng với
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
8 để thu hẹp mã). Ví dụ

hấp dẫn

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
5

Kotlin

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6

Nếu bạn chưa xây dựng ứng dụng của mình bằng cách sử dụng

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles
                getDefaultProguardFile('proguard-android-optimize.txt'),
                // List additional ProGuard rules for the given build type here. By default,
                // Android Studio creates and includes an empty rules file for you (located
                // at the root directory of each module).
                'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        flavor1 {
            ...
        }
        flavor2 {
            proguardFile 'flavor2-rules.pro'
        }
    }
}
8 để thu nhỏ mã, thì hãy thử cách đó trước khi bật
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
37, vì bạn có thể cần chỉnh sửa tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
8 của mình để giữ các lớp hoặc phương thức được tạo hoặc gọi động trước khi bạn bắt đầu xóa tài nguyên

Tùy chỉnh tài nguyên nào cần giữ lại

Nếu có các tài nguyên cụ thể mà bạn muốn giữ lại hoặc loại bỏ, hãy tạo một tệp XML trong dự án của bạn bằng thẻ

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
54 và chỉ định từng tài nguyên cần giữ trong thuộc tính
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
55 và từng tài nguyên cần loại bỏ trong thuộc tính
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
56. Cả hai thuộc tính đều chấp nhận danh sách tên tài nguyên được phân tách bằng dấu phẩy. Bạn có thể sử dụng ký tự dấu hoa thị làm ký tự đại diện

Ví dụ

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
0

Lưu tệp này vào tài nguyên dự án của bạn, ví dụ: tại

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
57. Bản dựng không đóng gói tệp này vào ứng dụng của bạn

Việc chỉ định tài nguyên nào cần loại bỏ có vẻ ngớ ngẩn khi thay vào đó bạn có thể xóa chúng, nhưng điều này có thể hữu ích khi sử dụng các biến thể bản dựng. Ví dụ: bạn có thể đặt tất cả các tài nguyên của mình vào thư mục dự án chung, sau đó tạo một tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
58 khác cho mỗi biến thể bản dựng khi bạn biết rằng một tài nguyên nhất định dường như được sử dụng trong mã (và do đó không bị xóa bởi trình thu nhỏ) nhưng bạn biết . Cũng có thể các công cụ xây dựng đã xác định sai tài nguyên khi cần, điều này có thể xảy ra do trình biên dịch thêm ID tài nguyên nội tuyến và sau đó bộ phân tích tài nguyên có thể không biết sự khác biệt giữa tài nguyên được tham chiếu thực sự và giá trị số nguyên trong mã xảy ra với

Cho phép kiểm tra tham chiếu nghiêm ngặt

Thông thường, trình thu hẹp tài nguyên có thể xác định chính xác liệu tài nguyên có được sử dụng hay không. Tuy nhiên, nếu mã của bạn thực hiện lệnh gọi tới

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
59 (hoặc nếu bất kỳ thư viện nào của bạn thực hiện việc đó—thư viện AppCompat thực hiện), điều đó có nghĩa là mã của bạn đang tra cứu tên tài nguyên dựa trên các chuỗi được tạo động. Khi bạn thực hiện việc này, trình thu hẹp tài nguyên sẽ hoạt động theo cách phòng thủ theo mặc định và đánh dấu tất cả các tài nguyên có định dạng tên phù hợp là có khả năng được sử dụng và không có sẵn để xóa

Ví dụ: đoạn mã sau khiến tất cả các tài nguyên có tiền tố

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
60 được đánh dấu là đã sử dụng

Kotlin

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
1

Java

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
2

Trình thu hẹp tài nguyên cũng xem xét tất cả các hằng số chuỗi trong mã của bạn, cũng như các tài nguyên

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
61 khác nhau, tìm kiếm các URL tài nguyên ở định dạng tương tự như
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
62. Nếu nó tìm thấy các chuỗi như thế này hoặc các chuỗi khác trông giống như chúng có thể được sử dụng để tạo các URL như thế này, thì nó sẽ không xóa chúng

Đây là những ví dụ về chế độ thu nhỏ an toàn được bật theo mặc định. Tuy nhiên, bạn có thể tắt tính năng xử lý "tốt hơn là an toàn" này và chỉ định rằng trình thu hẹp tài nguyên chỉ giữ lại những tài nguyên mà nó chắc chắn được sử dụng. Để thực hiện việc này, hãy đặt

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
63 thành
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
64 trong tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
58, như sau

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
3

Nếu bạn bật chế độ thu hẹp nghiêm ngặt và mã của bạn cũng tham chiếu đến các tài nguyên có chuỗi được tạo động, như minh họa ở trên, thì bạn phải giữ các tài nguyên đó theo cách thủ công bằng cách sử dụng thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
55

Xóa các tài nguyên thay thế không sử dụng

Trình thu hẹp tài nguyên Gradle chỉ xóa các tài nguyên không được tham chiếu bởi mã ứng dụng của bạn, điều đó có nghĩa là nó sẽ không xóa các tài nguyên thay thế cho các cấu hình thiết bị khác nhau. Nếu cần, bạn có thể sử dụng thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
67 của plugin Android Gradle để xóa các tệp tài nguyên thay thế mà ứng dụng của bạn không cần

Ví dụ: nếu bạn đang sử dụng một thư viện bao gồm các tài nguyên ngôn ngữ (chẳng hạn như AppCompat hoặc Google Play Services), thì ứng dụng của bạn sẽ bao gồm tất cả các chuỗi ngôn ngữ đã dịch cho thư trong các thư viện đó cho dù phần còn lại của ứng dụng của bạn được dịch sang cùng ngôn ngữ hay . Nếu bạn chỉ muốn giữ lại những ngôn ngữ mà ứng dụng của bạn hỗ trợ chính thức, bạn có thể chỉ định những ngôn ngữ đó bằng thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
68. Mọi tài nguyên cho các ngôn ngữ không được chỉ định sẽ bị xóa

Đoạn mã sau đây cho biết cách giới hạn tài nguyên ngôn ngữ của bạn chỉ bằng tiếng Anh và tiếng Pháp

hấp dẫn

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
4

Kotlin

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
5

Khi phát hành ứng dụng bằng định dạng Android App Bundle, theo mặc định, chỉ những ngôn ngữ được định cấu hình trên thiết bị của người dùng mới được tải xuống khi cài đặt ứng dụng. Tương tự, chỉ những tài nguyên phù hợp với mật độ màn hình của thiết bị và các thư viện gốc phù hợp với ABI của thiết bị mới được đưa vào tải xuống. Để biết thêm thông tin, hãy tham khảo cấu hình Android App Bundle

Đối với các ứng dụng cũ phát hành bằng APK (được tạo trước tháng 8 năm 2021), bạn có thể tùy chỉnh mật độ màn hình hoặc tài nguyên ABI nào để đưa vào APK của mình bằng cách tạo nhiều APK mà mỗi APK nhắm mục tiêu đến một cấu hình thiết bị khác nhau

Hợp nhất các tài nguyên trùng lặp

Theo mặc định, Gradle cũng hợp nhất các tài nguyên có tên giống hệt nhau, chẳng hạn như các tài nguyên có thể kéo có cùng tên có thể nằm trong các thư mục tài nguyên khác nhau. Hành vi này không được kiểm soát bởi thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
37 và không thể bị vô hiệu hóa, vì cần phải tránh lỗi khi nhiều tài nguyên khớp với tên mà mã của bạn đang tra cứu

Việc hợp nhất tài nguyên chỉ xảy ra khi hai hoặc nhiều tệp chia sẻ tên, loại và vòng loại tài nguyên giống hệt nhau. Gradle chọn tệp mà nó coi là lựa chọn tốt nhất trong số các tệp trùng lặp (dựa trên thứ tự ưu tiên được mô tả bên dưới) và chỉ chuyển một tài nguyên đó cho AAPT để phân phối trong phần tạo tác cuối cùng

Gradle tìm kiếm các tài nguyên trùng lặp ở các vị trí sau

  • Các tài nguyên chính, được liên kết với bộ nguồn chính, thường nằm ở
    android {
        buildTypes {
            getByName("release") {
                // Enables code shrinking, obfuscation, and optimization for only
                // your project's release build type.
                isMinifyEnabled = true
    
                // Enables resource shrinking, which is performed by the
                // Android Gradle plugin.
                isShrinkResources = true
    
                // Includes the default ProGuard rules files that are packaged with
                // the Android Gradle plugin. To learn more, go to the section about
                // R8 configuration files.
                proguardFiles(
                    getDefaultProguardFile("proguard-android-optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }
        ...
    }
    
    00
  • Các lớp phủ biến thể, từ loại bản dựng và hương vị bản dựng
  • Các phụ thuộc dự án thư viện

Gradle hợp nhất các tài nguyên trùng lặp theo thứ tự ưu tiên xếp tầng sau

Phụ thuộc → Chính → Hương vị bản dựng → Loại bản dựng

Ví dụ: nếu một tài nguyên trùng lặp xuất hiện trong cả tài nguyên chính và hương vị bản dựng của bạn, thì Gradle sẽ chọn tài nguyên đó trong hương vị bản dựng

Nếu các tài nguyên giống hệt nhau xuất hiện trong cùng một bộ nguồn, Gradle không thể hợp nhất chúng và phát ra lỗi hợp nhất tài nguyên. Điều này có thể xảy ra nếu bạn xác định nhiều bộ nguồn trong thuộc tính

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
01 của tệp
// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
6—ví dụ: nếu cả
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
00 và
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
04 đều chứa các tài nguyên giống hệt nhau

Làm xáo trộn mã của bạn

Mục đích của việc che giấu là để giảm kích thước ứng dụng của bạn bằng cách rút ngắn tên của các lớp, phương thức và trường của ứng dụng của bạn. Sau đây là một ví dụ về obfuscation sử dụng R8

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
6

Mặc dù che giấu mã không xóa mã khỏi ứng dụng của bạn, nhưng bạn có thể thấy kích thước tiết kiệm đáng kể trong các ứng dụng có tệp DEX lập chỉ mục cho nhiều lớp, phương thức và trường. Tuy nhiên, khi xáo trộn đổi tên các phần khác nhau trong mã của bạn, một số tác vụ nhất định, chẳng hạn như kiểm tra dấu vết ngăn xếp, yêu cầu các công cụ bổ sung. Để hiểu về dấu vết ngăn xếp của bạn sau khi làm xáo trộn, hãy đọc phần về cách giải mã dấu vết ngăn xếp bị xáo trộn

Ngoài ra, nếu mã của bạn dựa vào cách đặt tên có thể đoán trước cho các phương thức và lớp của ứng dụng—ví dụ: khi sử dụng phản chiếu, bạn nên coi các chữ ký đó là điểm nhập và chỉ định giữ quy tắc cho chúng, như được mô tả trong phần về cách tùy chỉnh mã nào để . Các quy tắc giữ nguyên đó yêu cầu R8 không chỉ giữ mã đó trong DEX cuối cùng của ứng dụng mà còn giữ lại cách đặt tên ban đầu của nó

Giải mã dấu vết ngăn xếp bị xáo trộn

Sau khi R8 làm xáo trộn mã của bạn, việc hiểu dấu vết ngăn xếp rất khó (nếu không muốn nói là không thể) vì tên của các lớp và phương thức có thể đã bị thay đổi. Để có được dấu vết ngăn xếp ban đầu, bạn nên truy xuất lại dấu vết ngăn xếp

tối ưu hóa mã

Để thu nhỏ ứng dụng của bạn hơn nữa, R8 kiểm tra mã của bạn ở cấp độ sâu hơn để loại bỏ nhiều mã không sử dụng hơn hoặc, nếu có thể, viết lại mã của bạn để làm cho mã ít dài dòng hơn. Sau đây là một vài ví dụ về các tối ưu hóa như vậy

  • Nếu mã của bạn không bao giờ lấy nhánh
    // You can specify any path and filename.
    -printconfiguration ~/tmp/full-r8-config.txt
    
    4 cho câu lệnh if/else đã cho, R8 có thể xóa mã cho nhánh
    // You can specify any path and filename.
    -printconfiguration ~/tmp/full-r8-config.txt
    
    4
  • Nếu mã của bạn chỉ gọi một phương thức ở một nơi duy nhất, R8 có thể loại bỏ phương thức đó và đặt nội tuyến nó tại một trang web gọi duy nhất
  • Nếu R8 xác định rằng một lớp chỉ có một lớp con duy nhất và bản thân lớp đó không được khởi tạo (ví dụ: một lớp cơ sở trừu tượng chỉ được sử dụng bởi một lớp triển khai cụ thể), thì R8 có thể kết hợp hai lớp và loại bỏ một lớp khỏi ứng dụng
  • Để tìm hiểu thêm, hãy đọc các bài đăng trên blog về tối ưu hóa R8 của Jake Wharton

R8 không cho phép bạn tắt hoặc bật tối ưu hóa rời rạc hoặc sửa đổi hành vi của tối ưu hóa. Trên thực tế, R8 bỏ qua bất kỳ quy tắc ProGuard nào cố gắng sửa đổi các tối ưu hóa mặc định, chẳng hạn như

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
07 và
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
08. Hạn chế này rất quan trọng vì khi R8 tiếp tục cải tiến, việc duy trì hành vi tiêu chuẩn để tối ưu hóa sẽ giúp nhóm Android Studio dễ dàng khắc phục sự cố và giải quyết mọi sự cố mà bạn có thể gặp phải

Lưu ý rằng việc bật tối ưu hóa sẽ thay đổi dấu vết ngăn xếp cho ứng dụng của bạn. Ví dụ: nội tuyến sẽ loại bỏ các khung ngăn xếp. Xem phần truy xuất để tìm hiểu cách lấy dấu vết ngăn xếp ban đầu

Cho phép tối ưu hóa tích cực hơn

R8 bao gồm một tập hợp các tối ưu hóa bổ sung không được bật theo mặc định. Bạn có thể kích hoạt các tối ưu hóa bổ sung này bằng cách đưa các nội dung sau vào tệp

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
09 của dự án

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
7

Do các tối ưu hóa bổ sung làm cho R8 hoạt động khác với ProGuard nên chúng có thể yêu cầu bạn đưa vào các quy tắc ProGuard bổ sung để tránh các sự cố về thời gian chạy. Ví dụ: giả sử rằng mã của bạn tham chiếu đến một lớp thông qua Java Reflection API. Theo mặc định, R8 giả định rằng bạn có ý định kiểm tra và thao tác các đối tượng của lớp đó trong thời gian chạy—ngay cả khi mã của bạn thực sự không—và nó sẽ tự động giữ lớp đó và bộ khởi tạo tĩnh của nó

Tuy nhiên, khi sử dụng “chế độ đầy đủ”, R8 không đưa ra giả định này và nếu R8 khẳng định rằng mã của bạn không bao giờ sử dụng lớp trong thời gian chạy, thì nó sẽ xóa lớp khỏi DEX cuối cùng của ứng dụng của bạn. Nghĩa là, nếu bạn muốn giữ lớp và trình khởi tạo tĩnh của nó, bạn cần đưa quy tắc giữ vào tệp quy tắc của mình để thực hiện điều đó

Nếu bạn gặp phải bất kỳ sự cố nào khi sử dụng “chế độ đầy đủ” của R8, hãy tham khảo trang Câu hỏi thường gặp về R8 để biết giải pháp khả thi. Nếu bạn không thể giải quyết vấn đề, vui lòng báo cáo lỗi

Truy xuất dấu vết ngăn xếp

Mã được xử lý bởi R8 được thay đổi theo nhiều cách khác nhau có thể làm cho dấu vết ngăn xếp khó hiểu hơn vì dấu vết ngăn xếp sẽ không tương ứng chính xác với mã nguồn. Đây có thể là trường hợp thay đổi số dòng khi thông tin gỡ lỗi không được lưu giữ. Nó có thể là do tối ưu hóa như nội tuyến và phác thảo. Đóng góp lớn nhất là obfuscation trong đó ngay cả các lớp và phương thức sẽ thay đổi tên

Để khôi phục dấu vết ngăn xếp ban đầu, R8 cung cấp công cụ dòng lệnh truy xuất, đi kèm với gói công cụ dòng lệnh

Để hỗ trợ truy xuất dấu vết ngăn xếp của ứng dụng, bạn nên đảm bảo bản dựng lưu giữ đủ thông tin để truy xuất bằng cách thêm các quy tắc sau vào tệp

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
8 của mô-đun của bạn

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
8

Thuộc tính

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
11 giữ lại thông tin vị trí trong các phương thức sao cho những vị trí đó được in trong dấu vết ngăn xếp. Thuộc tính
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
12 đảm bảo rằng tất cả thời gian chạy tiềm năng thực sự in thông tin vị trí. Lệnh
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
13 đặt tên tệp nguồn trong dấu vết ngăn xếp thành chỉ
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
12. Tên tệp nguồn gốc thực tế không bắt buộc khi truy xuất vì tệp ánh xạ chứa tệp nguồn gốc

R8 tạo một tệp

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
15 mỗi khi nó chạy, tệp này chứa thông tin cần thiết để ánh xạ dấu vết ngăn xếp trở lại dấu vết ngăn xếp ban đầu. Android Studio lưu tệp trong thư mục
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
16

Thận trọng. Tệp
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
15 mà studio tạo ra bị ghi đè mỗi khi bạn xây dựng dự án của mình, vì vậy bạn phải cẩn thận lưu một bản sao mỗi khi xuất bản bản phát hành mới. Bằng cách giữ lại một bản sao của tệp
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
15 cho mỗi bản phát hành, bạn sẽ có thể truy xuất nếu người dùng gửi theo dõi ngăn xếp bị xáo trộn từ phiên bản cũ hơn của ứng dụng của bạn

Khi xuất bản ứng dụng của bạn trên Google Play, bạn có thể tải tệp

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
15 lên cho từng phiên bản ứng dụng của mình. Khi xuất bản bằng Android App Bundle, tệp này sẽ tự động được đưa vào như một phần của nội dung gói ứng dụng. Sau đó, Google Play sẽ truy xuất dấu vết ngăn xếp đến từ các sự cố do người dùng báo cáo để bạn có thể xem lại chúng trong Play Console. Để biết thêm thông tin, hãy xem bài viết trong Trung tâm trợ giúp về cách giải mã dấu vết ngăn xếp sự cố

Khắc phục sự cố với R8

Phần này mô tả một số chiến lược khắc phục sự cố khi kích hoạt tính năng thu nhỏ, che giấu và tối ưu hóa bằng R8. Nếu bạn không tìm thấy giải pháp cho vấn đề của mình bên dưới, hãy đọc thêm trang Câu hỏi thường gặp về R8 và hướng dẫn khắc phục sự cố của ProGuard

Tạo báo cáo về mã đã xóa (hoặc giữ)

Để giúp bạn khắc phục sự cố nhất định của R8, có thể hữu ích khi xem báo cáo về tất cả mã mà R8 đã xóa khỏi ứng dụng của bạn. Đối với mỗi mô-đun mà bạn muốn tạo báo cáo này, hãy thêm

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
20 vào tệp quy tắc tùy chỉnh của bạn. Khi bạn bật R8 và xây dựng ứng dụng của mình, R8 sẽ xuất một báo cáo có đường dẫn và tên tệp mà bạn đã chỉ định. Báo cáo về mã bị xóa trông giống như sau

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
9

Thay vào đó, nếu bạn muốn xem báo cáo về các điểm vào mà R8 xác định từ các quy tắc giữ nguyên của dự án của bạn, hãy bao gồm

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
21 trong tệp quy tắc tùy chỉnh của bạn. Khi bạn bật R8 và xây dựng ứng dụng của mình, R8 sẽ xuất một báo cáo có đường dẫn và tên tệp mà bạn đã chỉ định. Báo cáo về các điểm vào được giữ trông tương tự như sau

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
0

Khắc phục sự cố thu hẹp tài nguyên

Khi bạn thu nhỏ tài nguyên, cửa sổ Bản dựng

Hàng sọc trên google sheet
hiển thị tóm tắt các tài nguyên đã bị xóa khỏi ứng dụng. (Trước tiên, bạn cần nhấp vào Chuyển đổi chế độ xem
Hàng sọc trên google sheet
ở phía bên trái của cửa sổ để hiển thị đầu ra văn bản chi tiết từ Gradle. ) Ví dụ.

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
1

Gradle cũng tạo một tệp chẩn đoán có tên là

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
22 trong
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
23 (cùng thư mục với các tệp đầu ra của ProGuard). Tệp này bao gồm các chi tiết như tài nguyên nào tham chiếu đến các tài nguyên khác và tài nguyên nào được sử dụng hoặc xóa

Ví dụ: để tìm hiểu lý do tại sao

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
24 vẫn còn trong ứng dụng của bạn, hãy mở tệp
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
22 và tìm kiếm tên tệp đó. Bạn có thể thấy rằng nó được tham chiếu từ một nguồn khác, như sau

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
2

Bây giờ bạn cần biết tại sao có thể truy cập

android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
26—và nếu tìm kiếm từ trên cao, bạn sẽ thấy tài nguyên đó được liệt kê trong phần "Các tài nguyên có thể truy cập gốc là. ". Điều này có nghĩa là có một tham chiếu mã tới
android {
    buildTypes {
        getByName("release") {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            isMinifyEnabled = true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            isShrinkResources = true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    ...
}
27 (nghĩa là R của nó. drawable ID đã được tìm thấy trong mã có thể truy cập)

Nếu bạn không sử dụng kiểm tra nghiêm ngặt, ID tài nguyên có thể được đánh dấu là có thể truy cập nếu có các hằng số chuỗi trông giống như chúng có thể được sử dụng để tạo tên tài nguyên cho các tài nguyên được tải động. Trong trường hợp đó, nếu bạn tìm kiếm đầu ra bản dựng cho tên tài nguyên, bạn có thể tìm thấy một thông báo như thế này

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
3

Nếu bạn thấy một trong các chuỗi này và bạn chắc chắn rằng chuỗi đó không được sử dụng để tải động tài nguyên đã cho, bạn có thể sử dụng thuộc tính

// You can specify any path and filename.
-printconfiguration ~/tmp/full-r8-config.txt
56 để thông báo cho hệ thống xây dựng xóa nó, như được mô tả trong phần về cách tùy chỉnh chuỗi đó.