The older
ARCore Cloud Anchor API cloud endpoint has been deprecated and will not be supported after August 31, 2023. If your app is using this API, you must
update it to use the new
ARCore API cloud endpoint as soon as possible.
Cloud Anchors quickstart for Android
Stay organized with collections
Save and categorize content based on your preferences.
Try out hosting and resolving Cloud Anchors in the cloud_anchor_java
and persistent_cloud_anchor_java
sample apps.
Which sample app should you use?
Cloud Anchors are anchors that are hosted on the ARCore API cloud endpoint. This API enables users to share experiences in the same app. Persistent Cloud Anchors are Cloud Anchors that can be hosted for more than 24 hours.
persistent_cloud_anchor_java
is an implementation of a collaborative project use case. It uses keyless authorization to authorize ARCore API calls. Because it doesn’t use a Firebase backend to share Cloud Anchor IDs between devices, it is much easier to get up and running.
cloud_anchor_java
is an implementation of a multi-player use case. It uses an API key to authorize ARCore API calls.
Prerequisites
Requirements
Hardware
Software
- Android Studio version 3.0 or later with Android SDK Platform version 7.0 (API level 24) or higher
- The ARCore SDK for Android, which you can get in one of two ways:
- Download it from GitHub and extract it on your machine
- Clone the repository with the following command:
git clone https://github.com/google-ar/arcore-android-sdk.git
Open the sample app in Android Studio
The ARCore SDK provides the cloud_anchor_java
and persistent_cloud_anchor_java
sample apps to demonstrate Cloud Anchors functionality. Follow these steps to open the apps in Android Studio.
Persistent Cloud Anchors
In Android Studio, click Open.
Navigate to the place where the arcore-android-sdk directory is stored on your machine. Do not open the entire SDK folder. Instead, go to samples > persistent_cloud_anchor_java and click Open.
Cloud Anchors
In Android Studio, click Open.
Navigate to the place where the arcore-android-sdk directory is stored on your machine. Do not open the entire SDK folder. Instead, go to samples > cloud_anchor_java and click Open.
Set up Cloud Anchor ID sharing
Cloud Anchor IDs are strings that identify hosted Cloud Anchors. They are used to resolve, or render the 3D objects attached to, the hosted anchors.
Persistent Cloud Anchors
Cloud Anchor ID sharing is maintained locally in the app. You won’t need to do anything here.
Cloud Anchors
The cloud_anchor_java
sample app uses Firebase's Realtime Databases to share Cloud Anchor IDs between devices. You can use a different solution in your own apps.
- Manually add Firebase to your app. The
cloud_anchor_java
package name is com.google.ar.core.examples.java.cloudanchor
. You can find it in main/AndroidManifest.xml
.
- Download the
google-services.json
file that you generated when you added Firebase to your app.
- Create a Realtime Database with Firebase.
- In Android Studio, add the
google-services.json
file to your project’s app
directory.
Authorize ARCore API calls
Authorize calls to the ARCore API to host and resolve Cloud Anchors for your
app. Follow steps for on Use the ARCore API on Google
Cloud and use Keyless
authorization for Persistent Cloud Anchors or API Key authorization for
Cloud Anchors.
Build and run the sample app
Persistent Cloud Anchors
Run the app
- Make sure that your device has enabled developer options and USB debugging.
- Connect your device via USB to your development machine.
- In Android Studio, select your device as the deployment target and click Run.
persistent_cloud_anchor_java
should launch on your device, prompting ARCore to detect the planes in front of the device’s camera.
If Google Cloud authorization fails, see Troubleshooting steps.
Place an anchor
- Once the app begins to detect planes, tap your screen to place an anchor on one of the detected planes.
- Tap the HOST button to host the placed anchor. This sends a host request to the ARCore API, which includes data representing the anchor’s position relative to the visual features near it.
A successful host request establishes an anchor at the placed location and assigns it a Cloud Anchor ID. If the host request is successful, the app should display a room code. You can use this code to access previously hosted anchors for this room on any device.
Resolve an anchor
- Tap RESOLVE and enter a previously returned room code to access the anchors hosted in this room. This sends a resolve request to the ARCore API, which returns the IDs of all anchors currently hosted in the room.
persistent_cloud_anchor_java
will use these IDs to render 3D objects attached to the hosted anchors.
Cloud Anchors
Run the app
- Make sure that your device has enabled developer options and USB debugging.
- Connect your device via USB to your development machine.
- In Android Studio, select your device as the deployment target and click Run.
cloud_anchor_java
should launch on your device, prompting ARCore to detect the planes in front of the device’s camera.
Error: com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance
If you encounter this error, ensure that the firebase_url
property is present in google-services.json
. You can obtain the correct value for this property by ensuring that the Realtime Database has been created and downloading google-services.json
.
Place an anchor
- Once the app begins to detect planes, tap your screen to place an anchor on one of the detected planes.
- Tap the HOST button to host the placed anchor. This sends a host request to the ARCore API, which includes data representing the anchor’s position relative to the visual features near it.
A successful host request establishes an anchor at the placed location and assigns it a Cloud Anchors ID. If the host request is successful, the app should display a room code. You can use this code to access previously hosted anchors for this room on any device.
Resolve an anchor
- Tap RESOLVE and enter a previously returned room code to access the anchors hosted in this room. This sends a resolve request to the ARCore API, which returns the IDs of all anchors currently hosted in the room.
cloud_anchor_java
will use these IDs to render 3D objects attached to the hosted anchors.
What’s next
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[null,null,["Last updated 2024-10-31 UTC."],[[["\u003cp\u003eExplore Cloud Anchors functionality using the \u003ccode\u003ecloud_anchor_java\u003c/code\u003e and \u003ccode\u003epersistent_cloud_anchor_java\u003c/code\u003e sample apps within the ARCore SDK.\u003c/p\u003e\n"],["\u003cp\u003ePersistent Cloud Anchors enable collaborative projects and persist for over 24 hours, while standard Cloud Anchors support multi-player use cases.\u003c/p\u003e\n"],["\u003cp\u003eBefore running the apps, ensure you have the necessary hardware and software, including an ARCore-supported device and Android Studio.\u003c/p\u003e\n"],["\u003cp\u003eAuthorize ARCore API calls using keyless authorization for Persistent Cloud Anchors and API key authorization for Cloud Anchors.\u003c/p\u003e\n"],["\u003cp\u003eHost and resolve anchors within the sample apps to experience Cloud Anchor functionality, placing and rendering 3D objects in shared AR experiences.\u003c/p\u003e\n"]]],["The content guides users to experiment with Cloud Anchors using two sample apps: `cloud_anchor_java` and `persistent_cloud_anchor_java`. These apps demonstrate hosting and resolving anchors via the ARCore API. Key actions include: installing prerequisites, opening the sample app in Android Studio, setting up Cloud Anchor ID sharing (using either keyless authorization or API key authorization), running the app, placing and hosting an anchor, and resolving an anchor using a room code. `Persistent_cloud_anchor_java` uses keyless authorization and local ID sharing. `cloud_anchor_java` uses API key authorization and Firebase for ID sharing.\n"],null,["# Cloud Anchors quickstart for Android\n\nTry out hosting and resolving [Cloud Anchors](/ar/develop/java/cloud-anchors/introduction) in the `cloud_anchor_java` and `persistent_cloud_anchor_java` sample apps.\n\nWhich sample app should you use?\n--------------------------------\n\nCloud Anchors are [anchors](/ar/develop/java/anchors) that are hosted on the [**ARCore API**](https://console.cloud.google.com/apis/library/arcore) cloud endpoint. This API enables users to share experiences in the same app. **Persistent Cloud Anchors** are Cloud Anchors that can be hosted for more than 24 hours.\n\n- `persistent_cloud_anchor_java` is an implementation of a collaborative project use case. It uses keyless authorization to authorize ARCore API calls. Because it doesn't use a Firebase backend to share Cloud Anchor IDs between devices, it is much easier to get up and running.\n- `cloud_anchor_java` is an implementation of a multi-player use case. It uses an API key to authorize ARCore API calls.\n\nPrerequisites\n-------------\n\n- Understand how [anchors](/ar/develop/java/anchors) and [Cloud Anchors](/ar/develop/java/cloud-anchors/introduction) work.\n- [Install and configure](/ar/develop/java/quickstart) the ARCore SDK for Android.\n\nRequirements\n------------\n\n### Hardware\n\n- An [ARCore supported device](/ar/devices) with the latest version of [Google Play Services for AR](https://play.google.com/store/apps/details?id=com.google.ar.core) installed\n- A USB cable to connect the device to your development machine\n\n### Software\n\n- [Android Studio](https://developer.android.com/studio) version 3.0 or later with Android SDK Platform version 7.0 (API level 24) or higher\n- The ARCore SDK for Android, which you can get in one of two ways:\n - Download it from [GitHub](https://github.com/google-ar/arcore-android-sdk) and extract it on your machine\n - Clone the repository with the following command: \n\n ```\n git clone https://github.com/google-ar/arcore-android-sdk.git\n ```\n\nOpen the sample app in Android Studio\n-------------------------------------\n\nThe ARCore SDK provides the `cloud_anchor_java` and `persistent_cloud_anchor_java` sample apps to demonstrate Cloud Anchors functionality. Follow these steps to open the apps in Android Studio. \n\n### Persistent Cloud Anchors\n\n1. In Android Studio, click **Open**.\n\n2. Navigate to the place where the **arcore-android-sdk** directory is stored on your machine. Do not open the entire SDK folder. Instead, go to **samples** \\\u003e **persistent_cloud_anchor_java** and click **Open**.\n\n### Cloud Anchors\n\n1. In Android Studio, click **Open**.\n\n2. Navigate to the place where the **arcore-android-sdk** directory is stored on your machine. Do not open the entire SDK folder. Instead, go to **samples** \\\u003e **cloud_anchor_java** and click **Open**.\n\nSet up Cloud Anchor ID sharing\n------------------------------\n\n**Cloud Anchor IDs** are strings that identify hosted Cloud Anchors. They are used to resolve, or render the 3D objects attached to, the hosted anchors. \n\n### Persistent Cloud Anchors\n\nCloud Anchor ID sharing is maintained locally in the app. You won't need to do anything here.\n\n### Cloud Anchors\n\nThe `cloud_anchor_java` sample app uses Firebase's Realtime Databases to share Cloud Anchor IDs between devices. You can use a different solution in your own apps.\n\n1. [Manually add Firebase to your app.](https://firebase.google.com/docs/android/setup#manually_add_firebase) The `cloud_anchor_java` package name is `com.google.ar.core.examples.java.cloudanchor`. You can find it in `main/AndroidManifest.xml`.\n2. Download the `google-services.json` file that you generated when you added Firebase to your app.\n3. [Create a Realtime Database with Firebase.](https://firebase.google.com/docs/database/android/start#create_a_database)\n4. In Android Studio, add the `google-services.json` file to your project's `app` directory.\n\nAuthorize ARCore API calls\n--------------------------\n\nAuthorize calls to the ARCore API to host and resolve Cloud Anchors for your\napp. Follow steps for on [Use the ARCore API on Google\nCloud](/ar/develop/authorization?platform=android) and use **Keyless\nauthorization** for Persistent Cloud Anchors or **API Key authorization** for\nCloud Anchors.\n\nBuild and run the sample app\n----------------------------\n\n### Persistent Cloud Anchors\n\n### Run the app\n\n1. Make sure that your device has enabled [developer options and USB debugging](https://developer.android.com/studio/debug/dev-options).\n2. Connect your device via USB to your development machine.\n3. In Android Studio, select your device as the deployment target and click **Run**.\n\n`persistent_cloud_anchor_java` should launch on your device, prompting ARCore to detect the planes in front of the device's camera.\n\nIf Google Cloud authorization fails, see [Troubleshooting steps](/ar/develop/authorization/troubleshooting?platform=android).\n\n### Place an anchor\n\n1. Once the app begins to detect planes, tap your screen to place an anchor on one of the detected planes.\n2. Tap the **HOST** button to host the placed anchor. This sends a host request to the ARCore API, which includes data representing the anchor's position relative to the visual features near it.\n\nA successful host request establishes an anchor at the placed location and assigns it a Cloud Anchor ID. If the host request is successful, the app should display a room code. You can use this code to access previously hosted anchors for this room on any device.\n\n### Resolve an anchor\n\n1. Tap **RESOLVE** and enter a previously returned room code to access the anchors hosted in this room. This sends a resolve request to the ARCore API, which returns the IDs of all anchors currently hosted in the room. `persistent_cloud_anchor_java` will use these IDs to render 3D objects attached to the hosted anchors.\n\n### Cloud Anchors\n\n### Run the app\n\n1. Make sure that your device has enabled [developer options and USB debugging](https://developer.android.com/studio/debug/dev-options).\n2. Connect your device via USB to your development machine.\n3. In Android Studio, select your device as the deployment target and click **Run**.\n\n`cloud_anchor_java` should launch on your device, prompting ARCore to detect the planes in front of the device's camera. \n\n#### Error: `com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance`\n\nIf you encounter this error, ensure that the `firebase_url` property is present in `google-services.json`. You can obtain the correct value for this property by ensuring that the Realtime Database has been created and downloading `google-services.json`.\n\n*** ** * ** ***\n\n### Place an anchor\n\n1. Once the app begins to detect planes, tap your screen to place an anchor on one of the detected planes.\n2. Tap the **HOST** button to host the placed anchor. This sends a host request to the ARCore API, which includes data representing the anchor's position relative to the visual features near it.\n\nA successful host request establishes an anchor at the placed location and assigns it a Cloud Anchors ID. If the host request is successful, the app should display a room code. You can use this code to access previously hosted anchors for this room on any device.\n\n### Resolve an anchor\n\n1. Tap **RESOLVE** and enter a previously returned room code to access the anchors hosted in this room. This sends a resolve request to the ARCore API, which returns the IDs of all anchors currently hosted in the room. `cloud_anchor_java` will use these IDs to render 3D objects attached to the hosted anchors.\n\nWhat's next\n-----------\n\n- Create a Cloud Anchors app with the [ARCore Cloud Anchors with persistent Cloud Anchors codelab](https://codelabs.developers.google.com/codelabs/arcore-cloud-anchors#0).\n- Learn how to incorporate Cloud Anchors in your app with the [Cloud Anchors developer guide](/ar/develop/java/cloud-anchors/developer-guide).\n- Manage Cloud Anchors outside of your ARCore app using the [Cloud Anchors Management API](/ar/develop/java/cloud-anchors/management-api)."]]