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

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

アプリを「AR 必須」または「AR オプション」に設定する

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

AR 必須アプリは、ARCore なしでは動作できません。ご利用には、AR 用 Google Play 開発者サービスがインストールされた ARCore 対応デバイスが必要です。

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

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

  • AR オプション アプリは、ARCore に対応していないデバイスにインストールして実行できます。
  • ユーザーが AR オプション アプリをインストールしても、Google Play ストアによってデバイスに Google Play 開発者サービス(AR)が自動的にインストールされることはありません
AR が必要AR(オプション)
AR 機能の使用 アプリの基本的な機能には ARCore が必要です。 ARCore はアプリの機能を強化します。アプリは ARCore サポートなしでも実行できます。
Google Play ストアの公開設定 お客様のアプリは、ARCore をサポートするデバイスの Google Play ストアにのみ掲載されます。 アプリは通常のリスト手順に沿っています。
Google Play 開発者サービス(AR)のインストール方法 Google Play ストアは、アプリとともに Google Play 開発者サービス(AR)をインストールします。 アプリで使用されている <ph type="x-smartling-placeholder"></ph> ArCoreApk.requestInstall() ARCore をダウンロードしてインストールします。
Android minSdkVersion の要件 Android 7.0(API レベル 24) Android 4.4(API レベル 19)ですが、AR 機能を実行するには Android 7.0(API レベル 24)以降が必要です
ARCore のサポートとインストール ステータスを確認するには、ArCoreApk.checkAvailability() または ArCoreApk.checkAvailabilityAsync() を使用する必要があります
必須 <ph type="x-smartling-placeholder"></ph> 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 を変更して、少なくとも 24minSdkVersion を指定します。

 android {
     defaultConfig {
         …
         minSdkVersion 24
     }
 }

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

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

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

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

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

ランタイム チェックを行う

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

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

AR 必須アプリと AR オプション アプリはどちらも、ArCoreApk.checkAvailability() または ArCoreApk.checkAvailabilityAsync() を使用して、現在のデバイスが ARCore に対応しているかどうかを判断する必要があります。ARCore をサポートしていないデバイスでは、アプリは AR 関連の機能を無効にして、関連する UI 要素を非表示にする必要があります。

Kotlin

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
    }
  }
}

Java

@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);
    }
  });
}
AR 必須アプリとともに Google Play 開発者サービス(AR)がインストールされていても、サポートされていないデバイスを使用しているユーザーが外部ソースからインストールする可能性があります。ArCoreApk.checkAvailability() または ArCoreApk.checkAvailabilityAsync() を使用して ARCore のサポートを確認することで、一貫したエクスペリエンスを実現できます。

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

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

AR 必須アプリと AR オプション アプリの両方で使用が必須 <ph type="x-smartling-placeholder"></ph> ArCoreApk.requestInstall() を使用して、ARCore セッションを作成する前に、互換性のあるバージョンの Google Play 開発者サービス(AR)が(まだ)インストールされているかどうか、必要な ARCore デバイス プロファイル データがすべてダウンロードされていることを確認します。

Kotlin

// 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.
  }
  …
}

Java

// 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 セッションを作成する前にカメラの権限が付与されていることを確認する必要があります。

Kotlin

override fun onResume() {
  super.onResume()

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

  …
}

Java

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

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

  …
}

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

Kotlin

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()
  }
}

Java

@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 の ユーザーのプライバシー要件

次のステップ