我刚刚开始迁移项目以使用Firebase和AndroidX,据我所知,我不得不做一些我不应该做的事情:我不得不降级Google服务的类路径。
我犯了什么错误,这意味着我不能使用最新版本(4.1.0)?我有一个正在运行的应用程序,所以我不会太沮丧,我只想知道我做错了什么!
我在最新的4.1.0版本中遇到的错误:
09-25 20:54:38.152 5311-5311/com.iainism.joebot E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.iainism.joebot, PID: 5311
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.iainism.joebot/com.iainism.joebot.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.iainism.joebot. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.iainism.joebot. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.0.5:49)
at com.iainism.joebot.MainActivity.<init>(MainActivity.java:32)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
-项目buidle.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha11'
classpath 'com.google.gms:google-services:4.0.0' //4.1.0 Crashes app on start
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
-应用程序build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.iainism.joebot"
minSdkVersion 19
targetSdkVersion 28
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-firestore:17.1.0'
implementation 'androidx.multidex:multidex:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
apply plugin: 'com.google.gms.google-services'
-MainActivity.java:
package com.iainism.joebot;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.multidex.MultiDex;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
// TODO: Add authentication
// TODO: Change security rules
// TODO: Rework content provider to do all CRUD via Firestore
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("FirstGo/Test");
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set-up Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Set-up FAB
FloatingActionButton floatingActionButton = findViewById(R.id.floating_action_button);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(LOG_TAG, "FAB clicked!");
// TODO: add proper action
}
});
// Set up Firebase Firestore
Map<String , Object> cloudData = new HashMap<>();
cloudData.put("data", "This is a test!");
mDocRef.set(cloudData).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()) {
Log.d(LOG_TAG, "Created Firestore document");
} else {
Log.w(LOG_TAG, "Failed to create Firestore document", task.getException());
}
}
});
Log.d(LOG_TAG, "Reached end of onCreate");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings: {
// TODO: Rebuild settings menu...
Toast toast = Toast.makeText(this, "You selected Settings", Toast.LENGTH_LONG);
toast.show();
Log.d(LOG_TAG, "Settings menu selected");
break;
}
// case blocks for other MenuItems (if any)
}
return super.onOptionsItemSelected(item);
}
}
-在gradle.properties中:
android.useAndroidX = true
android.enableJetifier = true
答案 0 :(得分:0)
默认FirebaseApp在此过程中未初始化
console
。确保致电 首先com.iainism.joebot
。
FirebaseApp.initializeApp(Context)
classpath 'com.google.gms:google-services:4.0.0' //4.1.0 Crashes app on start
也已启用。我没看到但是,您可以尝试禁用它,然后使用google服务(最新版本)进行检查:
Multidex
然后致电:
classpath 'com.google.gms:google-services:4.1.0'
然后在FirebaseApp.initializeApp(MainActivity.this)
方法内声明DocumentReference
。
答案 1 :(得分:0)
它显示为:
默认
FirebaseApp
在此过程com.iainism.joebot
中未初始化。请务必先致电
FirebaseApp.initializeApp(Context)
。
设置private DocumentReference mDocRef = null;
...以遵循上述说明:
FirebaseApp.initializeApp(this.getContext());
this.mDocRef = FirebaseFirestore.getInstance().document("FirstGo/Test");
否则,您将在初始化FirebaseFirestore
之前访问FirebaseApp
。
答案 2 :(得分:0)
即使我通过全新更新的Android Studio 3.2安装遇到此问题,似乎Google也在准备新版本的Android SDK Build-Tools。找到并在今天早上应用此更新后,使用com.google.gms:google-services:4.1.0
时不再存在运行时错误(没有其他更改)。
故事的寓意:尽快放弃并在第二天回来,看看在解决问题的工具中是否有通宵修补的东西!