Android Studio Release build 给我:不幸的是,“App Name”已停止

Android Studio Release build gives me: Unfortunately, “App Name” has stopped

我正在开发 Android Studio 1.1.0。 构建变体 debug 完美运行,但是当我尝试 运行 release 构建变体时,应用程序启动但给了我一个 "Unfortunately, “App Name” has stopped."

我过去已经签署了我的应用程序,也许我错过了一个我完全忘记的简单步骤,我错过了什么?

这是我的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.madx.quiz.apf"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    signingConfigs {
        release {
            storeFile file('/Users/madx/Documents/Workspaces/Android/APF/app/keystore.keystore')
            storePassword ‘my_store_password’
            keyAlias ‘my_key_alias’
            keyPassword ‘my_key_password’
        }
        debug {}
    }

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

repositories{
// some repositories
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:support-v13:21.0.0'
    compile 'com.google.code.gson:gson:2.3.1'
}

事件日志给我:

19:52:49 Gradle build finished in 3 sec
19:52:50 Session 'app': running

Logcat:

03-02 19:33:11.851  25913-25913/com.madx.quiz.apf I/art﹕ Late-enabling -Xcheck:jni
03-02 19:33:12.840  25913-25946/com.madx.quiz.apf D/OpenGLRenderer﹕ Render dirty regions requested: true
03-02 19:33:12.849  25913-25913/com.madx.quiz.apf D/Atlas﹕ Validating map...
03-02 19:33:12.922  25913-25946/com.madx.quiz.apf I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
03-02 19:33:12.924  25913-25946/com.madx.quiz.apf I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-02 19:33:12.936  25913-25946/com.madx.quiz.apf D/OpenGLRenderer﹕ Enabling debug mode 0

Gradle 控制台:

Executing tasks: [:app:generateReleaseSources]

Configuration on demand is an incubating feature.
:app:preBuild
:app:preReleaseBuild
:app:checkReleaseManifest
:app:preDebugBuild
:app:prepareComAfollestadMaterialDialogs0612Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72103Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV132100Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42103Library UP-TO-DATE
:app:prepareComGithubDanielemaddalunoAndroidupdatecheckerLibrary102Library UP-TO-DATE
:app:prepareReleaseDependencies
:app:compileReleaseAidl UP-TO-DATE
:app:compileReleaseRenderscript UP-TO-DATE
:app:generateReleaseBuildConfig UP-TO-DATE
:app:generateReleaseAssets UP-TO-DATE
:app:mergeReleaseAssets UP-TO-DATE
:app:generateReleaseResValues UP-TO-DATE
:app:generateReleaseResources UP-TO-DATE
:app:mergeReleaseResources
:app:processReleaseManifest UP-TO-DATE
:app:processReleaseResources
:app:generateReleaseSources

BUILD SUCCESSFUL

Total time: 2.4 secs

尝试在您的发布版本中禁用 ProGuard:

build.gradle :

buildTypes {
    release {
        signingConfig signingConfigs.release
        zipAlignEnabled true
    }
    debug {
    }
}

问题可能是由 ProGuard 引起的,但没有堆栈跟踪,我无法真正解决您的问题。

如果你还想用proguard我建议你去寻找更多关于错误的信息,你可以在release字段中添加以下内容:

debuggable true
jniDebuggable true

这样你就可以更好的定位问题。 然而,问题可能与 gson 库有关,您应该将以下行添加到 proguard-rules.pro 文件(您可以查看更复杂的示例 here):

# Add any classes the interact with gson
-keepclassmembers class com.madx.quiz.apf.apf.APFQuestion { *; }
-keepclassmembers class com.madx.quiz.apf.apf.APFSubCategory { *; }

###---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------