You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Application Credential是什么?Android集成Google Speech API认证疑问

Understanding GOOGLE_APPLICATION_CREDENTIALS & Service Account JSON in Android

Hey there! Let's clear up your confusion step by step—this is a common point of friction when moving from server-side Google API examples to Android.

What is GOOGLE_APPLICATION_CREDENTIALS?

It's an environment variable used by Google's server-side/desktop SDKs to locate your service account key file (the JSON file you downloaded from Google Cloud Console). When set, the SDK automatically loads the credentials from that path, so you don't have to hardcode authentication details in your code.

But wait—this doesn't work directly on Android!

Android apps can't easily access system-level environment variables like server-side apps can. The Gradle code from the example is likely intended for backend/desktop projects, so you'll need a different approach for your Android app.

Where to put your JSON credential file & how to use it

Follow these steps:

  1. Create the raw resource directory: In your Android project, go to app/src/main/res/—if there's no raw folder there, right-click res → New → Android Resource Directory, set "Resource type" to raw, then click OK.
  2. Add your JSON file: Copy your service account JSON file into this raw folder. Rename it to something simple with lowercase letters and no spaces (e.g., speech_credentials.json—Android resource names can't have special characters).
  3. Load credentials in code manually: Instead of relying on the environment variable, read the file directly from your resources and initialize the Speech API client with it.

Example code (Kotlin):

import com.google.api.gax.core.FixedCredentialsProvider
import com.google.auth.oauth2.GoogleCredentials
import com.google.cloud.speech.v1.SpeechClient
import com.google.cloud.speech.v1.SpeechSettings

// Inside your Activity/Fragment
fun initSpeechClient() {
    try {
        // Read the credential file from raw resources
        val inputStream = resources.openRawResource(R.raw.speech_credentials)
        val credentials = GoogleCredentials.fromStream(inputStream)
        
        // Configure SpeechSettings with the loaded credentials
        val speechSettings = SpeechSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
            .build()
        
        // Initialize the SpeechClient
        val speechClient = SpeechClient.create(speechSettings)
        
        // Use speechClient for your API calls here
    } catch (e: Exception) {
        // Handle errors (e.g., file not found, invalid credentials)
        e.printStackTrace()
    }
}

Example code (Java):

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechSettings;
import java.io.InputStream;

// Inside your Activity/Fragment
public void initSpeechClient() {
    try {
        // Read the credential file from raw resources
        InputStream inputStream = getResources().openRawResource(R.raw.speech_credentials);
        GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream);
        
        // Configure SpeechSettings with the loaded credentials
        SpeechSettings speechSettings = SpeechSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
            .build();
        
        // Initialize the SpeechClient
        SpeechClient speechClient = SpeechClient.create(speechSettings);
        
        // Use speechClient for your API calls here
    } catch (Exception e) {
        // Handle errors (e.g., file not found, invalid credentials)
        e.printStackTrace();
    }
}

Important Security Note

Storing a service account JSON directly in your Android APK carries risk—since APKs can be decompiled, someone could extract your credentials. For production apps, it's better to set up a backend server that handles the Speech API calls, and have your Android app communicate with your backend instead. This way, your credentials stay secure on the server.

内容的提问来源于stack exchange,提问作者Akash Mishra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:42:38