【问题标题】:Application's onCreate not being called for production builds应用程序的 onCreate 不被用于生产构建
【发布时间】:2017-10-31 13:00:08
【问题描述】:

自从我升级到 gradle 3.0 后,我们的主要 Application 的 onCreate 就没有从生产级应用程序中触发。然而,在为我们的 staging 风格构建时,应用程序的 onCreate 调用得很好。

由于应用程序的 onCreate 从未被调用,我们的 Realm 从未被初始化,并且应用程序在稍后尝试访问 Realm 时崩溃。

AndroidManifest.xml:

 <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
....

App.kt:

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        Realm.init(this)
        println("This is never called")
    }
}

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.triplet.play'

....

android {

    flavorDimensions "testing"

    productFlavors {
        production {
            dimension "testing"
            applicationId "com.imerso.app"
        }
        staging {
            dimension "testing"
            applicationId "com.imerso.app.staging"
            externalNativeBuild {
              cmake {
                targets 'cpp_scan_app', 'unittests'
              }
            }
        }
    }

    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    dexOptions {
      // Sets the maximum number of DEX processes
      // that can be started concurrently.
      maxProcessCount 8
      // Sets the maximum memory allocation pool size
      // for the dex operation.
      javaMaxHeapSize "2g"

....

不确定我们对 gradle 3.0 的更改是否是这里的罪魁祸首,但在我进行升级之前一切正常。

【问题讨论】:

  • 您是否尝试禁用 Instant Run ?
  • 是的,即时运行已禁用。
  • 您运行的是哪个版本的 Android Studio?

标签: android kotlin android-gradle-3.0


【解决方案1】:

未调用 Application onCreate 的原因是由于在备份过程中未记录的行为。您可以找到更多信息Why backup related process might cause Application's onCreate is not executed?

我创建了一个问题 https://issuetracker.google.com/issues/138423608 。如果您希望以更高的优先级解决此问题,请为该问题加注星标。

【讨论】:

    【解决方案2】:

    我必须重新启动计算机才能解决此问题。我试图禁用即时运行、清理、重建。重新启动 Android Studio。在应用程序名称中使用了完整路径。完全重启我的 Macbook 后终于可以工作了。

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 2020-05-07
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多