Is it possible to use Mockito with Kotlin without open the class?

As we probably know, by default Kotlin class once defined, it is final, unless it is explicitly declared open. This would post a challenge when we want to Mock it using Mockito. We need to...

Reading console input in Kotlin

I am attempting to accept input from the console in Kotlin but it is difficult because I am not too sure about the syntax. I begin with the main fun main(args: Array<String>) { } WHAT should I...

Converting Python code to Kotlin

I found code to generate n distinct colors. I took the code and made a class out of it import colorsys import itertools from fractions import Fraction class DistinctColorsGenerator: def...

How to run suspend method via reflection?

There is an coroutine block that can runs suspend functions. But I call the function by invoke via reflection. This is java style invocation, apparently a simple call will not work. Are there ways...

Kotlin: Difference between constant in companion object and top level

The general pattern to create constants in Kotlin seems to be using companion objects. However, I can also define a constant at the file level. Why is that not so popular? Am I missing...

Can't access view in holder

I'm trying to create RecyclerView adapter and following this documentation. private class Adapter(private val list: List<HashMap<String, Any>>, private val ctx: Context) :...

What is the proper way to implement .addCallback() when providing RoomDatabase via Dagger 2?

I'm using Dagger 2 to create and share my RoomDatabase where necessary in my app. I'm trying to implement addCallback() so I can override the database's onCreate() function and use it to insert my...

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0

I've got a gradle FAILURE: ..."Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0." Case description: Attached to the project codebase the next...

What is Dart's equivalent to Kotlin's let?

Recently I've been delving into Flutter's ecosystem and Dart has proven itself a neat and simple language. Currently, I am looking for a best practice to run methods if an optional variable is not...

Cancel all pending Kotlin jobs from ViewModel's onCleared

With Kotlin 1.3 one can launch job using GlobalScope.launch but one thing that I can't seem to figure out is how to keep track of Job returned by ``GlobalScope.launch` and cancel all pending jobs...

Kotlin extension function start activity with Intent extras

I am trying to write a kotlin extension function for Context which will start a new activity in android with given class name and list of intent extras. I am able to successfully start activity...

Add optional parameter to Android Kotlin class

I've created an extension of DialogFragment(): class AlertDialogFragment(context: Context, val positiveButtonText: String, val...

Get an element with its index counting from the end in Kotlin

Is there a simple function to get an element with its index counting from the end in a Kotlin Array, List, or String? In other words, is there a Kotlin equivalent of negative index slicing in Python?

Mapbox IllegalStateException: Calling getSourceAs when a newer style is loading/has loaded

I'm getting the following error during my mapView.getMapAsync{} call: 2019-10-03 19:37:57.605 10964-10964/my.app E/AndroidRuntime: FATAL EXCEPTION: main Process: my.app, PID: 10964 ...

How to call private functions in Kotlin

I want to call a private functions of class SomeClass: class SomeClass { private fun somePrivateFunction() { //... } private fun somePrivateFunctionWithParams(text: String) { ...

Kotlin Coroutine SupervisorJob canceling behaviour

code: viewModelScope.launch(CoroutineExceptionHandler { _, _ -> Log.e("TAG", "task 1") }) { try { Log.e("TAG", "task 1 start") ...

Activity never becomes requested state in UI Testing

When implement code to test UI in android using kotlin. this is my code I thinks it 's simple code to test but android studio prevent me due to some problem > I searched a lot for this but no...

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

All of sudden I start getting this error, and I am not getting idea why if anyone just let me know where this error is, will be enough helpful. As much I am able to get is this because of new...

Android unable get back from email intent

So in my app I have a button to open gmail so the user can check for a verification code. I use this code to open gmail: val intent =...

How to use Single Live Event to show toast in Kotlin

I want to use single live event class to show toast (like flag) Here is my code what I tried. I want to not use peding like flag. how do I fix it? MainViewModel class MainViewModel(private val...

StateFlow collect emit NullPointerException

My repository layer have a MutableStateFlow, collecting it in my ViewModel. I am getting this NPE on some user devices Fatal Exception: java.lang.NullPointerException at...

BaseFragment with viewbinding

To use viewbinding in an android app I am basically creating base classes for Activity & Fragment to remove boilerplate of everytime writing inflating code. ACTIVITY: BaseActivity with...

Contents of Bundle in Firebase Analytics Event not showing in dashboard

I have the same issue as this thread but the answer is outdated and seems to be incorrect. It refers to a button "Add event parameters" which is not present in the current version of firebase. I...

w: Detected multiple Kotlin daemon sessions at build/kotlin/sessions

Performing ./gradlew assembleRelease and hit the issue of this. Below shows the entire error log... > Task :app:mergeDexRelease FAILED java.nio.file.NoSuchFileException:...

Unable to set abiFilter using kotlin dsl

After updating android gradle plugin from 4.0.3 to 4.1.0, I am not able to set abiFilter in kotlin DSL. Getting following error. Val cannot be reassigned

jetpack compose pass parameter to viewModel

how can we pass parameter to viewModel in jetpack compose? this is my composable @Composable fun UsersList() { val myViewModel: MyViewModel = viewModel("db2name") // pass param like...

android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify

After upgrading to android 12, the application is not compiling. It shows "Manifest merger failed with multiple errors, see logs" Error showing in Merged manifest: Merging Errors: Error:...

Android MVVM ROOM Single Sources of Truth Question

About Single Sources of Truth Google document said: Using this model, the database serves as the single source of truth, and other parts of the app access it using our UserRepository. Regardless...

How to handle activity lifecycle events for Mapbox Map in Jetpack Compose?

in the official documentation for Mapbox SDK for android there is a simple example of map using standard UI library for Android. Full documentation for completeness of the question can be found...

Kotlin 1.6, Jetpack Compose 1.1.0 compilation error

I have updated my project to Kotlin 1.6.10, Compose 1.1.0-rc01 and compose compiler to 1.1.0-rc02, because I need some new features in Compose. During compilation I have now this error, any...