Kotlin Custom View class implements an interface that cannot be resolved in `:app:compileDebugKotlinAfterJava`. Identical Java version works fine


#1

I have an Android project which contains an aar library module with generic classes that are being used as a foundation for several apps. Everything is written in Kotlin and no Java code has been used (except third party libraries).

The issue occurs when trying to assemble the app for running (not building, the build passes). I get the following error when trying to run my application:

:app:compileDebugKotlinAfterJava
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class com.test.app.view.ECMKotlinTextView, unresolved supertypes: com.test.lib.localization.Localizable

For the purpose of this question, I have written two separate versions of a custom TextView that extends the android.widget.TextView class and implements an interface, let’s say Localizable, one in Java and the other one in Kotlin.

ECMJavaTextView.java

package com.test.lib.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

import com.test.lib.localization.Localizable;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;


public class ECMJavaTextView extends TextView implements Localizable{
    public ECMJavaTextView(Context context) {
        super(context);
    }

    public ECMJavaTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ECMJavaTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ECMJavaTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Nullable
    @Override
    public String getTextId() {
        return "JAVA TEXT ID";
    }

    @Override
    public void setTextId(@Nullable String s) {

    }

    @Nullable
    @Override
    public Map<String, String> getTextArgs() {
        return null;
    }

    @Override
    public void setTextArgs(@Nullable Map<String, String> map) {

    }

    @Override
    public void updateText(@NotNull String text) {

    }
}

ECMKotlinTextView.kt

package com.test.lib.view

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Canvas
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.widget.TextView
import com.test.lib.localization.Localizable
import com.test.lib.localization.updateText

class ECMKotlinTextView : TextView , Localizable {

       override var textId: String? = "textID"
        get() = field
        set(value) {
            field = value
        }
    override var textArgs: Map<String, String>? = null
        get() = field
        set(value) {
            field = value
        }

    override fun updateText(text: String) {
        
    }
    constructor(context: Context?) : super(context){
        
    }
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
       
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
       
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int, defStyleRes: Int) : super(context, attrs, defStyle, defStyleRes) {
        
    }
    
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
    }

}

Both have xml layouts in the library module and I include them into the main_activity.xml file. I managed to isolate the problem down to the findViewbyId() method (here I use the kotlin-extensions library to get the views; tried with both).

MainActivity.kt

import kotlinx.android.synthetic.main.content_main.*

class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        var java_textV = demo_java_text_view as ECMJavaTextView
        Log.d("JAVA", "${java_textV.textId}")

        var kotlin_textV = demo_kotlin_text_view as ECMKotlinTextView
        Log.d("Kotlin", "${kotlin_textV.textId}")
    }
...

The second cast always gives the error mentioned above, but if I remove the cast (ECMKotlinTextView) I do not have access to the Localizable methods that I need. A more interesting point is that the JAVA class works as expected.

Don’t know for sure if it’s something that I’m missing or haven’t setup or if it’s currently a bug in the Kotlin plugins. Can somebody help me with this? Some insight would be much appreciated.


#2

Any resolution to this? I am seeing a similar issue.


#3

There was a problem with the IDE caching and indexing, and deleting the build folder didn’t help. Click on Invalidate Caches and Restart in Android Studio’s menu.

This should solve the issue.


#4

I am also experiencing this issue, however invalidating the cache hasn’t fixed the problem.

EDIT: From testing I have also found that the issue is with findViewById. The Kotlin Android Extensions work though.

EDIT2: …Works fine until I try and call any of the methods within my class. Then I get the same error above.