I've experienced a crash when bumping Kotlin from ...
# android
c
I've experienced a crash when bumping Kotlin from 2.1.20 to 2.2 and wondering if something has changed in the compiler. For context, the crash is totally expected but I'm curious why this doesn't crash prior to 2.2. I have something similar to the following snippet of code:
Copy code
private lateinit var scrollView: ScrollView
fun onCreateView(): View? {
    val view = super.onCreateView()
    if (view != null) {
        scrollView = view.findViewById(R.id.scoll_view)
    }
}
The crash happens at the assignment of findViewById, as the layout doesn't actually exist. We didn't notice this crash as it's in an abstraction and this property isn't actually accessed or read. However since bumping to Kotlin 2.2 this crashes with an NPE - which is actually expected. Does anyone know what might have changed in 2.2 that might have caused this? Or why we wouldn't have seen this prior to 2.2?
p
My XML will be rusty at this point but shouldn't findViewById used during onViewCreated?
c
I don't think that's the issue, as were invoking the super instance and using the returned view to invoke the function on. Regardless this is a function that has existed for quite a few years in a high volume production app
👍 1
g
Can you share link to a minimal, reproducible project? That'll make it much easier for others to take a look at the issue and help.
c
sure, heres a reproducable project -> https://github.com/IndianaSloan/kotlin-view-interop-crash The kotlin version in this project is 2.1.20 and it does not crash - however it should as on MainActivity::20 it attempts to resolve a view that does not exist in the view tree. This NPE crash does happen when bumping kotlin to 2.2.*+ and I'm trying to understand the reason for this
g
Seems it might be a regression in Kotlin 2.1.x versions as the crash is occurring as expected on versions prior to 2.x (pre-k2 compiler) as well as 2.2.x onwards. I tested on versions 1.9.22, 2.0.10, 2.1.21 (project language version) and 2.2.10 and the crash did not occur on 2.0.10 and 2.1.21. Edit: Found https://youtrack.jetbrains.com/issue/KT-71752, https://youtrack.jetbrains.com/issue/KT-75677, https://youtrack.jetbrains.com/issue/KT-75649 which seem related to your issue. A couple of questions/musings: 1. Were you not getting this crash on first access prior to version 2.2.x? Regardless, given the
textView
is still a platform type, the nullability is unknown and is on the developer to decide whether to handle it as a null or non-nullable type. So if the value is null, the app should have still crashed. I verified this by setting a click listener on text to when it crashed every time doing activity creation, unless I handled it as a null type. 2. The lint warning - did this not show for you? This doesn’t answer the question but would’ve completely avoided the crash.
c
Thanks for following up! This is a simplified example to demonstrate the problem. I do get the lint warning in the sample, but where I'm experiencing this is in a complex abstraction and the lint warning is not displayed. In my production app, the view is never accessed/read in the case where I'm now experiencing this crash. It's instantiated in a base class and read/access in one implementation (where it does exist in the view tree), but not read/access in another (where it doesnt exist in the view tree) - which is where it is now crashing, but didn't with Kotlin 2.1 I think I have enough to understand the risks, before fixing and bumping to Kotlin 2.2! Thanks
👍 1
g
I'm planning to try and go deeper to understand the regression if time allows today. Perhaps I'll be able to post more findings here.
👍🏼 1