AndroidX 和 FirebaseFirestore 提供 java.lang.RuntimeException
AndroidX and FirebaseFirestore gives java.lang.RuntimeException
我刚刚开始将项目迁移到使用 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.access0(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.access0(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
Default FirebaseApp is not initialized in this process
com.iainism.joebot
. Make sure to call
FirebaseApp.initializeApp(Context)
first.
classpath 'com.google.gms:google-services:4.0.0' //4.1.0 Crashes app on start
Multidex
也已启用。我没看到。但是,您可以尝试禁用它,然后检查 google 服务(到目前为止的最新版本):
classpath 'com.google.gms:google-services:4.1.0'
然后调用:
FirebaseApp.initializeApp(MainActivity.this)
并在 onCreate()
方法中声明 DocumentReference
。
上面写着:
Default FirebaseApp
is not initialized in this process com.iainism.joebot
.
Make sure to call FirebaseApp.initializeApp(Context)
first.
设置private DocumentReference mDocRef = null;
...为了遵循上面的说明:
FirebaseApp.initializeApp(this.getContext());
this.mDocRef = FirebaseFirestore.getInstance().document("FirstGo/Test");
否则您将在初始化 FirebaseApp
之前访问 FirebaseFirestore
。
即使我在安装新的 Android Studio 3.2 时遇到了这个问题,似乎 Google 已经在准备新版本的 Android SDK 构建工具。在今天早上找到并应用此更新后,使用 com.google.gms:google-services:4.1.0
(没有其他更改)时不再出现运行时错误。
这个故事的寓意:早点放弃,第二天再回来查看工具是否已在一夜之间修补了某些问题并解决了问题!
我刚刚开始将项目迁移到使用 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.access0(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.access0(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
Default FirebaseApp is not initialized in this process
com.iainism.joebot
. Make sure to callFirebaseApp.initializeApp(Context)
first.
classpath 'com.google.gms:google-services:4.0.0' //4.1.0 Crashes app on start
Multidex
也已启用。我没看到。但是,您可以尝试禁用它,然后检查 google 服务(到目前为止的最新版本):
classpath 'com.google.gms:google-services:4.1.0'
然后调用:
FirebaseApp.initializeApp(MainActivity.this)
并在 onCreate()
方法中声明 DocumentReference
。
上面写着:
Default
FirebaseApp
is not initialized in this processcom.iainism.joebot
.Make sure to call
FirebaseApp.initializeApp(Context)
first.
设置private DocumentReference mDocRef = null;
...为了遵循上面的说明:
FirebaseApp.initializeApp(this.getContext());
this.mDocRef = FirebaseFirestore.getInstance().document("FirstGo/Test");
否则您将在初始化 FirebaseApp
之前访问 FirebaseFirestore
。
即使我在安装新的 Android Studio 3.2 时遇到了这个问题,似乎 Google 已经在准备新版本的 Android SDK 构建工具。在今天早上找到并应用此更新后,使用 com.google.gms:google-services:4.1.0
(没有其他更改)时不再出现运行时错误。
这个故事的寓意:早点放弃,第二天再回来查看工具是否已在一夜之间修补了某些问题并解决了问题!