user3307102
user3307102

Reputation: 305

Problems inheriting inner java class

I'm creating an android live wallpaper using Kotlin. This requires a class that extends WallpaperService, which contains an inner class that extends WallpaperService.Engine.

So I've written this:

import android.service.wallpaper.WallpaperService
import android.service.wallpaper.WallpaperService.Engine

public class MyWallpaperService : WallpaperService() {

    override fun onCreateEngine(): Engine = MyEngine()

    private inner class MyEngine : Engine() {

    }
}

The problem is that I'm getting the following 2 errors at compile time:

Error:java.lang.RuntimeException: Error generating constructors of class MyEngine with kind IMPLEMENTATION 

Error:java.lang.UnsupportedOperationException: Don't know how to generate outer expression for lazy class MyWallpaperService

I cant figure out why this is happening so any help would be greatly appreciated.

Upvotes: 3

Views: 535

Answers (3)

Travis Castillo
Travis Castillo

Reputation: 1827

For anyone else searching through this issue I'd like to point out that this now works in Kotlin ( i'm using 1.4.10 I haven't verified what version this was fixed).

Key to remember is that the Engine has to be marked an inner class or Kotlin won't know how to reference the Engine being inherited.

class SampleService : WallpaperService() {

// region override
override fun onCreateEngine(): Engine {

    return IntermediateEngine()
}

// endregion

// region inner class

inner class IntermediateEngine() : Engine() {

    
}

// endregion

}

Upvotes: 1

carlospiles
carlospiles

Reputation: 397

The best solution I have found is to use an intermediate Java class:

public class Intermediate extends WallpaperService.Engine {
    public Intermediate(WatchfaceService outer) {
        outer.super();
    }
}

Then the inner class in your Kotlin WallpaperService should inherit Intermediate, passing the outer class as parameter.

public class MyWallpaperService : WallpaperService() {
    override fun onCreateEngine(): Engine = MyEngine()
​
    private inner class MyEngine : Intermediate(this) {
    }
}

Upvotes: 3

D3xter
D3xter

Reputation: 6435

See KT-6727

You can try the following workaround:

private inner class MyEngine : super.Engine() {
}

Upvotes: 1

Related Questions