Android Code Snippet Get Firebase config parameters from google-services.json

Code to get configuration parameters from google-services.json

Put this in the "Starter" service module:
B4X:
Sub Process_Globals
    Dim mapFirebaseServices As Map
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
  
    ' ---- ---- ---- ---- ---- -------- ---- -------- ---- -------- ---- ----
    ' Get Firebase related strings from google-services.json config file
    ' ---- ---- ---- ---- ---- -------- ---- -------- ---- -------- ---- ----
    Dim AndroidResources1 As AndroidResources ' warwound --> https://www.b4x.com/android/forum/threads/androidresources.16847/page-2#post-200943
    '
    ' Preparation for GetApplicationStrings():
    mapFirebaseServices.Initialize
    mapFirebaseServices.Put("default_web_client_id", "")
    mapFirebaseServices.Put("firebase_database_url", "")
    mapFirebaseServices.Put("gcm_defaultSenderId", "")
    mapFirebaseServices.Put("google_api_key", "")
    mapFirebaseServices.Put("google_app_id", "")
    mapFirebaseServices.Put("google_crash_reporting_api_key", "")
    mapFirebaseServices.Put("google_storage_bucket", "")
    mapFirebaseServices.Put("project_id", "")
    mapFirebaseServices = AndroidResources1.GetApplicationStrings(mapFirebaseServices)
    '
    ' Resulting Map:
    '             default_web_client_id         =  3695618-q745hlr3267t9eug4v3940.apps.googleusercontent.com,
    '             firebase_database_url         =  https://myprojectxyzxyzxyz.firebaseio.com,
    '             gcm_defaultSenderId             =  3695618,
    '             google_api_key                 =  AIzaasdasdasdyAWLp7crph3Vbh20OtmSFiivyX0ffH1w,
    '             google_app_id                 =  1:3695618:android:03d60330828,
    '             google_crash_reporting_api_key=  AIsdasdasdasd,
    '             google_storage_bucket         =  myprojectxyzxyzxyz.appspot.com,
    '             project_id                     =  myprojectxyzxyzxyz
    ' ---- ---- ---- ---- ---- -------- ---- -------- ---- -------- ---- ----
  
End Sub

So instead of copypasting the API keys from the Firebase console to the B4A codemodules the parameters are globally available.

The required library from warwound is located here
 
Last edited:
Top