Google Application Credential是什么?Android集成Google Speech API认证疑问
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:
- Create the
rawresource directory: In your Android project, go toapp/src/main/res/—if there's norawfolder there, right-clickres→ New → Android Resource Directory, set "Resource type" toraw, then click OK. - Add your JSON file: Copy your service account JSON file into this
rawfolder. Rename it to something simple with lowercase letters and no spaces (e.g.,speech_credentials.json—Android resource names can't have special characters). - 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

