Android アプリで AR を有効にする

AR を有効にして、新規または既存のアプリで拡張現実機能を使用します。

AR 必須または AR 任意としてアプリを構成する

個々のデバイスの容量を節約するため、すべての AR 機能は Google Play 開発者サービス(AR)というアプリに保存されます。このアプリは Google Play ストアによって個別に更新されます。AR 機能を使用している Android アプリは、ARCore SDK を使用して Google Play 開発者サービス(AR)と通信します。AR 機能をサポートするアプリは、AR 必須AR 任意の 2 つの方法で構成できます。この指定により、アプリが Google Play 開発者サービス(AR)アプリとやり取りする方法が決まります。

AR 必須のアプリは、ARCore なしでは機能しません。Google Play 開発者サービス(AR)がインストールされている ARCore 対応デバイスが必要です。

  • Google Play ストアでは、ARCore をサポートするデバイスでのみ、AR 必須アプリを利用できるようになります。
  • ユーザーが AR 必須アプリをインストールすると、Google Play ストアから Google Play 開発者サービス(AR)がデバイスに自動的にインストールされます。ただし、Google Play 開発者サービス for AR が古くなっている場合や手動でアンインストールされている場合を考慮して、アプリは追加のランタイム チェックを実行する必要があります。

AR オプションのアプリは、ARCore を使用して既存の機能を強化します。オプションの AR 機能は、Google Play 開発者サービス(AR)がインストールされている ARCore 対応デバイスでのみ有効になります。

  • AR オプション アプリは、ARCore に対応していないデバイスにインストールして実行できます。
  • ユーザーが AR オプション アプリをインストールしても、Google Play ストアから Google Play 開発者サービス(AR)がデバイスに自動的にインストールされることはありません。
AR 必須AR オプション
AR 機能の使用状況 アプリの基本機能に ARCore が必要である。 ARCore はアプリの機能を拡張します。アプリは ARCore のサポートなしで実行できます。
Play ストアの公開設定 アプリは、ARCore をサポートするデバイスの Google Play ストアにのみ表示されます。 アプリは通常の掲載手順に沿って掲載されています。
Google Play 開発者サービス(AR)のインストール方法 Google Play ストアで、アプリと一緒に 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 必須または AR オプションにするには、AndroidManifest.xml を更新して次のエントリを含めます。

<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>
<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 を変更して、24 以上の minSdkVersion を指定します。

 android {
     defaultConfig {
         
         minSdkVersion 24
     }
 }

ビルド依存関係を追加する

Android Studio プロジェクトに ARCore を追加する手順は次のとおりです。

  1. プロジェクトの build.gradle ファイルに Google の Maven リポジトリが含まれていることを確認します。

    allprojects {
        repositories {
            google()
            
        }
    }
    
  2. 最新の ARCore ライブラリをアプリの build.gradle ファイルに依存関係として追加します。

    dependencies {
        
        implementation 'com.google.ar:core:1.33.0'
    }
    

ランタイム チェックを実行する

実行時に、アプリの AR 機能がスムーズに動作するように次の操作を行います。

ARCore がサポートされているかどうかを確認する

AR 必須アプリと AR 任意アプリの両方で、ArCoreApk.checkAvailability() または ArCoreApk.checkAvailabilityAsync() を使用して、現在のデバイスが ARCore をサポートしているかどうかを判断する必要があります。ARCore をサポートしていないデバイスでは、アプリは AR 関連の機能を無効にし、関連する UI 要素を非表示にする必要があります。
KotlinJava
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  // Enable AR-related functionality on ARCore supported devices only.
  maybeEnableArButton()
  
}

fun maybeEnableArButton() {
  ArCoreApk.getInstance().checkAvailabilityAsync(this) { availability ->
    if (availability.isSupported) {
      mArButton.visibility = View.VISIBLE
      mArButton.isEnabled = true
    } else { // The device is unsupported or unknown.
      mArButton.visibility = View.INVISIBLE
      mArButton.isEnabled = false
    }
  }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Enable AR-related functionality on ARCore supported devices only.
  maybeEnableArButton();
  
}

void maybeEnableArButton() {
  ArCoreApk.getInstance().checkAvailabilityAsync(this, availability -> {
    if (availability.isSupported()) {
      mArButton.setVisibility(View.VISIBLE);
      mArButton.setEnabled(true);
    } else { // The device is unsupported or unknown.
      mArButton.setVisibility(View.INVISIBLE);
      mArButton.setEnabled(false);
    }
  });
}
Google Play 開発者サービス(AR)が AR 必須アプリと一緒にインストールされている場合でも、サポートされていないデバイスを使用しているユーザーが外部ソースからインストールする可能性があります。ArCoreApk.checkAvailability() または ArCoreApk.checkAvailabilityAsync() を使用して ARCore のサポートを確認することで、一貫したエクスペリエンスを実現できます。

ArCoreApk.checkAvailability() は、デバイスが ARCore をサポートしているかどうかを判断するために、ネットワーク リソースをクエリする必要がある場合があります。この間、UNKNOWN_CHECKING が返されます。認識されるレイテンシとポップインを減らすには、アプリはライフサイクルの早い段階で ArCoreApk.checkAvailability() を 1 回呼び出してクエリを開始し、返された値を無視する必要があります。これにより、AR への入力 UI 要素が表示される可能性があるときに、キャッシュに保存された結果をすぐに利用できるようになります。

Google Play 開発者サービス(AR)がインストールされているかどうかを確認する

AR 必須アプリと AR 任意アプリの両方で、ARCore セッションを作成する前に ArCoreApk.requestInstall() を使用して、対応バージョンの Google Play 開発者サービス(AR)が(まだ)インストールされているかどうかを確認し、必要な ARCore デバイス プロファイル データがすべてダウンロードされていることを確認する必要があります。

KotlinJava
// requestInstall(Activity, true) will triggers installation of
// Google Play Services for AR if necessary.
var mUserRequestedInstall = true

override fun onResume() {
  super.onResume()

  // Check camera permission.
  

  // Ensure that Google Play Services for AR and ARCore device profile data are
  // installed and up to date.
  try {
    if (mSession == null) {
      when (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
        ArCoreApk.InstallStatus.INSTALLED -> {
          // Success: Safe to create the AR session.
          mSession = Session(this)
        }
        ArCoreApk.InstallStatus.INSTALL_REQUESTED -> {
          // When this method returns `INSTALL_REQUESTED`:
          // 1. ARCore pauses this activity.
          // 2. ARCore prompts the user 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. ARCore resumes this activity. The next invocation of
          //    requestInstall() will either return `INSTALLED` or throw an
          //    exception if the installation or update did not succeed.
          mUserRequestedInstall = false
          return
        }
      }
    }
  } catch (e: UnavailableUserDeclinedInstallationException) {
    // Display an appropriate message to the user and return gracefully.
    Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
        .show()
    return
  } catch () {
    
    return  // mSession remains null, since session creation has failed.
  }
  
}
// requestInstall(Activity, true) will trigger installation of
// Google Play Services for AR if necessary.
private boolean mUserRequestedInstall = true;

@Override
protected void onResume() {
  super.onResume();

  // Check camera permission.
  

  // Ensure that Google Play Services for AR and ARCore device profile data are
  // installed and up to date.
  try {
    if (mSession == null) {
      switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
        case INSTALLED:
          // Success: Safe to create the AR session.
          mSession = new Session(this);
          break;
        case INSTALL_REQUESTED:
          // When this method returns `INSTALL_REQUESTED`:
          // 1. ARCore pauses this activity.
          // 2. ARCore prompts the user 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. ARCore resumes this activity. The next invocation of
          //    requestInstall() will either return `INSTALLED` or throw an
          //    exception if the installation or update did not succeed.
          mUserRequestedInstall = false;
          return;
      }
    }
  } catch (UnavailableUserDeclinedInstallationException e) {
    // Display an appropriate message to the user and return gracefully.
    Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
        .show();
    return;
  } catch () {
    
    return;  // mSession remains null, since session creation has failed.
  }
  
}

カメラの権限をリクエストする

AR がオプションのアプリと AR が必須のアプリの両方で、AR セッションを作成する前にカメラの権限が付与されていることを確認する必要があります。

KotlinJava
override fun onResume() {
  super.onResume()

  // ARCore requires camera permission to operate.
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    CameraPermissionHelper.requestCameraPermission(this)
    return
  }

  
}
@Override
protected void onResume() {
  super.onResume();

  // ARCore requires camera permission to operate.
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    CameraPermissionHelper.requestCameraPermission(this);
    return;
  }

  
}

AR アクティビティは onRequestPermissionsResult() も実装する必要があります。

KotlinJava
override fun onRequestPermissionsResult(
  requestCode: Int,
  permissions: Array<String>,
  results: IntArray
) {
  super.onRequestPermissionsResult(requestCode, permissions, results)
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
      .show()
    if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
      // Permission denied with checking "Do not ask again".
      CameraPermissionHelper.launchPermissionSettings(this)
    }
    finish()
  }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
  super.onRequestPermissionsResult(requestCode, permissions, results);
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
        .show();
    if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
      // Permission denied with checking "Do not ask again".
      CameraPermissionHelper.launchPermissionSettings(this);
    }
    finish();
  }
}

ユーザーのプライバシーに関する要件に準拠する

アプリを Google Play ストアで公開するには、アプリが ARCore のユーザー プライバシー要件に準拠していることを確認してください。

次のステップ