firebase
firebase copied to clipboard
[question] firebase-core function initializeAppWithPath(path) for Android
Hello,
I am trying to load a different firebase configuration per environment. I figured that using the initializeAppWithPath()
I could point to the Google Services config files for dev and prod for Android and iOS.
For iOS It worked out of the box using the path res://
, it points to the App_Resource/iOS
folder.
However, for android I do not seem to be able to load the google-services.json
at all.
I tried with path res://
, ~/
and resolving the path myself and I always get the error firebase.init error: Error: com.tns.NativeScriptException: Failed to find module: "/data/data/com.nativescript/files/app/firebase/dev/google-services.json", relative to: app/
Would you guys be so kind and provide some examples for Android on to which folder the paths res://
, ~/
point in the code base?
I am using Angular flavor.
Best
@erodriguezh iirc you can drop the into let say App_Resources/Android/src/debug
by doing this it will only be used for your debug apps.
Hi @triniwiz,
Thanks for the tip. The reason that I was asking for example on how to load a file on Android using res://
or ~/
is to have both Android and iOS implementations consistent.
While having App_Resources/Android/src/debug
for debug and App_Resources/Android/src/prod
for production on Android will work out of the box, This approach did not work on iOS. On iOS I will have to point to the file specifically using initializeAppWithPath()
.
The code would roughly look like this.
let initializeFirebase;
if (isAndroid) {
initializeFirebase = firebase().initializeApp();
}
if (isIOS) {
initializeFirebase = firebase().initializeAppWithPath('res://firebase/dev/GoogleService-Info.plist');
}
initializeFirebase.then(
(firebaseApp: FirebaseApp) => { console.log('firebase.init done'); },
error => { console.log(`firebase.init error: ${error}`); }
);
I believe this is a bug on my end with reading the file from app resources
Try dropping the config into App_Resources/Android/src/main/res/raw
I added the config file to the following path App_Resources/Android/src/main/res/raw/debug/google-services.json
I tried with the following paths
firebase().initializeAppWithPath('res://debug/google-services.json');
firebase().initializeAppWithPath('res://raw/debug/google-services.json');
Both returned this error:
firebase.init error: Error: java.lang.NoSuchFieldException: No field raw/debug/google-services.json in class Lcom/nativescript/R$raw; (declaration of 'com.nativescript.R$raw' appears in /data/app/~~ahUHybxEYkwOp7GGWOAGug==/com.nativescript-8dUx9vz5oYzaAcDegJH4mQ==/base.apk!classes2.dex)
Then I tried with
firebase().initializeAppWithPath('~/debug/google-services.json);
firebase().initializeAppWithPath('~/raw/debug/google-services.json);
Both returned this error:
firebase.init error: Error: com.tns.NativeScriptException: Failed to find module: "/data/data/com.nativescript/files/app/raw/debug/google-services.json", relative to: app/
Try the following firebase().initializeAppWithPath('res://google-services');
I found out a few things
- There are requirements for the naming convention of the file. E.g: Using dashes is forbidden.
Execution failed for task ':app:mergeDebugResources'.
/nativescript/platforms/android/app/src/main/res/raw/google-services.json: Error: '-' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore
- This almost works
Add the config file to the following path App_Resources/Android/src/main/res/raw/google_services.json
Then call it from the code firebase().initializeAppWithPath('res://google_services')
But it throws the following error
JS: firebase.init error: Error: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
Then I figured out that I had a google_services.json
in the debug
folder, so I removed it, but then I remembered that the first time you compile after running ns clean
, if there is no google_services.json
on the debug
folder it throws the following error.
Execution failed for task ':app:processDebugGoogleServices'.
File google-services.json is missing. The Google Services Plugin cannot function without it.
Searched Location:
/nativescript/platforms/android/app/src/debug/google-services.json
/nativescript/platforms/android/app/src/google-services.json
/nativescript/platforms/android/app/src/Debug/google-services.json
/nativescript/platforms/android/app/google-services.json
We have the chicken or the egg problem :sweat_smile:
Hi there @erodriguezh
Did you manage to find a solution to this?