ARCore กลับมาในงาน Google I/O ในวันที่ 10 พฤษภาคม ลงทะเบียนเลย

เปิดใช้ AR ในแอป NDK สําหรับ Android

เปิดใช้ AR เพื่อใช้ฟีเจอร์ Augmented Reality ในแอปใหม่หรือที่มีอยู่

กําหนดค่าแอปให้เป็นไปตาม AR หรือ AR ไม่บังคับ

หากต้องการประหยัดพื้นที่ในอุปกรณ์แต่ละเครื่อง ฟีเจอร์ AR ทั้งหมดจะจัดเก็บอยู่ในแอปที่ชื่อว่า Google Play Services สําหรับ AR ที่ Play Store อัปเดตแยกต่างหาก แอป Android ที่ใช้ฟีเจอร์ AR จะสื่อสารกับบริการ Google Play สําหรับ AR โดยใช้ ARCore SDK แอปที่รองรับฟีเจอร์ AR ทําได้ 2 วิธีดังนี้ต้องมี 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 ไม่บังคับใช้ ARCore เพื่อเพิ่มประสิทธิภาพฟังก์ชันที่มีอยู่ ซึ่งมีฟีเจอร์ AR ที่ไม่บังคับซึ่งเปิดใช้งานเฉพาะในอุปกรณ์ที่รองรับ ARCore ที่ติดตั้งบริการ Google Play สําหรับ AR เท่านั้น

  • คุณติดตั้งแอปและเรียกใช้ AR ไม่บังคับได้ในอุปกรณ์ที่ไม่รองรับ ARCore
  • เมื่อผู้ใช้ติดตั้งแอปที่ไม่บังคับสําหรับ AR แอป Google Play Store จะไม่ติดตั้งบริการ Google Play สําหรับ AR ในอุปกรณ์โดยอัตโนมัติ
ต้องมี ARAR ที่ไม่บังคับ
การใช้ฟีเจอร์ 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 7.0 (API ระดับ 24)
ต้องใช้ ArCoreApk_checkAvailability() เพื่อตรวจสอบการรองรับ ARCore และสถานะการติดตั้ง
ต้องใช้ ArCoreApk_requestInstall() เพื่อติดตั้งบริการ Google Play สําหรับ AR

หากต้องการกําหนดให้แอปต้องใช้ AR หรือ AR ไม่บังคับ ให้อัปเดต 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
     }
 }

เพิ่มการพึ่งพิงบิวด์

  1. ตรวจสอบว่าไฟล์ build.gradle ของโปรเจ็กต์มีที่เก็บ Maven ของ Google

    allprojects {
        repositories {
            google()
            …
        }
    }
    
  2. เพิ่มงานที่กําหนดเองลงในไฟล์ build.gradle ของโมดูลเพื่อดึงไลบรารีที่มาพร้อมเครื่องจากไฟล์ ARCore AAR ซึ่งสามารถใช้อ้างอิงได้โดยตรงในโปรเจ็กต์ C หรือ C++

  3. ในไดเรกทอรี app/build ให้กําหนดตัวแปรไปยังไดเรกทอรีที่จะดึงข้อมูลไลบรารีแบบเนทีฟ

  4. สร้างการกําหนดค่า 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 }
    
  5. สร้างงานเพื่อคัดลอกไลบรารีแบบเนทีฟจากไฟล์ AAR แล้วเพิ่มไปยังทรัพยากร Dependency ของบิลด์

    // 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)
        }
    }
    
  6. กําหนดค่าแฟล็กบิวด์แบบเนทีฟเพื่อส่งตําแหน่งไปยังเครื่องมือสร้างภายนอก

    // 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"
        }
    }
    
  7. เพิ่มทรัพยากร 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'
         ...
    }
    
  8. โปรดดูไลบรารีที่มาพร้อมเครื่องใน 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() เพื่อระบุว่าอุปกรณ์ปัจจุบันรองรับ ARCore หรือไม่ ในอุปกรณ์ที่ไม่รองรับ ARCore แอปควรปิดใช้ฟังก์ชันที่เกี่ยวข้องกับ AR และซ่อนองค์ประกอบ UI ที่เกี่ยวข้อง

แอป Android NDK อาจใช้คลาส Java สําหรับ ArCoreApk เพื่อตรวจสอบความเข้ากันได้และจัดการการติดตั้งใน C ARCore Session API ดั้งเดิม ซึ่งอาจจะง่ายกว่าการใช้ฟังก์ชัน 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.
  }
}

แม้ว่าบริการ Google Play สําหรับ AR จะได้รับการติดตั้งควบคู่ไปกับแอป AR ที่ต้องใช้ แต่ผู้ใช้ที่มีอุปกรณ์ที่ไม่รองรับอาจติดตั้งแอปดังกล่าวจากแหล่งที่มาภายนอกได้ การใช้ ArCoreApk_checkAvailability() เพื่อตรวจสอบการรองรับ ARCore จะช่วยให้ได้รับประสบการณ์การใช้งานที่สอดคล้องกัน

ArCoreApk_checkAvailability() อาจต้องค้นหาทรัพยากรของเครือข่ายเพื่อตรวจสอบว่าอุปกรณ์รองรับ ARCore หรือไม่ ในระหว่างนี้ อุปกรณ์จะแสดง AR_AVAILABILITY_UNKNOWN_CHECKING เพื่อลดเวลาในการตอบสนองที่รับรู้และป๊อปอัป แอปควรเรียกใช้ ArCoreApk_checkAvailability() ในช่วงต้นของวงจรเพื่อเริ่มค้นหาโดยไม่สนใจค่าที่ส่งคืน ด้วยวิธีนี้ ผลการค้นหาที่แคชจะพร้อมใช้งานทันทีเมื่ออาจแสดงองค์ประกอบ 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

สิ่งที่จะเกิดขึ้นหลังจากนี้