我的 Android 应用程序仅在不可调试时崩溃?

My Android app only crashes when not debuggable?

我有一个 Android 应用程序,它在调试模式下运行良好,但在发布模式下开始出现问题。它在将项目绑定到 recyclerview 时崩溃。

最初我认为这是一个proguard 问题,但在为调试模式启用proguard 后,很明显只有发布版本有这个问题。只是为了尝试一下,我将发布版本设置为 debuggable true 并且问题消失了。

有没有人知道只有当 apk 不可调试时才会导致崩溃?或者有什么方法可以调试它并查看到底出了什么问题?

这些是我的构建类型:

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

因此,如果我将 debuggable true 添加到 release,则发行版有效。如果我将 debuggable false 添加到 debug,调试版本将停止工作...

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: __, PID: _
a.a
    at ___.overview.dashboard.m.a(Unknown Source)
    at ___.overview.dashboard.c.a(Unknown Source)
    at android.support.v7.widget.RecyclerView$a.a(Unknown Source)
    at android.support.v7.widget.RecyclerView$a.b(Unknown Source)
    at android.support.v7.widget.RecyclerView$p.a(Unknown Source)
    at android.support.v7.widget.RecyclerView$p.a(Unknown Source)
    at android.support.v7.widget.RecyclerView$p.a(Unknown Source)
    at android.support.v7.widget.RecyclerView$p.c(Unknown Source)
    at android.support.v7.widget.LinearLayoutManager$c.a(Unknown Source)
    at android.support.v7.widget.LinearLayoutManager.a(Unknown Source)
    at android.support.v7.widget.LinearLayoutManager.a(Unknown Source)
    at android.support.v7.widget.LinearLayoutManager.c(Unknown Source)
    at android.support.v7.widget.RecyclerView.N(Unknown Source)
    at android.support.v7.widget.RecyclerView.q(Unknown Source)
    at android.support.v7.widget.RecyclerView.onLayout(Unknown Source)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.support.constraint.ConstraintLayout.onLayout(Unknown Source)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.support.constraint.ConstraintLayout.onLayout(Unknown Source)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:887)
    at android.view.View.layout(View.java:18798)
    at android.view.ViewGroup.layout(ViewGroup.java:5951)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2648)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2364)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1515)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7091)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
    at android.view.Choreographer.doCallbacks(Choreographer.java:702)
    at android.view.Choreographer.doFrame(Choreographer.java:638)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)

正如@laalto 所说,有一个堆栈跟踪可以提供帮助

将其放入我的 proguard 文件中:

-keepattributes SourceFile,LineNumberTable

帮我找到了错误所在。这会向您显示堆栈跟踪中的原始类名和行号。我仍然不确定为什么在调试模式下不会发生错误,但至少我能够解决它。再次感谢!

对我来说,问题是我正在测试发布版本并禁用了 Crashlytics,因为它无法跟踪我的 activity。 当 "debuggable" 设置为 true 时工作正常,但一旦设置为 false 我在应用程序启动时收到即时崩溃。 所以基本上我忘了打开 Crashlytics 并且从中得到了一些内部错误。

如果您正在使用 C 重载函数,请不要!

I.E Class一个

ofAndroidInit(){ 
  // will only work with debugging = false
}

Class乙 main.cpp

ofAndroidInit() {
 // will work with debugging = true
}