Repository: thundernest/k-9
Issue: Crash with v5.711

### Actual behavior

K9 crashed on startup.

### Steps to reproduce

Start K9

### Environment

K-9 Mail version: v5.711

All previous pre-release versions before 5.711 were running fine.

Android version: 7.1.2 (LOS 14.1)

### logcat

```
05-08 13:49:24.913  3133 28478 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.fsck.k9/.activity.MessageList bnds=[540,1108][756,1349] (has extras)} from uid 10028 on display 0
05-08 13:49:24.926  3133 28478 E QCOM PowerHAL: Failed to acquire lock.
05-08 13:49:24.966  3133  4244 I ActivityManager: Start proc 24491:com.fsck.k9/u0a77 for activity com.fsck.k9/.activity.MessageList
05-08 13:49:25.037 24491 24491 W System  : ClassLoader referenced unknown path: /data/app/com.fsck.k9-2/lib/arm
05-08 13:49:25.435 24491 24491 D AndroidRuntime: Shutting down VM
--------- beginning of crash
05-08 13:49:25.437 24491 24491 E AndroidRuntime: FATAL EXCEPTION: main
05-08 13:49:25.437 24491 24491 E AndroidRuntime: Process: com.fsck.k9, PID: 24491
05-08 13:49:25.437 24491 24491 E AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Duration;
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.job.MailSyncWorkerManager.<clinit>(MailSyncWorkerManager.kt:85)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.job.KoinModuleKt$jobModule$1$5.invoke(KoinModule.kt:12)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.job.KoinModuleKt$jobModule$1$5.invoke(KoinModule.kt)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:36)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:87)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:214)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.scope.Scope.get(Scope.kt:181)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.job.KoinModuleKt$jobModule$1$4.invoke(KoinModule.kt:26)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.job.KoinModuleKt$jobModule$1$4.invoke(KoinModule.kt)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.instance.SingleInstanceFactory.create(SingleInstanceFactory.kt:40)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.instance.SingleInstanceFactory.get(SingleInstanceFactory.kt:48)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:87)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:214)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at org.koin.core.scope.Scope.get(Scope.kt:181)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.Core$$special$$inlined$inject$3.invoke(DI.kt:43)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.Core.getJobManager(Core.kt)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.Core.setServicesEnabled(Core.kt:78)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.Core.setServicesEnabled(Core.kt:52)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.Core.init(Core.kt:37)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.fsck.k9.App.onCreate(App.kt:23)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5448)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:360)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(<Xposed>)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.app.ActivityThread.-wrap2(ActivityThread.java)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1564)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6186)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)
05-08 13:49:25.437 24491 24491 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.Duration" on path: DexPathList[[zip file "/data/app/com.fsck.k9-2/base.apk"],nativeLibraryDirectories=[/data/app/com.fsck.k9-2/lib/arm, /system/lib, /vendor/lib]]
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
05-08 13:49:25.437 24491 24491 E AndroidRuntime:        ... 37 more
05-08 13:49:25.442  3133 25207 W ActivityManager:   Force finishing activity com.fsck.k9/.activity.MessageList
```




Changed files:
app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt

diff --git a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt
index 3db9b9d66..989a42ee0 100644
--- a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt
+++ b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt
@@ -10,7 +10,7 @@ import androidx.work.workDataOf
 import com.fsck.k9.Account
 import com.fsck.k9.Clock
 import com.fsck.k9.K9
-import java.time.Duration
+import java.util.concurrent.TimeUnit
 import timber.log.Timber
 
 class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clock) {
@@ -24,9 +24,9 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo
     fun scheduleMailSync(account: Account) {
         if (isNeverSyncInBackground()) return
 
-        getSyncIntervalIfEnabled(account)?.let { syncInterval ->
+        getSyncIntervalIfEnabled(account)?.let { syncIntervalMinutes ->
             Timber.v("Scheduling mail sync worker for %s", account.description)
-            Timber.v("  sync interval: %d minutes", syncInterval.toMinutes())
+            Timber.v("  sync interval: %d minutes", syncIntervalMinutes)
 
             val constraints = Constraints.Builder()
                 .setRequiredNetworkType(NetworkType.CONNECTED)
@@ -36,14 +36,14 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo
             val lastSyncTime = account.lastSyncTime
             Timber.v("  last sync time: %tc", lastSyncTime)
 
-            val initialDelay = calculateInitialDelay(lastSyncTime, syncInterval)
-            Timber.v("  initial delay: %d minutes", initialDelay.toMinutes())
+            val initialDelay = calculateInitialDelay(lastSyncTime, syncIntervalMinutes)
+            Timber.v("  initial delay: %d ms", initialDelay)
 
             val data = workDataOf(MailSyncWorker.EXTRA_ACCOUNT_UUID to account.uuid)
 
-            val mailSyncRequest = PeriodicWorkRequestBuilder<MailSyncWorker>(syncInterval)
-                .setInitialDelay(initialDelay)
-                .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, INITIAL_BACKOFF_DELAY)
+            val mailSyncRequest = PeriodicWorkRequestBuilder<MailSyncWorker>(syncIntervalMinutes, TimeUnit.MINUTES)
+                .setInitialDelay(initialDelay, TimeUnit.MILLISECONDS)
+                .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, INITIAL_BACKOFF_DELAY_MINUTES, TimeUnit.MINUTES)
                 .setConstraints(constraints)
                 .setInputData(data)
                 .addTag(MAIL_SYNC_TAG)
@@ -56,23 +56,23 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo
 
     private fun isNeverSyncInBackground() = K9.backgroundOps == K9.BACKGROUND_OPS.NEVER
 
-    private fun getSyncIntervalIfEnabled(account: Account): Duration? {
+    private fun getSyncIntervalIfEnabled(account: Account): Long? {
         val intervalMinutes = account.automaticCheckIntervalMinutes
         if (intervalMinutes <= Account.INTERVAL_MINUTES_NEVER) {
             return null
         }
 
-        return Duration.ofMinutes(intervalMinutes.toLong())
+        return intervalMinutes.toLong()
     }
 
-    private fun calculateInitialDelay(lastSyncTime: Long, syncInterval: Duration): Duration {
+    private fun calculateInitialDelay(lastSyncTime: Long, syncIntervalMinutes: Long): Long {
         val now = clock.time
-        val nextSyncTime = lastSyncTime + syncInterval.toMillis()
+        val nextSyncTime = lastSyncTime + (syncIntervalMinutes * 60L * 1000L)
 
         return if (lastSyncTime > now || nextSyncTime <= now) {
-            Duration.ZERO
+            0L
         } else {
-            Duration.ofMillis(nextSyncTime - now)
+            nextSyncTime - now
         }
     }
 
@@ -82,6 +82,6 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo
 
     companion object {
         const val MAIL_SYNC_TAG = "MailSync"
-        private val INITIAL_BACKOFF_DELAY = Duration.ofMinutes(5)
+        private const val INITIAL_BACKOFF_DELAY_MINUTES = 5L
     }
 }