Search code examples
androidcordovapluginsionic-frameworkcordova-plugins

Developing an Ionic/Cordova Plugin - Plugin won't build


I am currently working on a project to develop an android app using Ionic/Cordova. In this app we need to automatically connect to a wifi network using EAP TTLS and to do so we are developing a cordova plugin.

As many tutorials suggest, we started the development by creating an Android app that did exactly what we wanted. Next, we turned it into a plugin. Up to that point everything was great, we achieved the desired functionality and added the plugin to the Ionic project without errors.

Now, when we execute the command ionic build we receive many errors related to the imports used in the plugin, for example:

import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager; 
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

From all the research done before the development we understood that any app that you could be develop directly for Android could be turn into a plugin. Therefore, I believe that we are missing an important step in the process or taking the wrong approach to the development.

May anyone help me solve this or give me pointers to the proper way to build an Ionic/Cordova plugin?

Here is a copy of the error log:

  ConnectivityChangeReceiver.java:19: error: cannot find symbol
import com.wbinnova.adsfinetworkplugin.R;
                                      ^
  symbol:   class R
  location: package com.wbinnova.adsfinetworkplugin
  ConnectionActivity.java:9: error: package android.support.v7.app does not exist
import android.support.v7.app.ActionBarActivity;
                             ^
  ConnectionActivity.java:17: error: cannot find symbol
public class ConnectionActivity extends ActionBarActivity {
                                        ^
  symbol: class ActionBarActivity
  ScanActivity.java:13: error: package android.support.v7.app does not exist
import android.support.v7.app.ActionBarActivity;
                             ^
  ScanActivity.java:21: error: cannot find symbol
public class ScanActivity extends ActionBarActivity {
                                  ^
  symbol: class ActionBarActivity
  ConnectivityChangeReceiver.java:93: error: package R does not exist
                Notification notification = new Notification(R.mipmap.ic_launcher, "Red Ads-Fi disponible", System.currentTimeMillis());
                                                              ^
  ConnectionActivity.java:24: error: cannot find symbol
        super.onCreate(savedInstanceState);
        ^
  symbol:   variable super
  location: class ConnectionActivity
  ConnectionActivity.java:27: error: cannot find symbol
        wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                  ^
  symbol:   method getSystemService(String)
  location: class ConnectionActivity
  ConnectionActivity.java:53: error: no suitable constructor found for Builder(ConnectionActivity)
        AlertDialog.Builder builder = new AlertDialog.Builder(ConnectionActivity.this);
                                      ^
    constructor Builder.Builder(Context,int) is not applicable
      (actual and formal argument lists differ in length)
    constructor Builder.Builder(Context) is not applicable
      (actual argument ConnectionActivity cannot be converted to Context by method invocation conversion)
  ConnectionActivity.java:22: error: method does not override or implement a method from a supertype
    @Override
    ^
  ConnectionActivity.java:62: error: package R does not exist
        getMenuInflater().inflate(R.menu.menu_main, menu);
                                   ^
  ConnectionActivity.java:62: error: cannot find symbol
        getMenuInflater().inflate(R.menu.menu_main, menu);
        ^
  symbol:   method getMenuInflater()
  location: class ConnectionActivity
  ConnectionActivity.java:59: error: method does not override or implement a method from a supertype
    @Override
    ^
  ConnectionActivity.java:71: error: package R does not exist
        if (id == R.id.action_settings) {
                   ^
  ConnectionActivity.java:75: error: cannot find symbol
        return super.onOptionsItemSelected(item);
               ^
  symbol:   variable super
  location: class ConnectionActivity
  ConnectionActivity.java:66: error: method does not override or implement a method from a supertype
    @Override
    ^
  ScanActivity.java:35: error: cannot find symbol
        super.onCreate(savedInstanceState);
        ^
  symbol:   variable super
  location: class ScanActivity
  ScanActivity.java:36: error: package R does not exist
        setContentView(R.layout.activity_main);
                        ^
  ScanActivity.java:39: error: cannot find symbol
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                 ^
  symbol:   method getSystemService(String)
  location: class ScanActivity
  ScanActivity.java:33: error: method does not override or implement a method from a supertype
    @Override
    ^
  ScanActivity.java:54: error: package R does not exist
        getMenuInflater().inflate(R.menu.menu_main, menu);
                                   ^
  ScanActivity.java:54: error: cannot find symbol
        getMenuInflater().inflate(R.menu.menu_main, menu);
        ^
  symbol:   method getMenuInflater()
  location: class ScanActivity
  ScanActivity.java:51: error: method does not override or implement a method from a supertype
    @Override
    ^
  ScanActivity.java:63: error: package R does not exist
        if (id == R.id.action_settings) {
                   ^
  ScanActivity.java:67: error: cannot find symbol
        return super.onOptionsItemSelected(item);
               ^
  symbol:   variable super
  location: class ScanActivity
  ScanActivity.java:58: error: method does not override or implement a method from a supertype
    @Override
    ^
  ScanActivity.java:74: error: cannot find symbol
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                 ^
  symbol:   method getSystemService(String)
  location: class ScanActivity
  ScanActivity.java:99: error: cannot find symbol
        WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                              ^
  symbol:   method getSystemService(String)
  location: class ScanActivity
  ScanActivity.java:153: error: cannot find symbol
            Intent intent2 = new Intent(ScanActivity.this, ConectionActivity.class);
                                                           ^
  symbol:   class ConectionActivity
  location: class ScanActivity
  ScanActivity.java:154: error: cannot find symbol
            startActivity(intent2);
            ^
  symbol:   method startActivity(Intent)
  location: class ScanActivity

Solution

  • It seems you're creating a Java action, you need to create a simple Java class with some functions that will allow you transform that Java class into a plugin for cordova

    Here you can find how to create a simple echo plugin:

    https://cordova.apache.org/docs/en/2.5.0/guide/plugin-development/android/

    Pay attention that it extends CordovaPlugIn and the main function is execute with 3 arguments

    • The name of the action that will be executed. String parameter
    • The arguments. JSONArray parameter
    • The callback. This will be called when the function end and if you want to return a value pass it as argument of the functions success or error