Firebase 正在引发数据库泄漏
Firebase is firing a database leak
我在我的应用程序中添加了 firebase 以获取一些特殊事件,例如用户打开和关闭他们的应用程序时。
问题是每次我启动应用程序时,它都会立即崩溃。它在我添加 firebase 时开始发生,当我评论我的 Firebase 代码时,它又开始工作了,所以现在我很确定这是 firebase 的问题。
问题似乎是在调用 onForeground()
时发生的。我试图将它放在新的 Thread
上或在 Handler
中调用它,但它一直崩溃...
这是堆栈:
10-28 16:46:26.106 27724-27732/com.package.custom E/System: Uncaught exception thrown by finalizer
10-28 16:46:26.107 27724-27732/com.package.custom E/System: java.lang.IllegalStateException: The database '/data/user/0/com.package.custom/databases/google_app_measurement_local.db' is not open.
at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2169)
at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:365)
at android.database.sqlite.SQLiteDatabase.initialValue(SQLiteDatabase.java:84)
at android.database.sqlite.SQLiteDatabase.initialValue(SQLiteDatabase.java:83)
at java.lang.ThreadLocal$Values.getAfterMiss(ThreadLocal.java:430)
at java.lang.ThreadLocal.get(ThreadLocal.java:65)
at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:359)
at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:101)
at android.database.sqlite.SQLiteQuery.setLastStmt(SQLiteQuery.java:96)
at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:111)
at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:300)
at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:366)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
at java.lang.Thread.run(Thread.java:818)
这是我的 AnalyticsHelper class 的一部分:
public class AnalyticsHelper
{
//...
/**
* Hook called when the app switch to foreground mode
*/
public static void onForeground()
{
Log.d(TAG, "FOREGROUND");
Bundle bundle = new Bundle();
bundle.putString(KEY_TYPE, AnalyticsCategories.CATEGORY_LIFECYCLE_ACTION_FOREGROUND);
CustomApplication.getAnalytics().logEvent(AnalyticsCategories.CATEGORY_LIFECYCLE, bundle);
}
//...
}
这里是activity中AnalyticsHelper的调用:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onResume()
{
super.onResume();
//...
AnalyticsHelper.onForeground();
}
}
这是自定义应用程序的一部分 class:
public class CustomApplication extends Application
{
private static final String TAG = "CustomApplication";
private static CustomApplication sApplication;
public static CustomApplication get()
{
return sApplication;
}
public static FirebaseAnalytics getAnalytics()
{
return get().getFirebaseAnalytics();
}
@Override
public void onCreate()
{
super.onCreate();
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
sApplication = this;
}
/**
* Gets the default {@link Tracker} for this {@link Application}.
*
* @return tracker
*/
public FirebaseAnalytics getFirebaseAnalytics()
{
if (mFirebaseAnalytics == null)
{
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
}
return mFirebaseAnalytics;
}
}
找了一个下午都没有结果,求助..
编辑 #1 (build.gradle):
apply plugin: 'com.android.application'
android {
signingConfigs {
release {
keyAlias '##########'
keyPassword '##########'
storeFile file('##########')
storePassword '##########'
}
}
compileSdkVersion 25
buildToolsVersion '24.0.2'
defaultConfig {
applicationId "com.custom.app"
minSdkVersion 18
targetSdkVersion 25
android.applicationVariants.all { variant ->
def versionPropsFile = file('src/main/res/raw/version.properties')
if (versionPropsFile.canRead())
{
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
def code = versionProps['code'].toInteger() + 1
versionProps['code'] = code.toString()
def version = versionProps['version']
versionProps.store(versionPropsFile.newWriter(), null)
versionCode code
versionName version
}
else
{
throw new GradleScriptException('Cant access version.properties');
}
}
signingConfig signingConfigs.release
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
debuggable true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "boolean", "EXPORT_EXTERNAL_STORAGE", "true"
}
debug {
debuggable true
buildConfigField "boolean", "EXPORT_EXTERNAL_STORAGE", "true"
}
}
productFlavors {
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/commons-net-3.3.jar')
//Debug
compile files('libs/commons-lang3-3.5.jar')
compile files('libs/joda-time-2.9.5.jar')
compile project(':android-ble-module')
debugCompile project(path: ':android-log-module', configuration: 'debug')
releaseCompile project(path: ':android-log-module', configuration: 'release')
compile project(':android-location-module')
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:preference-v14:25.0.0'
compile 'com.google.android.gms:play-services-gcm:9.8.0'
compile 'com.google.android.gms:play-services-maps:9.8.0'
compile 'com.google.android.gms:play-services-identity:9.8.0'
compile 'com.google.firebase:firebase-analytics:9.8.0'
}
apply plugin: 'com.google.gms.google-services'
请移至最新版本的 firebase SDK。 Firebase 在他们更新的 SDK 中解决了这个内存泄漏问题。在这种情况下替换此行
compile 'com.google.firebase:firebase-analytics:9.8.0'
下面一行。
compile 'com.google.firebase:firebase-analytics:11.0.1'
我在我的应用程序中添加了 firebase 以获取一些特殊事件,例如用户打开和关闭他们的应用程序时。
问题是每次我启动应用程序时,它都会立即崩溃。它在我添加 firebase 时开始发生,当我评论我的 Firebase 代码时,它又开始工作了,所以现在我很确定这是 firebase 的问题。
问题似乎是在调用 onForeground()
时发生的。我试图将它放在新的 Thread
上或在 Handler
中调用它,但它一直崩溃...
这是堆栈:
10-28 16:46:26.106 27724-27732/com.package.custom E/System: Uncaught exception thrown by finalizer
10-28 16:46:26.107 27724-27732/com.package.custom E/System: java.lang.IllegalStateException: The database '/data/user/0/com.package.custom/databases/google_app_measurement_local.db' is not open.
at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2169)
at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:365)
at android.database.sqlite.SQLiteDatabase.initialValue(SQLiteDatabase.java:84)
at android.database.sqlite.SQLiteDatabase.initialValue(SQLiteDatabase.java:83)
at java.lang.ThreadLocal$Values.getAfterMiss(ThreadLocal.java:430)
at java.lang.ThreadLocal.get(ThreadLocal.java:65)
at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:359)
at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:101)
at android.database.sqlite.SQLiteQuery.setLastStmt(SQLiteQuery.java:96)
at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:111)
at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:300)
at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:366)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
at java.lang.Thread.run(Thread.java:818)
这是我的 AnalyticsHelper class 的一部分:
public class AnalyticsHelper
{
//...
/**
* Hook called when the app switch to foreground mode
*/
public static void onForeground()
{
Log.d(TAG, "FOREGROUND");
Bundle bundle = new Bundle();
bundle.putString(KEY_TYPE, AnalyticsCategories.CATEGORY_LIFECYCLE_ACTION_FOREGROUND);
CustomApplication.getAnalytics().logEvent(AnalyticsCategories.CATEGORY_LIFECYCLE, bundle);
}
//...
}
这里是activity中AnalyticsHelper的调用:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onResume()
{
super.onResume();
//...
AnalyticsHelper.onForeground();
}
}
这是自定义应用程序的一部分 class:
public class CustomApplication extends Application
{
private static final String TAG = "CustomApplication";
private static CustomApplication sApplication;
public static CustomApplication get()
{
return sApplication;
}
public static FirebaseAnalytics getAnalytics()
{
return get().getFirebaseAnalytics();
}
@Override
public void onCreate()
{
super.onCreate();
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
sApplication = this;
}
/**
* Gets the default {@link Tracker} for this {@link Application}.
*
* @return tracker
*/
public FirebaseAnalytics getFirebaseAnalytics()
{
if (mFirebaseAnalytics == null)
{
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
}
return mFirebaseAnalytics;
}
}
找了一个下午都没有结果,求助..
编辑 #1 (build.gradle):
apply plugin: 'com.android.application'
android {
signingConfigs {
release {
keyAlias '##########'
keyPassword '##########'
storeFile file('##########')
storePassword '##########'
}
}
compileSdkVersion 25
buildToolsVersion '24.0.2'
defaultConfig {
applicationId "com.custom.app"
minSdkVersion 18
targetSdkVersion 25
android.applicationVariants.all { variant ->
def versionPropsFile = file('src/main/res/raw/version.properties')
if (versionPropsFile.canRead())
{
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
def code = versionProps['code'].toInteger() + 1
versionProps['code'] = code.toString()
def version = versionProps['version']
versionProps.store(versionPropsFile.newWriter(), null)
versionCode code
versionName version
}
else
{
throw new GradleScriptException('Cant access version.properties');
}
}
signingConfig signingConfigs.release
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
debuggable true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "boolean", "EXPORT_EXTERNAL_STORAGE", "true"
}
debug {
debuggable true
buildConfigField "boolean", "EXPORT_EXTERNAL_STORAGE", "true"
}
}
productFlavors {
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/commons-net-3.3.jar')
//Debug
compile files('libs/commons-lang3-3.5.jar')
compile files('libs/joda-time-2.9.5.jar')
compile project(':android-ble-module')
debugCompile project(path: ':android-log-module', configuration: 'debug')
releaseCompile project(path: ':android-log-module', configuration: 'release')
compile project(':android-location-module')
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:preference-v14:25.0.0'
compile 'com.google.android.gms:play-services-gcm:9.8.0'
compile 'com.google.android.gms:play-services-maps:9.8.0'
compile 'com.google.android.gms:play-services-identity:9.8.0'
compile 'com.google.firebase:firebase-analytics:9.8.0'
}
apply plugin: 'com.google.gms.google-services'
请移至最新版本的 firebase SDK。 Firebase 在他们更新的 SDK 中解决了这个内存泄漏问题。在这种情况下替换此行
compile 'com.google.firebase:firebase-analytics:9.8.0'
下面一行。
compile 'com.google.firebase:firebase-analytics:11.0.1'