简体   繁体   中英

Android App crashes only with release APK

I have a curious issue. I built an app that works fine in debug mode (direkt debugging to my phone and with debug APK installed on my phone) but the app crashes at one point if I use the release build APK. I found the point where the app crashes with release APK, but I don't know why and what I can do:

protected final String TABLE = "done";
protected final String COL_ID = "_id";
protected final String COL_TASK = "taskid";
protected final String COL_DATE = "donedate";

protected String getLastDoneDate(String id) {
    String date = "";

    String filter = COL_TASK + " LIKE ?";
    String[] filterArgs = new String[] {id};
    String sortOrder = COL_DATE + " DESC";
    String[] columns = new String[] {COL_DATE};
    Cursor c = MyTime.db.query(TABLE, columns, filter, filterArgs, null, null, sortOrder, "1");
    if (c.moveToFirst()) {
        date = c.getString(c.getColumnIndex(COL_DATE));
    }
    c.close();

    return date;
}

If I remove the line Cursor c = ... (and all according to c) it works.

Database looks like that:

CREATE TABLE IF NOT EXISTS done (
                _id INTEGER PRIMARY KEY autoincrement,
                taskid INTEGER,
                donedate TEXT DEFAULT '');

Gradle Snippet:

buildTypes {
    release {
        shrinkResources false
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        shrinkResources false
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

I can not debug to find the problem because the crash does'nt happen while debugging.

Can somebody help me to solve this issue?

I added debuggable = true to my gradle file and found the reason of my problem.

buildTypes {
release {
    shrinkResources false
    debuggable true
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
    shrinkResources false
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

Table "done" was not created because I tried to put two table create statements into a single db.execSQL()

I had the same issue and i solved it by making both minifyEnabled and shrinkResources false

 release {
        minifyEnabled false
        shrinkResources false
       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

Except for the SQLite, you can check proguard-rules.pro file, it maybe makes release.apk crash. if you want to lean more, click https://developer.android.com/studio/build/shrink-code.html

The issue comes only if you add the below line in release section of build.gradle file:

 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

Use this instead of above:

 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Sample of gradle file looks like this and it resolved my issue:

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Hope it helps you! Happy coding...

我也在https://medium.com/@aanshul16/android-app-crashes-in-release-mode-but-working-perfectly-in-debug-c051cc83a019 中发现并提到了类似的问题所以你可以先检查一下通过在您的调试风格中启用“minifyEnable”为 true,如果问题在调试模式下重现,那么肯定是因为 proguard。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM