Firebase ML Android アプリの Cloud 認証情報を保護する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android アプリが Firebase ML の Cloud APIs のいずれかを使用している場合、本番環境でアプリをリリースする前に、不正な API アクセスを防ぐためにいくつかの追加手順を行う必要があります。
本番環境用のアプリでは、認証されたクライアントだけがクラウド サービスにアクセスできるようにします(説明された方法で認証できるのは root 権限が取得されていないデバイスのみであることに注意してください)。
次に、テストと開発の際に便宜上使用できるデバッグ専用の API キーを作成します。
1. Firebase で本番環境用アプリを登録する
まず、本番環境用アプリを Firebase に登録します。
アプリの SHA-1 署名があることを確認します。方法については、クライアントの認証をご覧ください。
Firebase コンソールの [settings] > [プロジェクトの設定] に移動し、[設定] タブを選択します。
[マイアプリ] カードまで下にスクロールし、Android アプリを選択します。
アプリの情報に SHA-1 署名を追加します。
2. API キーのスコープを制限する
次に、既存の API キーを構成して、Cloud Vision API へのアクセスを許可しないようにします。
Google Cloud コンソールの [認証情報] ページを開きます。画面の指示に沿って、プロジェクトを選択します。
リスト内にある既存の API キーごとに、編集ビューを開きます。
[API の制限] セクションで [キーを制限] を選択し、API キーによるアクセスを許可するすべての API をリストに追加します。Cloud Vision API は含めないでください。
API キーの API の制限を構成することで、キーがアクセス権を持つ API を明示的に宣言します。デフォルトでは、[API の制限] セクションで [キーを制限しない] が選択されている場合、API キーを使用してプロジェクトで有効になっているすべての API にアクセスできます。
これで、既存の API キーはクラウド ML サービスへのアクセスを許可しなくなりますが、各キーは、API の制限リストに追加したすべての API で引き続き機能します。
今後 API を追加で有効にする場合は、該当する API キーの API の制限リストにそれらを追加する必要があります。
3. デバッグ専用の API キーを作成して使用する
最後に、開発でのみ使用する新しい API キーを作成します。Firebase ML は、この API キーを使用して、アプリ認証が不可能な環境(エミュレータで実行しているときなど)で Google Cloud サービスにアクセスできます。
開発に使用する新しい API キーを作成します。
Google Cloud コンソールの [認証情報] ページを開きます。画面の指示に沿って、プロジェクトを選択します。
[認証情報を作成] > [API キー] をクリックし、新しい API キーをメモします。このキーによって、認証されていないアプリからの API アクセスが許可されるため、このキーは機密にしておいてください。
リリースされたアプリで新しいデバッグ用 API キーが漏洩しないようにするには、デバッグビルド専用の Android マニフェスト ファイルでデバッグ用 API キーを指定します。
デバッグ マニフェストがまだない場合は、[ファイル] > [新規] > [その他] > [Android マニフェスト ファイル] をクリックし、ターゲットのソースセットから debug
を選択して、デバッグ マニフェストを作成します。
デバッグ マニフェストに次の宣言を追加します。
<application>
<meta-data
android:name="com.firebase.ml.cloud.ApiKeyForDebug"
android:value="your-debug-api-key" />
</application>
アプリの Firebase ML の設定で、証明書フィンガープリントのマッチングを使用して、本番環境でクライアントを認証し、API キー(デバッグキー)をデバッグビルドでのみ使用するように構成します。
Kotlin
val optionsBuilder = FirebaseVisionCloudImageLabelerOptions.Builder()
if (!BuildConfig.DEBUG) {
// Requires physical, non-rooted device:
optionsBuilder.enforceCertFingerprintMatch()
}
// Set other options. For example:
optionsBuilder.setConfidenceThreshold(0.8f)
// ...
// And lastly:
val options = optionsBuilder.build()
FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)
Java
FirebaseVisionCloudImageLabelerOptions.Builder optionsBuilder =
new FirebaseVisionCloudImageLabelerOptions.Builder();
if (!BuildConfig.DEBUG) {
// Requires physical, non-rooted device:
optionsBuilder.enforceCertFingerprintMatch();
}
// Set other options. For example:
optionsBuilder.setConfidenceThreshold(0.8f);
// ...
// And lastly:
FirebaseVisionCloudImageLabelerOptions options = optionsBuilder.build();
FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);
次のステップ
他の Firebase 機能を使用している場合のアプリのリリース準備方法については、リリース チェックリストをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-22 UTC。
[null,null,["最終更新日 2025-08-22 UTC。"],[],[],null,["\u003cbr /\u003e\n\nIf your Android app uses one of Firebase ML's cloud APIs, before you launch your\napp in production, you should take some additional steps to prevent\nunauthorized API access.\n\nFor your production apps, you will ensure that only authenticated clients can\naccess cloud services. (Note that only non-rooted devices can authenticate using\nthe method described.)\n\nThen, you will create a debug-only API key that you can use for convenience\nduring testing and development.\n\n1. Register your production apps with Firebase\n\nFirst, register your production apps with Firebase.\n\n1. Make sure that you have your app's SHA-1 signatures. Refer to\n [Authenticating your client](//developers.google.com/android/guides/client-auth)\n to learn how.\n\n2. Go to your settings\n *Project settings* in the Firebase console, then select the *Settings*\n tab.\n\n3. Scroll down to the *Your apps* card, then select your Android app.\n\n4. Add your app's SHA-1 signature to your app's information.\n\n2. Restrict the scope of your API keys\n\nNext, configure your existing API keys to disallow access to the Cloud Vision\nAPI:\n\n1. Open the [Credentials](https://console.cloud.google.com/apis/credentials?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n2. For each existing API key in the list, open the editing view.\n\n3. In the *API restrictions* section, select **Restrict key** , then add to the\n list all of the APIs to which you want the API key to have access. Make sure\n to ***not*** include the Cloud Vision API.\n\n When you configure an API key's *API restrictions* , you are explicitly\n declaring the APIs to which the key has access. **By default, when the *API\n restrictions* section has *Don't restrict key* selected, an API key can be\n used to access any API that is enabled for the project.**\n\nNow, your existing API keys will not grant access to cloud ML services, but each\nkey will continue to work for any APIs that you added to its *API restrictions*\nlist.\n\nNote that if you enable any additional APIs in the future, you must add them to\nthe *API restrictions* list for the applicable API key.\n\n3. Create and use a debug-only API key\n\nFinally, create a new API key to be used only for development. Firebase ML can\nuse this API key to access Google Cloud services in environments where app\nauthentication isn't possible, such as when running on emulators.\n\n1. Create a new API key to be used for development:\n\n 1. Open the [Credentials](https://console.cloud.google.com/apis/credentials?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n 2. Click **Create credentials \\\u003e API key** and take note of the new API\n key. This key allows API access from unauthenticated apps, so\n **keep this key confidential**.\n\n2. To ensure the new debug API key is not leaked with your released app,\n specify the debug API key in an Android manifest file used only for debug\n builds:\n\n 1. If you don't already have a debug manifest, create one by clicking\n **File \\\u003e New \\\u003e Other \\\u003e Android Manifest File** and selecting `debug`\n from the target source sets.\n\n 2. In the debug manifest, add the following declaration:\n\n ```text\n \u003capplication\u003e\n \u003cmeta-data\n android:name=\"com.firebase.ml.cloud.ApiKeyForDebug\"\n android:value=\"your-debug-api-key\" /\u003e\n \u003c/application\u003e\n ```\n3. In your app, configure Firebase ML to use certificate fingerprint matching to\n authenticate your client in production and to use API keys---the debug\n key---only in debug builds:\n\n Kotlin \n\n ```kotlin\n val optionsBuilder = FirebaseVisionCloudImageLabelerOptions.Builder()\n if (!BuildConfig.DEBUG) {\n // Requires physical, non-rooted device:\n optionsBuilder.enforceCertFingerprintMatch()\n }\n\n // Set other options. For example:\n optionsBuilder.setConfidenceThreshold(0.8f)\n // ...\n\n // And lastly:\n val options = optionsBuilder.build()\n FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/mlkit/app/src/main/java/com/google/firebase/example/mlkit/kotlin/MainActivity.kt#L30-L42\n ```\n\n Java \n\n ```java\n FirebaseVisionCloudImageLabelerOptions.Builder optionsBuilder =\n new FirebaseVisionCloudImageLabelerOptions.Builder();\n if (!BuildConfig.DEBUG) {\n // Requires physical, non-rooted device:\n optionsBuilder.enforceCertFingerprintMatch();\n }\n\n // Set other options. For example:\n optionsBuilder.setConfidenceThreshold(0.8f);\n // ...\n\n // And lastly:\n FirebaseVisionCloudImageLabelerOptions options = optionsBuilder.build();\n FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/mlkit/app/src/main/java/com/google/firebase/example/mlkit/MainActivity.java#L30-L43\n ```\n\nNext steps\n\nSee the [launch checklist](/support/guides/launch-checklist) for information on\npreparing your app to launch when using other Firebase features."]]