Android应用需要构建两次以避免崩溃

时间:2017-09-11 23:29:27

标签: android android-gradle

我面临一个我根本不懂的问题。我有一个Android应用程序,在输入设置时会崩溃(下面的崩溃日志),但只是第一次构建时。这个问题是在调查为什么F-Droid构建出现故障时发现的,而我自己的工作正常。

重现的步骤如下:

  1. rm -r ~/.gradle
  2. ./ gradlew assembleRelease&& adb install -r ...
  3. 这里的构建成功完成,但APK不起作用。

    1. ./ gradlew assembleRelease&& adb install -r ...
    2. 第二次构建也完成,只有这次崩溃不再存在。有谁知道为什么会出现这样的问题?

      可以使用位于here的项目重现该问题。只需输入设置即可触发崩溃。这是完整的堆栈跟踪:

      android.view.InflateException: Binary XML file line #19: Error inflating class PreferenceScreen
      at android.support.v7.preference.PreferenceInflater.createItem(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.onCreateItem(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.createItemFromTag(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.inflate(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.inflate(Unknown Source)
      at android.support.v7.preference.PreferenceManager.inflateFromResource(Unknown Source)
      at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(Unknown Source)
      at fr.kwiatkowski.apktrack.ui.SettingsFragment.onCreatePreferences(Unknown Source)
      at android.support.v7.preference.PreferenceFragmentCompat.onCreate(Unknown Source)
      at fr.kwiatkowski.apktrack.ui.SettingsFragment.onCreate(Unknown Source)
      at android.support.v4.app.Fragment.performCreate(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
      at android.support.v4.app.BackStackRecord.executeOps(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.executeOps(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:725)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5227)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
      at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
      at java.lang.Class.getConstructorOrMethod(Class.java:460)
      at java.lang.Class.getConstructor(Class.java:431)
      ... 29 more
      java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
      at java.lang.Class.getConstructorOrMethod(Class.java:460)
      at java.lang.Class.getConstructor(Class.java:431)
      at android.support.v7.preference.PreferenceInflater.createItem(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.onCreateItem(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.createItemFromTag(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.inflate(Unknown Source)
      at android.support.v7.preference.PreferenceInflater.inflate(Unknown Source)
      at android.support.v7.preference.PreferenceManager.inflateFromResource(Unknown Source)
      at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(Unknown Source)
      at fr.kwiatkowski.apktrack.ui.SettingsFragment.onCreatePreferences(Unknown Source)
      at android.support.v7.preference.PreferenceFragmentCompat.onCreate(Unknown Source)
      at fr.kwiatkowski.apktrack.ui.SettingsFragment.onCreate(Unknown Source)
      at android.support.v4.app.Fragment.performCreate(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
      at android.support.v4.app.BackStackRecord.executeOps(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.executeOps(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
      at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:725)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5227)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
      at dalvik.system.NativeStart.main(Native Method)
      

      为了完整起见,这是我的Gradle信息:

      ------------------------------------------------------------
      Gradle 3.3
      ------------------------------------------------------------
      
      Build time:   2017-01-03 15:31:04 UTC
      Revision:     075893a3d0798c0c1f322899b41ceca82e4e134b
      
      Groovy:       2.4.7
      Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
      JVM:          1.8.0_131 (Oracle Corporation 25.131-b11)
      OS:           Linux 3.16.0-4-amd64 amd64
      

      我为提出如此广泛的问题而道歉。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

正如@CommonsWare建议的那样,通过禁用ProGuard发布版本解决了该问题。这证实了它是问题的根源,现在工作将开始确定某个地方是否缺少-keep选项。

关于两个后续版本不会生成相同APK的原因,我担心我永远不会知道。