Hướng dẫn di chuyển SDK dành cho người dùng Android 3.0
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Di chuyển sang Kotlin 2.0
Nếu đang sử dụng Consumer SDK cho Android 3.0 trở lên, bạn phải nâng cấp lên Kotlin 2.0. Trang này cung cấp hướng dẫn để giúp bạn dễ dàng chuyển đổi sang phiên bản Kotlin mới.
Khả năng tương thích giữa Gradle và AGP
Kotlin 2.0 có các yêu cầu về phiên bản tối thiểu và tối đa cho phiên bản Gradle và trình bổ trợ Android cho Gradle (AGP). Đảm bảo rằng dự án của bạn đáp ứng các yêu cầu này đối với Kotlin 2.0.
Phiên bản AGP tối thiểu cho Consumer SDK 3.0 trở lên
Consumer SDK 3.0 yêu cầu sử dụng AGP 7.3 trở lên.
Chế độ R8 đầy đủ
Consumer SDK phiên bản 3 trở xuống không hỗ trợ chế độ R8 đầy đủ. Bạn phải tắt chế độ Full R8 một cách rõ ràng nếu ứng dụng của bạn nhắm đến AGP 8.0 trở lên.
# settings.gradle
android.enableR8.fullMode=false
Khả năng tương thích của phiên bản Kotlin
Kotlin hỗ trợ khả năng tương thích ngược với 3 phiên bản ngôn ngữ trước.
Điều này có nghĩa là nếu đang sử dụng Kotlin 1.7 trở lên với Consumer SDK, bạn có thể nâng cấp lên Kotlin 2.0 mà không cần phải giải quyết tất cả các thay đổi gây lỗi. Tuy nhiên, khả năng tương thích với Kotlin chỉ áp dụng cho các tính năng ngôn ngữ ổn định.
Nếu đang sử dụng các tính năng alpha, beta hoặc thử nghiệm trong ngôn ngữ Kotlin, thì bạn có thể phải thực hiện các thay đổi bổ sung khi nâng cấp.
Cờ tương thích Kotlin
Như đã đề cập trong phần trước, Kotlin hỗ trợ tối đa 3 phiên bản trước của ngôn ngữ khi nâng cấp. Kotlin cung cấp 2 cờ để giúp hạn chế các thay đổi gây lỗi:
language-version X.Y
Cờ này sẽ hoàn nguyên các thay đổi gây lỗi về hành vi của một phiên bản Kotlin trước đó.
Ví dụ: nếu đang dùng Kotlin 1.7, bạn có thể chỉ định [ -
language-version 1.7]
và các thay đổi có thể gây lỗi mới sẽ không còn có hiệu lực:
android {
kotlinOptions {
languageVersion = '1.7'
}
}
api-version X.Y
Cờ này ngăn việc sử dụng các API mới trước khi mã hạ nguồn phụ thuộc sẵn sàng kết hợp Kotlin 2.0.
android {
kotlinOptions {
apiVersion = '1.7'
}
}
Cách tiếp cận có mục tiêu cụ thể hơn
Ngoài việc sử dụng cờ tương thích Kotlin, bạn nên xem kỹ ghi chú phát hành Kotlin và chọn những hành vi mà bạn muốn giữ lại từ phiên bản mà bạn đang nâng cấp. Kotlin cung cấp danh sách các thay đổi có thể gây lỗi và các cờ có thể được đặt để giữ lại hành vi ban đầu trong hướng dẫn về khả năng tương thích cho từng phiên bản:
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-09-04 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-04 UTC."],[[["\u003cp\u003eUpgrading to Kotlin 2.0 is mandatory when utilizing the Consumer SDK for Android 3.0 or later.\u003c/p\u003e\n"],["\u003cp\u003eEnsure your Gradle and Android Gradle Plugin (AGP) versions meet the compatibility requirements for Kotlin 2.0, with Consumer SDK 3.0+ necessitating AGP 7.3 or higher.\u003c/p\u003e\n"],["\u003cp\u003eDisable Full R8 mode explicitly for Consumer SDK v3 and below when targeting AGP 8.0+.\u003c/p\u003e\n"],["\u003cp\u003eWhile Kotlin generally offers backward compatibility, potential breaking changes might require adjustments, especially when using alpha, beta, or experimental Kotlin features.\u003c/p\u003e\n"],["\u003cp\u003eLeverage Kotlin compatibility flags like \u003ccode\u003elanguage-version\u003c/code\u003e and \u003ccode\u003eapi-version\u003c/code\u003e or consult the Kotlin compatibility guides for managing specific breaking changes during the upgrade.\u003c/p\u003e\n"]]],["To migrate to Kotlin 2.0 with Consumer SDK 3.0+, ensure your Gradle and AGP versions meet Kotlin 2.0 requirements (AGP 7.3+). Disable Full R8 mode if using AGP 8.0+ by setting `android.enableR8.fullMode=false` in `settings.gradle`. Utilize Kotlin compatibility flags, `languageVersion` and `apiVersion`, to revert breaking changes or limit new API usage. Consult the Kotlin compatibility guides for specific version changes and behavior flags.\n"],null,["Migrate to Kotlin 2.0\n\nIf you are using the Consumer SDK for Android 3.0+ you must upgrade to Kotlin\n2.0. This page provides guidance to ease the transition to the new Kotlin\nversion.\n| **Note:** For information on upgrading to Kotlin 1.9, which was required for the Consumer SDK 5.0+ see the [Kotlin 1.9 upgrade\n| guide](/maps/documentation/transportation-logistics/on-demand-rides-deliveries-solution/trip-order-progress/migrations/android_consumer_sdk_v2.0_migration#kotlin_16_to_19_migration).\n\nGradle and AGP compatibility\n\nKotlin 2.0 has [requirements for the minimum and maximum\nversions](https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin)\nfor your Gradle and Android Gradle Plugin (AGP) version. Ensure that your\nproject meets these requirements for Kotlin 2.0.\n\nAGP minimum version for the Consumer SDK 3.0+\n\nConsumer SDK 3.0 requires using AGP 7.3+.\n\nFull R8 mode\n\nConsumer SDK v3 and below does not support Full R8 mode. You must explicitly\ndisable Full R8 mode if your application targets AGP 8.0+. \n\n # settings.gradle\n android.enableR8.fullMode=false\n\nKotlin version compatibility\n\nKotlin supports backwards compatibility with [three previous language\nversions](https://kotlinlang.org/docs/kotlin-evolution-principles.html#compatibility-options).\nThis means that if you are already using Kotlin 1.7+ with the Consumer SDK, you\nshould be able to upgrade to Kotlin 2.0 without having to resolve all of the\nbreaking changes. However, [Kotlin compatibility only applies to stable language\nfeatures](https://kotlinlang.org/docs/components-stability.html#stability-levels-explained).\nIf you are using alpha, beta or experimental features in the Kotlin language,\nthen you may have to make additional changes when upgrading.\n\nKotlin compatibility flags\n\nAs noted in the previous section, Kotlin supports up to 3 previous versions of\nthe language when upgrading. Kotlin [supplies two flags to help with limiting\nbreaking changes](https://kotlinlang.org/docs/compatibility-modes.html):\n\nlanguage-version X.Y\n\nThis flag reverts breaking changes to the behavior of a previous Kotlin version.\nFor example, if you are using Kotlin 1.7, you could specify `[ -\nlanguage-version 1.7]` and the new breaking changes would no longer take effect: \n\n android {\n kotlinOptions {\n languageVersion = '1.7'\n }\n }\n\napi-version X.Y\n\nThis flag prevents new APIs from being used before dependent downstream code is\nready to incorporate Kotlin 2.0. \n\n android {\n kotlinOptions {\n apiVersion = '1.7'\n }\n }\n\nA more targeted approach\n\nIn addition to using Kotlin compatibility flags, we recommend reviewing the\nKotlin release notes and choosing the behaviors that you would like to retain\nfrom the version that you are upgrading from. Kotlin provides a list of breaking\nchanges and the flags that can be set to retain the original behavior in its\ncompatibility guides for each version:\n\n- [1.8 compatibility guide](https://kotlinlang.org/docs/compatibility-guide-18.html)\n- [1.9 compatibility guide](https://kotlinlang.org/docs/compatibility-guide-19.html)\n- [2.0 compatibility guide](https://kotlinlang.org/docs/compatibility-guide-20.html)"]]