Firebase正在解决数据库泄漏问题

时间:2016-10-28 15:00:09

标签: android firebase analytics

我在我的应用程序中添加了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$1.initialValue(SQLiteDatabase.java:84)
                                                                  at android.database.sqlite.SQLiteDatabase$1.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类的一部分:

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);
    }

 //...
}

以下是此活动中AnalyticsHelper的调用:

public class MainActivity extends AppCompatActivity
{
 @Override
    protected void onResume()
    {
        super.onResume();

        //...

        AnalyticsHelper.onForeground();
    }

}

这是自定义Application类的一部分:

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'

1 个答案:

答案 0 :(得分:0)

请转到最新版本的firebase SDK。 Firebase在更新的sdk中解决了这个内存泄漏问题。在这种情况下,替换此行

compile 'com.google.firebase:firebase-analytics:9.8.0'

以下线。

compile 'com.google.firebase:firebase-analytics:11.0.1'