自从我升级到gradle 3.0以来,我们主要的Application的onCreate就不会从生产风味的应用中触发。但是,当构建我们的暂存样式时,应用程序的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的更改是否是这里的罪魁祸首,但是在我进行升级之前,一切工作正常。

最佳答案

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

10-06 13:14
查看更多