Sander Jochems
Sander Jochems

Reputation: 142

How to use FirebaseApp.initializeApp for FirebaseRemoteConfig

I am trying to use FirebaseApp.initializeApp, but its not working. I have the right library, but I cannot use the JSON file to initialize the app

This is my code:

Imports

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;

Variables

private FirebaseRemoteConfig mFirebaseRemoteConfig;

private String projectID;
private String apiKey;
private String appID;

How I initialize the app

public void Initialize() {
    FirebaseOptions mFirebaseOptions = new FirebaseOptions.Builder()
        .setApplicationId(this.appID)
        .setApiKey(this.apiKey)
        .build();
    FirebaseApp.initializeApp(this.context, mFirebaseOptions, this.projectID);
    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
}

The app crashed on startup this is the logcat error:

12-04 22:11:42.217 12566 12566 E AndroidRuntime: FATAL EXCEPTION: main
12-04 22:11:42.217 12566 12566 E AndroidRuntime: Process: com.makeroid.sanderjochems.FirebaseRemoteConfig, PID: 12566
12-04 22:11:42.217 12566 12566 E AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/internal/zzby;
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.google.firebase.FirebaseOptions.fromResource(Unknown Source)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.google.appinventor.components.runtime.FirebaseRemoteConfiguration.<init>(FirebaseRemoteConfiguration.java:67)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at java.lang.reflect.Constructor.newInstance0(Native Method)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at gnu.expr.PrimProcedure.apply(PrimProcedure.java:272)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at gnu.mapping.CallContext.runUntilDone(CallContext.java:234)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at gnu.mapping.CallContext.runUntilValue(CallContext.java:298)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at gnu.kawa.reflect.Invoke.applyN(Invoke.java:209)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at gnu.mapping.ProcedureN.apply2(ProcedureN.java:39)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.makeroid.sanderjochems.FirebaseRemoteConfig.Screen1.$define(Screen1.yail:10182)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.google.appinventor.components.runtime.Form.onCreateFinish(Form.java:389)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.google.appinventor.components.runtime.Form.onCreate(Form.java:336)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:6942)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.ActivityThread.-wrap14(ActivityThread.java)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6682)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
12-04 22:11:42.217 12566 12566 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.internal.zzby" on path: DexPathList[[zip file "/data/app/com.makeroid.sanderjochems.FirebaseRemoteConfig-1/base.apk"],nativeLibraryDirectories=[/data/app/com.makeroid.sanderjochems.FirebaseRemoteConfig-1/lib/arm64, /system/lib64, /vendor/lib64]]
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
12-04 22:11:42.217 12566 12566 E AndroidRuntime:        ... 25 more

Upvotes: 4

Views: 2248

Answers (3)

HarshitG
HarshitG

Reputation: 2827

Hey I am suggesting some solutions -

  1. Build.gradle file

    multiDexEnabled true
    
  2. Add this in your proguard-rules.pro file

    -keep public class com.google.android.gms.* { public *; }

Upvotes: 0

Bob Snyder
Bob Snyder

Reputation: 38289

There may be multiple problems. I'll point out what appears to be one.

The documentation for RemoteConfig.getInstance() notes that it expects the default FirebaseApp to be initialized:

FirebaseRemoteConfig uses the default FirebaseApp...

Unlike some of the other Firebase APIs, there is no way to initialize RemoteConfig using a different FirebaseApp.

That means that when you initialze FirebaseApp, projectID must be "[DEFAULT]", which is available as constant FirebaseApp.DEFAULT_APP_NAME.

FirebaseApp.initializeApp(this.context, mFirebaseOptions, this.projectID);

Try setting projectID to "[DEFAULT]" (or the available constant) to see if the failure changes or is resolved.

Upvotes: 1

Durga M
Durga M

Reputation: 544

Disable the instant-run while generating application and make sure you have added google-services.json file to your project.

Upvotes: 1

Related Questions