Search code examples
androidkotlinkotlin-multiplatformkotlin-multiplatform-mobile

Actual typealias 'ApplicationContext' has no corresponding expected declaration


I got this issue when trying to custom context for kotlin multiplatform

Actual typealias 'ApplicationContext' has no corresponding expected declaration
The following declaration is incompatible because modality is different:
    public final expect class ApplicationContext

androidMain

import android.app.Application

actual typealias ApplicationContext = Application

commonMain

expect class ApplicationContext

iosMain

import platform.UIKit.UIView

actual typealias ApplicationContext = UIView

enter image description here


Solution

  • The expect class structure should match the class you're typealiasing to.

    Android's context class is an abstract class

    public abstract class Context {}
    

    So the modality error comes from there.

    You would need the expect class to be abstract as well to fix that particular error

    expect abstract class ApplicationContext
    

    Making it abstract would break modality for UIView as it's not abstract. So what you're trying to do is not possible in a straightforward manner.

    You would need to think about a different strategy for what you're trying to achieve.