เปิดใช้ AR เพื่อใช้ฟีเจอร์ Augmented Reality ในแอปใหม่หรือแอปที่มีอยู่
กำหนดค่าแอปให้เป็น AR Required หรือ AR Optional
ฟีเจอร์ AR ทั้งหมดจะจัดเก็บไว้ในแอปชื่อบริการ Google Play สำหรับ AR ซึ่ง Play Store จะอัปเดตแยกต่างหาก เพื่อประหยัดพื้นที่ในอุปกรณ์แต่ละเครื่อง แอป Android ที่ใช้ฟีเจอร์ AR จะสื่อสารกับบริการ Google Play สำหรับ AR โดยใช้ ARCore SDK คุณกำหนดค่าแอปที่รองรับฟีเจอร์ AR ได้ 2 วิธี ได้แก่ ต้องใช้ AR และใช้ AR หรือไม่ก็ได้ การกำหนดนี้จะกำหนดวิธีที่แอป AR โต้ตอบกับแอปบริการ Google Play สำหรับ AR
แอปที่ต้องใช้ AR จะทำงานไม่ได้หากไม่มี ARCore โดยต้องใช้อุปกรณ์ที่รองรับ ARCore ซึ่งติดตั้งบริการ Google Play สำหรับ AR
- Google Play Store จะแสดงเฉพาะแอปที่ต้องใช้ AR ในอุปกรณ์ที่รองรับ ARCore เท่านั้น
- เมื่อผู้ใช้ติดตั้งแอปที่ต้องใช้ AR ทาง Google Play Store จะติดตั้งบริการ Google Play สำหรับ AR ในอุปกรณ์ของผู้ใช้โดยอัตโนมัติ อย่างไรก็ตาม แอปของคุณยังคงต้องทำการตรวจสอบเพิ่มเติมขณะรันไทม์ในกรณีที่บริการ Google Play สำหรับ AR ล้าสมัยหรือถูกถอนการติดตั้งด้วยตนเอง
แอป AR Optional ใช้ ARCore เพื่อปรับปรุงฟังก์ชันการทำงานที่มีอยู่ โดยมีฟีเจอร์ AR ที่ไม่บังคับซึ่งจะเปิดใช้งานในอุปกรณ์ที่รองรับ ARCore และติดตั้งบริการ Google Play สำหรับ AR เท่านั้น
- แอป AR ที่ไม่บังคับจะติดตั้งและเรียกใช้ได้ในอุปกรณ์ที่ไม่รองรับ ARCore
- เมื่อผู้ใช้ติดตั้งแอป AR ที่ไม่บังคับ Google Play Store จะไม่ติดตั้งบริการ Google Play สำหรับ AR ในอุปกรณ์โดยอัตโนมัติ
| ต้องใช้ AR | AR ไม่บังคับ | |
|---|---|---|
| การใช้งานฟีเจอร์ AR | แอปของคุณต้องใช้ ARCore สำหรับฟังก์ชันพื้นฐาน | ARCore จะเพิ่มฟังก์ชันการทำงานของแอป แอปของคุณจะทำงานได้โดยไม่ต้องรองรับ ARCore |
| ระดับการเข้าถึง Play Store | แอปของคุณจะแสดงใน Play Store เฉพาะในอุปกรณ์ที่รองรับ ARCore | แอปของคุณทำตามขั้นตอนการแสดงข้อมูลตามปกติ |
| วิธีการติดตั้งบริการ Google Play สำหรับ AR | Play Store จะติดตั้งบริการ Google Play สำหรับ AR ควบคู่ไปกับแอปของคุณ | แอปของคุณใช้
ArCoreApk.requestInstall()
เพื่อดาวน์โหลดและติดตั้ง ARCore |
ข้อกำหนดของ Android minSdkVersion |
Android 7.0 (API ระดับ 24) | Android 4.4 (API ระดับ 19) แต่การเรียกใช้ฟังก์ชัน AR ใดๆ ต้องใช้ Android 7.0 (API ระดับ 24) เป็นอย่างน้อย |
ต้องใช้ ArCoreApk_checkAvailability() หรือ ArCoreApk_checkAvailabilityAsync() เพื่อตรวจสอบการรองรับ ARCore และสถานะการติดตั้ง
|
||
ต้องใช้
ArCoreApk.requestInstall()
เพื่อติดตั้งบริการ Google Play สำหรับ AR |
หากต้องการทำให้แอปเป็น AR Required หรือ AR Optional ให้อัปเดต AndroidManifest.xml เพื่อรวมรายการต่อไปนี้
ต้องใช้ AR
<uses-permission android:name="android.permission.CAMERA" />
<!-- Limits app visibility in the Google Play Store to ARCore supported devices
(https://developers.google.com/ar/devices). -->
<uses-feature android:name="android.hardware.camera.ar" />
<application …>
…
<!-- "AR Required" app, requires "Google Play Services for AR" (ARCore)
to be installed, as the app does not include any non-AR features. -->
<meta-data android:name="com.google.ar.core" android:value="required" />
</application>
AR ไม่บังคับ
<uses-permission android:name="android.permission.CAMERA" />
<!-- If your app was previously AR Required, don't forget to remove the
`<uses-feature android:name="android.hardware.camera.ar" />` entry, as
this would limit app visibility in the Google Play Store to only
ARCore supported devices. -->
<application …>
…
<!-- "AR Optional" app, contains non-AR features that can be used when
"Google Play Services for AR" (ARCore) is not available. -->
<meta-data android:name="com.google.ar.core" android:value="optional" />
</application>
จากนั้นแก้ไข build.gradle ของแอปเพื่อระบุ minSdkVersion เป็นอย่างน้อย 24
android {
defaultConfig {
…
minSdkVersion 24
}
}
เพิ่มการพึ่งพิงบิวด์
ตรวจสอบว่าไฟล์
build.gradleของโปรเจ็กต์มีที่เก็บ Maven ของ Googleallprojects { repositories { google() … } }เพิ่มงานที่กำหนดเองลงในไฟล์
build.gradleของโมดูลเพื่อแยกไลบรารีเนทีฟที่รวมไว้จากไฟล์ AAR ของ ARCore ด้วยวิธีนี้ คุณจึงอ้างอิงไฟล์เหล่านี้ได้โดยตรงในโปรเจ็กต์ C หรือ C++ในไดเรกทอรี
app/buildให้กำหนดตัวแปรไปยังไดเรกทอรีที่จะแยกไลบรารีที่มาพร้อมเครื่องสร้างการกำหนดค่า Gradle เพื่อจัดเก็บข้อมูลและงานการแยกข้อมูล
/* The ARCore AAR library contains native shared libraries that are extracted before building to a temporary directory. */ def arcore_libpath = "${buildDir}/arcore-native" // Create a configuration to mark which aars to extract .so files from configurations { natives }สร้างงานเพื่อคัดลอกไลบรารีเนทีฟจากไฟล์ AAR แล้วเพิ่มลงในการพึ่งพิงบิลด์
// Extracts the shared libraries from AARs in the native configuration // so that NDK builds can access these libraries. task extractNativeLibraries() { // Extract every time. outputs.upToDateWhen { false } doFirst { configurations.natives.files.each { f -> copy { from zipTree(f) into arcore_libpath include "jni/**/*" } } } } tasks.whenTaskAdded { task-> if (task.name.contains("external") && !task.name.contains("Clean")) { task.dependsOn(extractNativeLibraries) } }กำหนดค่าแฟล็กการสร้างเนทีฟเพื่อส่งตำแหน่งไปยังเครื่องมือบิลด์ภายนอก
// From the sample app. externalNativeBuild { cmake { cppFlags "-std=c++11", "-Wall" arguments "-DANDROID_STL=c++_static", "-DARCORE_LIBPATH=${arcore_libpath}/jni", "-DARCORE_INCLUDE=${project.rootDir}/../../libraries/include" } }เพิ่มทรัพยากร Dependency สำหรับทั้งไลบรารี Java และไลบรารีเนทีฟ
dependencies { ... // Add Java and native dependencies to the ARCore library. implementation 'com.google.ar:core:1.33.0' natives 'com.google.ar:core:1.33.0' ... }อ้างอิงไลบรารีที่มาพร้อมเครื่องใน
CMakeLists.txt# Import the ARCore library. add_library(arcore SHARED IMPORTED) set_target_properties(arcore PROPERTIES IMPORTED_LOCATION ${ARCORE_LIBPATH}/${ANDROID_ABI}/libarcore_sdk_c.so INTERFACE_INCLUDE_DIRECTORIES ${ARCORE_INCLUDE} )
ทำการตรวจสอบรันไทม์
ในระหว่างรันไทม์ ให้ทำดังนี้เพื่อให้แน่ใจว่าฟีเจอร์ AR ในแอปทำงานได้อย่างราบรื่น
ตรวจสอบว่าระบบรองรับ ARCore หรือไม่
ทั้งแอปที่ต้องใช้ AR และแอปที่ใช้ AR ได้ควรใช้ArCoreApk_checkAvailability() หรือ ArCoreApk_checkAvailabilityAsync() เพื่อตรวจสอบว่าอุปกรณ์ปัจจุบันรองรับ ARCore หรือไม่ ในอุปกรณ์ที่ไม่รองรับ ARCore แอปควรปิดใช้ฟังก์ชันการทำงานที่เกี่ยวข้องกับ AR และซ่อนองค์ประกอบ UI ที่เกี่ยวข้อง
แอป Android NDK อาจใช้คลาส Java ArCoreApk เพื่อตรวจสอบความเข้ากันได้และจัดการการติดตั้งใน ARCore Session API ของ C ดั้งเดิม การดำเนินการนี้อาจง่ายกว่าการใช้ฟังก์ชัน ArCoreApk_ เนื่องจากต้องมีการจัดการข้อผิดพลาดและการโต้ตอบกับอินเทอร์เฟซผู้ใช้จำนวนมาก ทั้งนี้ขึ้นอยู่กับโครงสร้างของแอป
void maybeEnableArButton(JNIEnv env, jobject context) {
// Likely called from Activity.onCreate() of an activity with AR buttons.
ArAvailability availability
ArCoreApk_checkAvailability(env, context, &availability);
if (availability == AR_AVAILABILITY_UNKNOWN_CHECKING) {
// Set a timer to call maybeEnableArButton() again after about 200ms.
}
if (availability == AR_AVAILABILITY_SUPPORTED_NOT_INSTALLED ||
availability == AR_AVAILABILITY_SUPPORTED_APK_TOO_OLD ||
availability == AR_AVAILABILITY_SUPPORTED_INSTALLED) {
// Show or enable the AR button.
} else {
// Hide or disable the AR button.
}
}
ArCoreApk_checkAvailability() หรือ ArCoreApk_checkAvailabilityAsync() เพื่อตรวจสอบการรองรับ ARCore จะช่วยให้ผู้ใช้ได้รับประสบการณ์การใช้งานที่สม่ำเสมอ
ArCoreApk_checkAvailability() อาจต้องค้นหาทรัพยากรเครือข่ายเพื่อพิจารณาว่าอุปกรณ์รองรับ ARCore หรือไม่ ในระหว่างนี้ ฟังก์ชันจะแสดงผล AR_AVAILABILITY_UNKNOWN_CHECKING แอปควรเรียกใช้ ArCoreApk_checkAvailability() 1 ครั้งในช่วงต้นของวงจรการใช้งานเพื่อเริ่มการค้นหาโดยไม่ต้องสนใจค่าที่ส่งคืน เพื่อลดเวลาในการตอบสนองที่รับรู้ได้และการแสดงผลแบบป๊อปอิน ด้วยวิธีนี้ ผลการค้นหาที่แคชไว้จะพร้อมใช้งานทันทีเมื่อระบบอาจแสดงองค์ประกอบ UI สำหรับการเข้าสู่ AR อีกครั้ง
ตรวจสอบว่าได้ติดตั้งบริการ Google Play สำหรับ AR แล้ว
ทั้งแอปที่ต้องใช้ AR และแอปที่ใช้ AR หรือไม่ก็ได้ต้องใช้
ArCoreApk.requestInstall()
ก่อนสร้างเซสชัน ARCore เพื่อตรวจสอบว่าได้ติดตั้งบริการ Google Play สำหรับ AR เวอร์ชันที่เข้ากันได้ (ยังคง) หรือไม่ และเพื่อให้แน่ใจว่าได้ดาวน์โหลดข้อมูลโปรไฟล์อุปกรณ์ ARCore ที่จำเป็นทั้งหมดแล้ว
// Tracks if an installation request has already been triggered.
bool install_requested_;
void nativeOnCreate() {
// Do other setup here.
install_requested_ = false;
}
void nativeOnResume(JNIEnv env, jobject activity) {
if (ar_session_ == null) {
bool user_requested_install = !install_requested_;
ArInstallStatus install_status;
// Ensure that Google Play Services for AR and ARCore device profile data are
// installed and up to date.
ArStatus error = ArCoreApk_requestInstall(
env, activity, user_requested_install, &install_status);
if (error != AR_SUCCESS) {
// Inform user of error.
return;
}
switch (install_status) {
case AR_INSTALL_STATUS_INSTALLED:
break;
case AR_INSTALL_STATUS_INSTALL_REQUESTED:
// When this method returns AR_INSTALL_STATUS_INSTALL_REQUESTED:
// 1. This activity will be paused.
// 2. The user is prompted to install or update Google Play
// Services for AR (market://details?id=com.google.ar.core).
// 3. ARCore downloads the latest device profile data.
// 4. This activity is resumed. The next invocation of
// ArCoreApk_requestInstall() will either return
// AR_INSTALL_STATUS_INSTALLED or throw an exception if the
// installation or update did not succeed.
install_requested_ = true;
return;
}
// Request camera permissions.
error = ArSession_create(env, context, &ar_session_);
if (error != AR_SUCCESS) {
// Inform user of error.
return;
}
// Configure the ARCore session.
}
// Normal onResume behavior.
}
ปฏิบัติตามข้อกำหนดด้านความเป็นส่วนตัวของผู้ใช้
หากต้องการเผยแพร่แอปใน Play Store โปรดตรวจสอบว่าแอปเป็นไปตามข้อกำหนดด้านความเป็นส่วนตัวของผู้ใช้ของ ARCore
สิ่งที่จะเกิดขึ้นหลังจากนี้
- ดูวิธีกำหนดค่าเซสชัน ARCore