Android 適用的 Cloud Anchors 快速入門導覽課程
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
嘗試在 cloud_anchor_java
和 persistent_cloud_anchor_java
範例應用程式中託管及解析 Cloud Anchors。
您應該使用哪一個範例應用程式?
Cloud Anchor 是錨點,託管於 ARCore API 雲端端點。這個 API 可讓使用者在相同的應用程式中分享體驗。「永久 Cloud Anchors」是可託管超過 24 小時的 Cloud 錨點。
persistent_cloud_anchor_java
是協作專案用途的實作。它使用無金鑰授權來授權 ARCore API 呼叫。由於這個 API 不使用 Firebase 後端在裝置之間共用 Cloud Anchor ID,因此設定和執行會更加簡單。
cloud_anchor_java
是多人遊戲用途的實作。它會使用 API 金鑰授權 ARCore API 呼叫。
必要條件
需求條件
硬體
軟體
- 搭載 Android SDK Platform 7.0 (API 級別 24) 以上版本的 Android Studio 3.0 以上版本
- 適用於 Android 的 ARCore SDK 有兩種方法:
- 從 GitHub 下載並解壓縮至電腦
- 使用下列指令複製存放區:
git clone https://github.com/google-ar/arcore-android-sdk.git
在 Android Studio 中開啟範例應用程式
ARCore SDK 提供 cloud_anchor_java
和 persistent_cloud_anchor_java
範例應用程式,以示範 Cloud Anchors 功能。請按照下列步驟在 Android Studio 中開啟應用程式。
永久 Cloud 錨點
在 Android Studio 中按一下「Open」。
前往您電腦上儲存 arcore-android-sdk 目錄的位置。請勿開啟整個 SDK 資料夾。請改為前往範例 >persistent_cloud_anchor_java,然後點選「Open」。
Cloud 錨點
在 Android Studio 中按一下「Open」。
前往您電腦上儲存 arcore-android-sdk 目錄的位置。請勿開啟整個 SDK 資料夾。請改為前往範例 >cloud_anchor_java,然後按一下「Open」cloud_anchor_java。
設定 Cloud Anchor ID 共用功能
「Cloud Anchor ID」是識別代管 Cloud Anchor 的字串。它們可用於解析或轉譯附加至代管錨定廣告的 3D 物件。
永久 Cloud 錨點
Cloud Anchor ID 共用會保留在本機應用程式中。你不必在這裡採取任何行動。
Cloud 錨點
cloud_anchor_java
範例應用程式會使用 Firebase 的即時資料庫,在裝置之間共用 Cloud Anchor ID。您可以在自己的應用程式中使用其他解決方案。
- 在應用程式中手動新增 Firebase。
cloud_anchor_java
套件名稱為 com.google.ar.core.examples.java.cloudanchor
。你可以在 main/AndroidManifest.xml
中找到這項資訊。
- 下載您在應用程式中加入 Firebase 時產生的
google-services.json
檔案。
- 透過 Firebase 建立即時資料庫。
- 在 Android Studio 中,將
google-services.json
檔案新增至專案的 app
目錄。
授權 ARCore API 呼叫
授權向 ARCore API 發出的呼叫託管及解析 Cloud Anchors
應用程式。按照「在 Google 上使用 ARCore API」步驟
並使用 less Keyless 服務
授權永久 Cloud Anchors 或 API 金鑰授權:
Cloud Anchors。
建構並執行範例應用程式
永久 Cloud 錨點
執行應用程式
- 確認裝置已啟用開發人員選項和 USB 偵錯。
- 透過 USB 將裝置連接至開發機器。
- 在 Android Studio 中,選取裝置做為部署目標,然後按一下「Run」。
「persistent_cloud_anchor_java
」應會在裝置上啟動,並提示 ARCore 偵測裝置相機前方的飛機。
如果 Google Cloud 授權失敗,請參閱疑難排解步驟。
放置錨點
- 應用程式開始偵測飛機後,輕觸螢幕,在偵測到的平面上放置錨點。
- 輕觸「主持人」按鈕,代管放置錨點。這會傳送主機要求至 ARCore API,其中包括代表錨點相對於其附近的視覺功能的資料。
主機要求成功後,會在放置的位置建立錨點,並為其指派 Cloud Anchor ID。如果主機要求成功,應用程式應顯示會議室代碼。使用這個程式碼,即可在任何裝置上存取這個房間先前代管的錨定標記。
解析錨定標記
- 輕觸「解決」並輸入先前傳回的會議室代碼,即可存取這個房間代管的錨點。這會傳送解析要求至 ARCore API,該 API 會傳回目前在遊戲室中代管所有錨點的 ID。
persistent_cloud_anchor_java
會使用這些 ID 算繪附加至代管錨點的 3D 物件。
Cloud 錨點
執行應用程式
- 確認裝置已啟用開發人員選項和 USB 偵錯。
- 透過 USB 將裝置連接至開發機器。
- 在 Android Studio 中,選取裝置做為部署目標,然後按一下「Run」。
「cloud_anchor_java
」應會在裝置上啟動,並提示 ARCore 偵測裝置相機前方的飛機。
錯誤:com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance
如果發生這個錯誤,請確認 firebase_url
屬性出現在 google-services.json
中。只要確保已建立即時資料庫並下載 google-services.json
,即可取得這個屬性的正確值。
放置錨點
- 應用程式開始偵測飛機後,輕觸螢幕,在偵測到的平面上放置錨點。
- 輕觸「主持人」按鈕,代管放置錨點。這會傳送主機要求至 ARCore API,其中包括代表錨點相對於其附近的視覺功能的資料。
主機要求成功後,會在放置的位置建立錨點,並為其指派 Cloud Anchors ID。如果主機要求成功,應用程式應顯示會議室代碼。使用這個程式碼,即可在任何裝置上存取這個房間先前代管的錨定標記。
解析錨定標記
- 輕觸「解決」並輸入先前傳回的會議室代碼,即可存取這個房間代管的錨點。這會傳送解析要求至 ARCore API,該 API 會傳回目前在遊戲室中代管所有錨點的 ID。
cloud_anchor_java
會使用這些 ID 算繪附加至代管錨點的 3D 物件。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\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)."]]