findViewByID genericized return in API 26: how are YOU coping with it?

100+ errors when trying to use compileSdk 26:

val parent = activity.findViewById(android.R.id.content) as ViewGroup

Of course, if I had typed the declarations, they would all work. But as a good Kotlin developer, I did not.

val parent: ViewGroup = activity.findViewById(android.R.id.content)

I can only image the chagrin a team with a very large Kotlin Android codebase would face :expressionless: Has anyone come up with a regular expression, or shell script, anything that can automate this? I am a little rusty with that!

This should also work.

val parent = activity.findViewById<ViewGroup>(android.R.id.content)

IntelliJ’s structural search and replace would be helpful for converting

Structural search & replace is not supported for Kotlin, so no, it would not help.

I’am try do this ,And replace all findViewById

    fun View.findViewByIdOld(id: Int) :View{

        return findViewById<View>(id)
    }

You should be able to “search and replace in path” this Regex to catch most occurrences:

((?:\w+\.)*findViewById)\(((?:\w+\.)*\w+)\)\s+as\s+(\w+)

Using the replace string:

$1<$3>($2)
1 Like

Good to know, thanks!

An additional follw-up, I also had lines like

val view = findViewById(R.id.something)

I also ran this pattern

((?:\w+\.)*findViewById)\(((?:\w+\.)*\w+)\)
$1<View>($2)

THanks