此崩溃仅发生在从 Play 商店下载应用程序时。该活动打开并保持良好状态几秒钟,但就在那之后..正在发生这种情况:
2022-02-12 22:32:50.927 6188-6283/? E/AndroidRuntime: FATAL EXCEPTION: firebase-installations-executor-2
Process: com.Sujal_Industries.Notes.SelfNotes, PID: 6188
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:220)
at l6.c.g(SourceFile:346)
at l6.b.run(SourceFile:93)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
我尝试在调试模式下重现崩溃(通过启用混淆器),但不能,所以我认为它与混淆器优化/排除无关。
这是来自 MainActivity.java 的代码:
package com.Sujal_Industries.Notes.SelfNotes;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.splashscreen.SplashScreen;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract;
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "SelfNotes";
private static final String spFileKey = "SelfNotes.SECRET_FILE";
private SharedPreferences sharedPreferences;
Button button;
private final ActivityResultLauncher<Intent> signInLauncher = registerForActivityResult(
new FirebaseAuthUIActivityResultContract(),
this::onSignInResult
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SplashScreen.installSplashScreen(this);
doInitialSetup();
}
private void doInitialSetup() {
sharedPreferences = getSharedPreferences(spFileKey, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
if (!sharedPreferences.getBoolean("FCM", false)) {
FirebaseMessaging.getInstance().subscribeToTopic("Global")
.addOnCompleteListener(task -> {
String msg = getString(R.string.msg_subscribed);
editor.putBoolean("FCM", true);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
editor.putBoolean("FCM", false);
}
editor.apply();
Log.d(TAG, msg);
});
}
Intent intent = new Intent(getApplicationContext(), App.class);
startActivity(intent);
finish();
}
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(v -> {
// Choose authentication providers
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build());
// Create and launch sign-in intent
Intent signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build();
signInLauncher.launch(signInIntent);
});
}
@Override
protected void onStart() {
super.onStart();
}
private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
SharedPreferences.Editor editor = sharedPreferences.edit();
if (result.getResultCode() == RESULT_OK) {
// Successfully signed in
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
if (!sharedPreferences.getBoolean("FCM", false)) {
FirebaseMessaging.getInstance().subscribeToTopic("Global")
.addOnCompleteListener(task -> {
String msg = getString(R.string.msg_subscribed);
editor.putBoolean("FCM", true);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
editor.putBoolean("FCM", false);
}
editor.apply();
Log.d(TAG, msg);
});
}
// Accessing Cloud FireStore...
FirebaseFirestore db = FirebaseFirestore.getInstance();
//Creating New User...
String name = user.getDisplayName();
String email = user.getEmail();
if (email != null && name != null) {
Map<String, Object> newUser = new HashMap<>();
newUser.put("Name", name);
db.collection("users")
.document(email)
.set(newUser);
}
Intent intent = new Intent(getApplicationContext(), App.class);
startActivity(intent);
finish();
} else {
// Sign in failed. If response is null the user canceled the
// sign-in flow using the back button. Otherwise check
// response.getError().getErrorCode() and handle the error.
// ...
Log.e(TAG, "Failure!");
}
}
}
}
build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 32
defaultConfig {
applicationId "com.Sujal_Industries.Notes.SelfNotes"
minSdkVersion 21
targetSdkVersion 32
versionCode 18
versionName "1.4.7"
// Enabling multidex support.
multiDexEnabled true
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
buildToolsVersion '32.1.0-rc1'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.firebase:firebase-analytics:20.1.0'
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.firebaseui:firebase-ui-auth:8.0.0'
implementation 'com.google.android.material:material:1.6.0-alpha02'
implementation 'com.google.firebase:firebase-messaging:23.0.0'
implementation 'com.google.firebase:firebase-firestore:24.0.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'com.github.satyan:sugar:1.5'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.core:core-splashscreen:1.0.0-beta01'
}
apply plugin: 'com.google.gms.google-services'
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Sujal_Industries.Notes.SelfNotes">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/android_alarmmanager"
android:label="@string/app_name"
android:roundIcon="@drawable/android_alarmmanager"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="false" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".Add"
android:exported="false" />
<activity
android:name=".App"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.App.Starting">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/android_alarmmanager" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="FirebaseChannel" />
<meta-data
android:name="DATABASE"
android:value="notes.db" />
<meta-data
android:name="VERSION"
android:value="3" />
<meta-data
android:name="QUERY_LOG"
android:value="true" />
<meta-data
android:name="DOMAIN_PACKAGE_NAME"
android:value="com.Sujal_Industries.Notes.SelfNotes" />
<meta-data
android:name="google_analytics_adid_collection_enabled"
android:value="false" />
</application>
</manifest>
完整代码在这里:https://github.com/Sujal1245/SelfNotes
应用程序链接:https://play.google.com/store/apps/details?id=com.Sujal_Industries.Notes.SelfNotes
我搜索过类似的错误,但找不到像我的情况一样的错误。 logcat 也让我有点困惑。
更新:
我删除了Firebase Analytics库,现在没有崩溃,但我仍然想知道崩溃背后的确切原因。
implementation 'com.google.firebase:firebase-analytics:20.1.0'