Làm quen với Dịch vụ trò chơi của Play dành cho C++ trên Android
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.
Sau khi ngừng sử dụng API Đăng nhập bằng Google, chúng tôi sẽ xoá SDK trò chơi phiên bản 1 vào năm 2026. Sau tháng 2 năm 2025, bạn sẽ không thể phát hành các trò chơi mới tích hợp với SDK trò chơi phiên bản 1 trên Google Play. Thay vào đó, bạn nên sử dụng SDK trò chơi v2.
Mặc dù các trò chơi hiện có tích hợp với phiên bản 1 của trò chơi trước đó sẽ tiếp tục hoạt động trong vài năm, nhưng bạn nên di chuyển sang phiên bản 2 kể từ tháng 6 năm 2025.
Hướng dẫn này dành cho việc sử dụng SDK Dịch vụ trò chơi của Play phiên bản 1. SDK C++ cho Dịch vụ trò chơi của Play phiên bản 2 chưa có sẵn.
Hướng dẫn dành cho nhà phát triển này sẽ hướng dẫn bạn cách biên dịch và chạy trò chơi C++ trên Android bằng API Dịch vụ trò chơi của Google Play. Trước khi bắt đầu, hãy tải xuống và định cấu hình các yêu cầu sau:
Hướng dẫn dành cho nhà phát triển này sử dụng Bộ phát triển mã gốc Android (NDK). Nếu bạn chưa quen với NDK, hãy tham khảo tài liệu và mẫu NDK trước khi tiếp tục.
Bước 1: Thiết lập môi trường
Tải SDK Android và Android NDK xuống rồi giải nén vào máy của bạn. Trong môi trường của bạn, hãy đặt SDK_ROOT thành vị trí của thư mục SDK Android và NDK_ROOT thành vị trí của thư mục Android NDK.
Tải các trò chơi mẫu C++ xuống. Hướng dẫn dành cho nhà phát triển này gọi vị trí của các mẫu trên máy là SAMPLES_DIR.
Tải SDK C++ của dịch vụ Google Play Games. Giải nén SDK vào máy phát triển. Trong môi trường của bạn, hãy đặt biến NDK_MODULE_PATH để trỏ đến thư mục ở trên thư mục gpg-cpp-sdk. Bạn sẽ có cấu trúc thư mục sau:
NDK_MODULE_PATH/
gpg-cpp-sdk/
Mở Eclipse. Nếu bạn chưa thực hiện việc này, hãy cho Eclipse biết vị trí bạn đã cài đặt NDK bằng cách nhấp vào Preferences (Lựa chọn ưu tiên) > Android > NDK.
Nhập dự án thư viện Dịch vụ Google Play vào không gian làm việc Eclipse.
Trong Eclipse, hãy nhấp vào File > Import > Android > Existing Android Code into Workspace (Tệp > Nhập > Android > Mã Android hiện có vào không gian làm việc).
Chọn SDK_ROOT/extras/google/google_play_services/libproject/google-play-services_lib, trong đó SDK_ROOT là vị trí của SDK Android.
Nhấp vào Hoàn tất.
Nhập dự án mẫu tối giản vào không gian làm việc Eclipse.
Trong Eclipse, hãy nhấp vào File > Import > Android > Existing Android Code into Workspace (Tệp > Nhập > Android > Mã Android hiện có vào không gian làm việc).
Chọn SAMPLES_DIR/samples-android/minimalist.
Nhấp vào Hoàn tất.
Nhấp chuột phải vào dự án MinimalistActivity rồi nhấp vào Properties (Thuộc tính). Trong phần Android, hãy di chuyển xuống phần Thư viện và đảm bảo dự án google-play-services_lib được tham chiếu chính xác. Nếu không, hãy xoá tệp đối chiếu rồi thêm lại tệp đó vào không gian làm việc của bạn.
Eclipse sẽ tự động biên dịch các nguồn Java và Android của dự án; tuy nhiên, bạn cần biên dịch riêng mã gốc trong thư mục jni. Để thực hiện việc này theo cách thủ công, hãy chuyển đến thư mục jni và chạy ndk-build. Hãy nhớ thực hiện việc này sau khi bạn thực hiện bất kỳ thay đổi nào trong thư mục jni.
Lúc này, dự án của bạn sẽ biên dịch, mặc dù chưa hoạt động. Trước tiên, bạn phải định cấu hình trò chơi trong Google Play Console.
Bước 2: Thiết lập trò chơi trong Google Play Console
Tạo một mục nhập cho trò chơi của bạn trong Google Play Console. Thao tác này sẽ bật Dịch vụ trò chơi cho ứng dụng của bạn và tạo mã ứng dụng khách OAuth 2.0 (nếu bạn chưa có).
Trong AndroidManifest.xml, hãy thay đổi thuộc tính package của thẻ <manifest> thành tên gói mà bạn đã chọn khi thiết lập Google Play Console. Bạn có thể phải sửa một số tệp tham chiếu trong toàn bộ dự án sau khi thực hiện thay đổi này (đặc biệt là đối với lớp R đã tạo).
Mở res/values/ids.xml và đặt Mã ứng dụng của bạn vào đó. Xin lưu ý rằng Mã ứng dụng không giống với mã ứng dụng khách; đó là con số bên cạnh tên trò chơi của bạn trong trang Thông tin chi tiết về trò chơi của Google Play Console.
Bước 3: Chạy mẫu
Để chạy mẫu này, bạn cần có một thiết bị Android thực hoặc một trình mô phỏng đã cài đặt Dịch vụ Google Play:
Chạy ndk-build để biên dịch mã gốc.
Trong Eclipse, hãy nhấp vào Run > Run As > Android Application (Chạy > Chạy dưới dạng > Ứng dụng Android) rồi chạy mẫu trên thiết bị.
Khi mẫu mở ra, hãy nhấn vào vị trí bất kỳ trên màn hình. Bạn sẽ thấy biểu trưng Google Play Games xuất hiện. Nếu đã thiết lập ứng dụng đúng cách, bạn sẽ được nhắc đăng nhập.
Không bắt buộc: Tự động tạo bản dựng bằng Eclipse
Các bước sau đây cho bạn biết cách định cấu hình Eclipse để tự động chạy ndk-build khi bạn thực hiện thay đổi đối với các tệp trong thư mục jni.
Nhấp chuột phải vào dự án MinimalistActivity rồi nhấp vào Properties (Thuộc tính). Trong cửa sổ Properties (Thuộc tính), chọn ngăn Builders (Trình tạo).
Nhấp vào New (Mới) để thêm một Trình tạo mới, rồi chọn Program (Chương trình) rồi nhấp vào OK.
Trong trường Name (Tên), hãy nhập "NDK Builder".
Trong mục Vị trí, hãy nhấp vào Duyệt qua hệ thống tệp rồi chuyển đến thư mục NDK_ROOT và chọn lệnh ndk-build.
Trong Working Directory (Thư mục đang hoạt động), hãy nhấp vào Browse Workspace (Duyệt qua không gian làm việc) rồi chọn thư mục dự án MinimalistActivity.
Nhấp vào thẻ Làm mới. Đảm bảo bạn đã đánh dấu vào hộp Refresh resources upon completion (Làm mới tài nguyên khi hoàn tất).
Chọn nút chọn Tài nguyên cụ thể, sau đó nhấp vào Chỉ định tài nguyên. Trong hộp thoại thu được, hãy chọn thư mục jni trong MinimalActivity.
Nhấp vào Apply (Áp dụng) rồi nhấp vào OK để hoàn tất việc tạo Trình tạo.
Giờ đây, mỗi khi bạn chỉnh sửa một tệp trong thư mục jni, Eclipse sẽ chạy ndk-build và in kết quả vào Eclipse Console.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 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-07-27 UTC."],[],[],null,["# Get Started with Play Games Services for C++ on Android\n\nFollowing the deprecation of the\n[Google Sign-In](https://android-developers.googleblog.com/2024/09/streamlining-android-authentication-credential-manager-replaces-legacy-apis.html)\nAPI, we are removing the games v1 SDK in 2026. After February 2025, you will be unable to publish\ntitles that are newly integrated with games v1 SDK, on Google Play. We recommend that you use the\ngames v2 SDK instead. \n\nWhile existing titles with the previous games v1 integrations continue to function for a\ncouple of years, you are encouraged to\n[migrate to v2](/games/pgs/android/migrate-to-v2)\nstarting June 2025. \n\nThis guide is for using the Play Games Services v1 SDK. The C++ SDK for\nPlay Games Services v2 is not yet available.\n\nThis developer guide will show you how to compile and run a C++ game on Android that uses the Google Play\nGame services API. Before you begin, download and configure the following requirements:\n\n- [Android NDK](/ndk), Revision 14 or higher.\n- [Android SDK v10 or higher and latest version of the Eclipse ADT](/studio/tools/sdk/eclipse-adt).\n- Device must be running Android 4.0 (API level 14) or higher.\n- [The latest version of the Google Play services SDK.](https://developers.google.com/android/guides/setup)\n\nThis developer guide uses the Android Native Development Kit (NDK). If you are unfamiliar with the NDK,\nrefer to the [NDK documentation and samples](/ndk)\nbefore continuing.\n\nStep 1: Set up your environment\n-------------------------------\n\n1. Download the Android SDK and the Android NDK and extract them to your machine. In your environment, set `SDK_ROOT` to the location of your Android SDK folder and `NDK_ROOT` to the location of your Android NDK folder.\n2. Download the [C++ sample games](//github.com/playgameservices/cpp-android-basic-samples). This developer guide refers to the location of the samples on your machine as `SAMPLES_DIR`.\n3. Download the [Google Play Games services C++ SDK](/games/pgs/downloads#sdk). Extract the SDK onto your development machine. In your environment, set the variable `NDK_MODULE_PATH` to point to the directory **above** the `gpg-cpp-sdk` directory. You should have the following directory structure: \n\n ```\n NDK_MODULE_PATH/\n gpg-cpp-sdk/\n ```\n4. Open Eclipse. If you have not already done so, tell Eclipse where you installed the NDK by clicking **Preferences \\\u003e Android \\\u003e NDK**.\n5. Import the Google Play services library project into your Eclipse workspace.\n\n 1. In Eclipse, click **File \\\u003e Import \\\u003e Android \\\u003e Existing Android Code into Workspace**.\n 2. Select `SDK_ROOT/extras/google/google_play_services/libproject/google-play-services_lib` where `SDK_ROOT` is the location of your Android SDK.\n 3. Click **Finish**.\n6. Import the minimalist sample project into your Eclipse workspace.\n\n 1. In Eclipse, click **File \\\u003e Import \\\u003e Android \\\u003e Existing Android Code into Workspace**.\n 2. Select `SAMPLES_DIR/samples-android/minimalist`.\n 3. Click **Finish**.\n7. Right click the MinimalistActivity project and click **Properties** . Under **Android** , scroll\n down to the **Library** section and make sure the google-play-services_lib project is correctly\n referenced. If not, remove the reference and add it again from your workspace.\n\nEclipse will automatically compile the Java and Android sources of your project; however, the\nnative code in the `jni` folder needs to be compiled separately. To do this manually, navigate to\nthe `jni` folder and run `ndk-build`. Remember to do this after you make any changes inside\nthe `jni` folder.\n\nYour project should now compile, though it will not work yet. You must first configure your game\nin the Google Play Console.\n\nStep 2: Set up the game in the Google Play Console\n--------------------------------------------------\n\nCreate an entry for your game in the Google Play Console. This enables Games services for your\napplication, and creates an OAuth 2.0 client ID, if you don't already have one.\n\n1. Create an entry for your game by following the steps described in [Setting Up Google Play Games Services](/games/pgs/console/setup).\n2. In `AndroidManifest.xml`, change the `package` attribute of the `\u003cmanifest\u003e` tag to the package name you chose when setting up the Google Play Console. You may have to fix some references throughout the project after making this change (particularly to the generated `R` class).\n3. Open `res/values/ids.xml` and place your App ID there. Note that the App ID is not the same as the client ID; it is the number next to the name of your game in the **Game Details** page of the Google Play Console.\n\nStep 3: Run the sample\n----------------------\n\nIn order to run the sample you will need a physical Android device or an emulator with\nGoogle Play services installed:\n\n1. Run `ndk-build` to compile the native code.\n2. In Eclipse, click **Run \\\u003e Run As \\\u003e Android Application** and run the sample on your device.\n3. When the sample opens, tap anywhere on the screen. You should see a Google Play Games logo appear. If you have correctly configured your app, you will be prompted to sign in.\n\n| **Important:** Make sure to sign your APK with the same certificate as the one whose fingerprint you configured on Google Play Console. Failure to do so will produce errors.\n| **Important:** If you are testing an unpublished game, make sure that the account with which you intend to sign in (usually, the account on the test device) is listed as a tester in the project. Open the **Testing** page, in the **Game services** subtab of the Google Play Console, to check or perform whitelisting. Failure to perform this step causes the server to act as though your project does not exist, and return errors.\n\nOptional: Build automatically with Eclipse\n------------------------------------------\n\nThe following steps show you how to configure Eclipse to automatically run `ndk-build` when you\nmake changes to files in the `jni` folder.\n\n1. Right-click the MinimalistActivity project and click **Properties** . In the Properties window, select the **Builders** pane.\n2. Click **New** to add a new Builder and select **Program** then click **OK**.\n3. In the **Name** field, enter 'NDK Builder'.\n4. Under **Location** click **Browse File System** and navigate to the `NDK_ROOT` directory and select the `ndk-build` command.\n5. Under **Working Directory** click **Browse Workspace** and select the MinimalistActivity project folder.\n6. Click the **Refresh** tab. Make sure the **Refresh resources upon completion** box is checked.\n7. Select the **Specific resources** radio button and then click **Specify Resources** . In the resulting dialog, select the `jni` folder under MinimalActivity.\n8. Click **Apply** and then **OK** to finish creating your Builder.\n\nNow every time you edit a file within the `jni` folder, Eclipse will run `ndk-build` and print the\noutput to the Eclipse Console.\n| **Note:** If you see errors when editing C or C++ files in Eclipse that prevent you from building the project, you may want to disable C/C++ error reporting in Eclipse. In your project properties, navigate to **C/C++ General \\\u003e Code Analysis \\\u003e Launching** and make sure that both boxes are unchecked. Then click **Apply** and **OK** . This is a workaround to prevent Eclipse from checking your native code, it does **not** fix any real underlying errors you may have."]]