Search code examples
androidionic-frameworkcapacitorcapacitor-plugin

Capacitor Plugin for Ionic App in Kotiln Unresolved reference: Plugin


I have created a capacitor plugin and an Ionic app locally for development in two separate directories. Both the plugin and the app on Android are using Kotlin.

I am using

Ionic Framework : @ionic/react 6.0.7 Capacitor CLI : 3.4.1
@capacitor/android : 3.4.1
@capacitor/core : 3.4.1

I built the plugin, then linked it with npm install ../capacitor/capacitor-my-plugin

In Android studio when I go to the main activity I can register the plugin

package io.ionic.starter

import com.getcapacitor.BridgeActivity
import android.os.Bundle
import com.example.plugin.MyPlugin

class MainActivity : BridgeActivity() {
  public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    registerPlugin(MyPlugin::class.java)
  }
}

I can click into the MyPlugin class and see that it is linked and all my code is there but when I run the application from the command line with ionic capacitor run android I get an error Unresolved reference: MyPlugin

I check and the capacitor.settings.gradle include my plugin

include ':capacitor-my-plugin'
project(':capacitor-my-plugin').projectDir = new File('../capacitor/capacitor-my-plugin/android')

In my javascript I've added a button to call my plugin

import { MyPlugin } from 'capacitor-my-plugin'

<IonButton onClick={(e) => MyPlugin.open({value: {value:""}})}>
  Activate
</IonButton>

If I remove the import and the registering in the main activity the app runs fine.


Solution

  • When I converted the plugin to kotlin, Android Studio did not automatically add apply plugin: 'kotlin-android' to the plugin's build.gradle file.