Custom kotlin extension for android context


#1

Hi,
I want to use this extension in my android project,
I created a file called KotlinExtension.kt in the root of the package:

package some.name

...

fun Context.getDrawable(resId: Int): Drawable {
   return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       getDrawable(resId)
   } else {
       resources.getDrawable(resId)
   }
}

I want to access this custom extension from another file under the same package:

package some.name.activities

import some.name.KotlinExtension.*
...

class activity: Activity() {

   fun test() {
       context.getDrawable(R.some.drawable)
   }
}

But it references it to the regular context.getDrawable function…
How can I access this extension?
Thanks.


#2

If an method signature already exists, that signature will be chosen and the extensionFunction will be ignored.
Or in short, member-functions always win.

So, rename your function or change the parameters.


#3

I will try it, thanks.