Inizia a usare i servizi per i giochi di Play per C++ su Android
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
A seguito del ritiro dell'API Google Sign-in, rimuoveremo l'SDK di Google Play Services per i giochi v1 nel 2026. Dopo febbraio 2025, non potrai pubblicare su Google Play titoli appena integrati con l'SDK v1 per i giochi. Ti consigliamo di utilizzare invece l'SDK giochi v2.
Sebbene i titoli esistenti con le integrazioni precedenti della versione 1 dei giochi continuino a funzionare per un paio di anni, ti invitiamo a eseguire la migrazione alla versione 2 a partire da giugno 2025.
Questa guida è dedicata all'utilizzo dell'SDK Play Services v1. L'SDK C++ per la versione 2 dei servizi per i giochi di Google Play non è ancora disponibile.
Questa guida per gli sviluppatori ti mostrerà come compilare ed eseguire un gioco in C++ su Android che utilizza l'API Google Play Services per i giochi. Prima di iniziare, scarica e configura i seguenti requisiti:
Questa guida per gli sviluppatori utilizza il Native Development Kit (NDK) per Android. Se non hai familiarità con l'NDK, consulta la documentazione e i sample dell'NDK prima di continuare.
Passaggio 1: configura l'ambiente
Scarica l'SDK Android e l'Android NDK ed estraili sulla tua macchina. Nel tuo ambiente, imposta SDK_ROOT sulla posizione della cartella dell'SDK Android e NDK_ROOT sulla posizione della cartella dell'NDK Android.
Scarica i giochi di esempio in C++. In questa
guida per gli sviluppatori, la posizione dei sample sulla tua macchina è indicata come SAMPLES_DIR.
Scarica l'SDK C++ dei servizi per i giochi di Google Play. Estrai
l'SDK sulla tua macchina di sviluppo. Nel tuo ambiente, imposta la variabile NDK_MODULE_PATH in modo che rimandi alla directory sopra la directory gpg-cpp-sdk. Dovresti avere la seguente
struttura di directory:
NDK_MODULE_PATH/
gpg-cpp-sdk/
Apri Eclipse. Se non l'hai già fatto, indica a Eclipse dove hai installato il NDK facendo clic su Preferenze > Android > NDK.
Importa il progetto della libreria Google Play Services nello spazio di lavoro Eclipse.
In Eclipse, fai clic su File > Importa > Android > Codice Android esistente nello spazio di lavoro.
Seleziona SDK_ROOT/extras/google/google_play_services/libproject/google-play-services_lib
dove SDK_ROOT è la posizione dell'SDK Android.
Fai clic su Fine.
Importa il progetto di esempio minimalista nello spazio di lavoro Eclipse.
In Eclipse, fai clic su File > Importa > Android > Codice Android esistente nello spazio di lavoro.
Seleziona SAMPLES_DIR/samples-android/minimalist.
Fai clic su Fine.
Fai clic con il tasto destro del mouse sul progetto MinimalistActivity e poi su Proprietà. In Android, scorri verso il basso fino alla sezione Library e assicurati che il progetto google-play-services_lib sia correttamente richiamato. In caso contrario, rimuovi il riferimento e aggiungilo di nuovo dallo spazio di lavoro.
Eclipse compilerà automaticamente i sorgenti Java e Android del progetto, ma il codice nativo nella cartella jni deve essere compilato separatamente. Per farlo manualmente, vai alla
cartella jni ed esegui ndk-build. Ricordati di farlo dopo aver apportato modifiche all'interno della cartella jni.
Il progetto ora dovrebbe essere compilato, anche se non funzionerà ancora. Devi prima configurare il tuo gioco
in Google Play Console.
Passaggio 2: configura il gioco in Google Play Console
Crea una voce per il tuo gioco in Google Play Console. In questo modo, vengono attivati i servizi per i giochi per la tua applicazione e viene creato un ID client OAuth 2.0, se non ne hai già uno.
In AndroidManifest.xml, modifica l'attributo package del tag <manifest> con il nome del pacchetto scelto durante la configurazione di Google Play Console. Dopo aver apportato questa modifica, potresti dover correggere alcuni riferimenti
in tutto il progetto (in particolare alla classe R generata).
Apri res/values/ids.xml e inserisci l'ID app. Tieni presente che l'ID app non corrisponde all'ID cliente, ma è il numero accanto al nome del gioco nella pagina Dettagli gioco di Google Play Console.
Passaggio 3: esegui il sample
Per eseguire il sample, devi disporre di un dispositivo Android fisico o di un emulatore con Google Play Services installato:
Esegui ndk-build per compilare il codice nativo.
In Eclipse, fai clic su Esegui > Esegui come > Applicazione Android ed esegui il Sample sul tuo dispositivo.
Quando il Sample si apre, tocca un punto qualsiasi dello schermo. Dovresti vedere il logo di Google Play Giochi. Se hai configurato correttamente l'app, ti verrà chiesto di accedere.
(Facoltativo) Esegui la compilazione automaticamente con Eclipse
I passaggi che seguono mostrano come configurare Eclipse in modo che esegua automaticamente ndk-build quando apporti modifiche ai file nella cartella jni.
Fai clic con il tasto destro del mouse sul progetto MinimalistActivity e poi su Proprietà. Nella finestra Proprietà,
seleziona il riquadro Generatori.
Fai clic su Nuovo per aggiungere un nuovo generatore, seleziona Programma e poi fai clic su Ok.
Nel campo Nome, inserisci "NDK Builder".
In Posizione, fai clic su Sfoglia file system, vai alla directory NDK_ROOT e
seleziona il comando ndk-build.
In Directory di lavoro, fai clic su Sfoglia area di lavoro e seleziona la cartella del progetto MinimalistActivity.
Fai clic sulla scheda Aggiorna. Assicurati che la casella Aggiorna risorse al termine sia selezionata.
Seleziona il pulsante di opzione Risorse specifiche e poi fai clic su Specifica risorse. Nella
finestra di dialogo visualizzata, seleziona la cartella jni in MinimalActivity.
Fai clic su Applica e poi su OK per completare la creazione del generatore.
Ora, ogni volta che modifichi un file all'interno della cartella jni, Eclipse eseguirà ndk-build e stamperà l'output nella console Eclipse.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-27 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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."]]