Ionic build cannot find module error. How to import Android plugin?

Gustavo Conde

I'm trying to develop an Android Plugin to use in an Ionic app.

I created the app with a starter project already provided by Ionic using ionic start myApp tabs.

I have also created, in a different folder, my plugin, that contains the following files.

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
    id="location-plugin" version="0.0.1">
<name>GSLocationManager</name>
<description>Location Plugin</description>
<license>MIT</license>
<keywords>cordova,device,sensors,location</keywords>

<js-module name="LocationManager" src="www/LocationManager.js">
    <clobbers target="LocationManager" />
</js-module>

<engines>
    <engine name="cordova" version=">=3.6.0"></engine>
</engines>

<platform name="android">
  <preference name="GOOGLE_PLAY_SERVICES_VERSION" default="11+"/>
  <preference name="ANDROID_SUPPORT_LIBRARY_VERSION" default="26+"/>
  <preference name="ICON" default="@mipmap/icon" />
  <preference name="SMALL_ICON" default="@mipmap/icon" />
  <preference name="ACCOUNT_NAME" default="@string/app_name" />
  <preference name="ACCOUNT_LABEL" default="@string/app_name" />
  <preference name="ACCOUNT_TYPE" default="$PACKAGE_NAME.account" />
  <preference name="CONTENT_AUTHORITY" default="$PACKAGE_NAME" />

  <framework src="com.google.android.gms:play-services-location:$GOOGLE_PLAY_SERVICES_VERSION" />
  <framework src="com.android.support:support-v4:$ANDROID_SUPPORT_LIBRARY_VERSION" />
  <framework src="com.android.support:appcompat-v7:$ANDROID_SUPPORT_LIBRARY_VERSION" />
  <!-- <framework src="android/dependencies.gradle" custom="true" type="gradleReference"/> -->

  <source-file src="path/to/locationmanager/GSLocationManager.java"
               target-dir="path/to/locationmanager"></source-file>

  <config-file target="AndroidManifest.xml" parent="/manifest">
     <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
     <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
     <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.hardware.location" />
  </config-file>

  <config-file target="res/xml/config.xml" parent="/*">
      <feature name="GSLocationManager">
          <param name="android-package" value="android.package.locationmanager.GSLocationManager" />
      </feature>
  </config-file>
</platform>

locationManager.js

var GSLocationManager = {
  getCurrentLocation: function(success, failure) {
    exec(success, failure, 'GSLocationManager', 'getCurrentLocation', []);
  }
}

module.exports = GSLocationManager;

GSLocationManager.java

package android.package.locationmanager;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaPlugin;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class GSLocationManager extends CordovaPlugin {

    private JSONObject data = new JSONObject();

    // at the initialize function, we can configure the tools we want to use later, like the sensors
    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);

    }

    // safety unregistering from the events if the application stops somehow
    @Override
    public void onDestroy() {

    }

    // this is the main part of the plugin, we have to handle all of the actions sent from the js
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if ("getCurrentLocation".equals(action)) {
            Location location = getCurrentLocation();
            JSONObject json = new JSONObject();
            json.put("latitude", location.getLatitude());
            json.put("longitude", location.getLongitude());
            callbackContext.success(json);
            return true;
        }
        return false;  // Returning false results in a "MethodNotFound" error.
    }

    @SuppressLint("MissingPermission")
    private Location getCurrentLocation() {
        LocationManager locationManager = (LocationManager) cordova.getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        Location lastKnownGPSLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        return lastKnownGPSLocation;
    }
}

Then I created a package.json for my plugin usign plugman And after that, added my android plugin to my Ionic project by doing ionic cordova plugin add path/to/my/plugin

But I cannot seem to be able to import my plugin to app.module.ts using import { GSLocationManager } from 'globespinning-location-plugin' I try to build but CLI says ERROR in src/app/app.module.ts(12,31): error TS2307: Cannot find module 'globespinning-location-plugin'.

Any idea where the error is? How should I import a plugin created by me, and referenced from a local folder? I want to be able to call GSLocationManager.getCurrentLocation() inside my ionic app.

Gustavo Conde

What @Tachyon and @SeunBincom answer was helpful, but the problem was with my plugin.xml file. With the help of Android Studio I was able to debug it and finally got it working.

The xml ended up like this:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        id="globespinning-location-plugin" version="0.0.1">
    <name>LocationManager</name>
    <description>Globespinning Location Plugin</description>
    <license>MIT</license>
    <keywords>cordova,device,sensors,location</keywords>

    <js-module name="LocationManager" src="www/locationManager.js">
        <clobbers target="LocationManager" />
    </js-module>

    <platform name="android">
      <preference name="GOOGLE_PLAY_SERVICES_VERSION" default="11+"/>
      <preference name="ANDROID_SUPPORT_LIBRARY_VERSION" default="26+"/>
      <preference name="ICON" default="@mipmap/icon" />
      <preference name="SMALL_ICON" default="@mipmap/icon" />
      <preference name="ACCOUNT_NAME" default="@string/app_name" />
      <preference name="ACCOUNT_LABEL" default="@string/app_name" />
      <preference name="ACCOUNT_TYPE" default="$PACKAGE_NAME.account" />
      <preference name="CONTENT_AUTHORITY" default="$PACKAGE_NAME" />

      <framework src="com.google.android.gms:play-services-location:$GOOGLE_PLAY_SERVICES_VERSION" />
      <framework src="com.android.support:support-v4:$ANDROID_SUPPORT_LIBRARY_VERSION" />
      <framework src="com.android.support:appcompat-v7:$ANDROID_SUPPORT_LIBRARY_VERSION" />
      <framework src="src/android/dependencies.gradle" custom="true" type="gradleReference"/>

      <source-file src="src/android/com/globespinning/ionic/locationmanager/GSLocationManager.java"
                   target-dir="src/com/globespinning/ionic/locationmanager"></source-file>

      <config-file target="AndroidManifest.xml" parent="/manifest">
         <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
         <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
         <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
         <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
         <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
         <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
         <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
         <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
         <uses-permission android:name="android.permission.INTERNET" />
         <uses-permission android:name="android.permission.WAKE_LOCK" />
         <uses-permission android:name="android.hardware.location" />
      </config-file>

      <config-file target="res/xml/config.xml" parent="/*">
          <feature name="LocationManager">
              <param name="android-package" value="com.globespinning.ionic.locationmanager.GSLocationManager" />
          </feature>
      </config-file>
    </platform>
</plugin>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

webpacker Uncaught Error: Module build failed / Cannot find module 'babel-plugin-syntax-dynamic-import'

From Dev

Ionic build error Cannot find module './scheduler/Action'

From Dev

ionic cordova build android Cannot find module '../pages/Wallet/'

From Dev

Ionic 2 - Runtime error Cannot find module "."

From Dev

Ionic => Runtime error: Cannot find module "."

From Dev

Runtime Error Cannot find module "ionic-native" IONIC 2

From

How do I import global modules in Node? I get "Error: Cannot find module <module>"?

From Dev

Ionic 3: Build in “prod” mode: Cannot find module “.”

From Dev

Ionic Cordova Build prod: Cannot find module “.” - Typescript version >3

From Dev

Module build failed: Error: Cannot find module 'node-sass'

From Dev

Error: Cannot find module '/app/__sapper__/build' on Cloud Build

From Dev

Dynamic import components error - Uncaught (in promise) Error: Cannot find module

From Dev

Ionic: GoogleAnalytics plugin Error: ts: Cannot find name 'GoogleAnalytics'

From Dev

How can I resolve Ionic Uncaught Error: Cannot find module "." when adding new page?

From Dev

Error: "Cannot find module" when cloning Ionic 4 component with StencilJS

From Dev

Ionic Uncaught Error: Cannot find module "." when importing a service provider

From

Error: Cannot find module '@ionic/app-scripts'

From Dev

Error: Cannot find module "rxjs/operators" in Ionic Angular

From Dev

Ionic3 - Uncaught Error: Cannot find module '.'

From Dev

Error: Cannot find module 'symbol-observable' in Ionic 4

From Dev

Ionic build android --prod cleancss broken @import error

From Dev

Cordova can't add plugin: Error: Cannot find module 'unorm'

From Dev

Error: Cannot find module 'extract-text-webpack-plugin'

From Dev

Uncaught Error: Cannot Find Module When Using Dynamic Import for JavaScript

From Dev

Simple absolute import causing 'Cannot find module' error

From Dev

How to solve Error : cannot find module "ejs"?

From Dev

Cordova 4.3.0 - build command returns error Cannot find module 'Q'

From Dev

Error: Cannot find module '../build/Release/bson' on Mac

From Dev

npm build error with svelte: Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module 'X' or its corresponding type declarations

Related Related

  1. 1

    webpacker Uncaught Error: Module build failed / Cannot find module 'babel-plugin-syntax-dynamic-import'

  2. 2

    Ionic build error Cannot find module './scheduler/Action'

  3. 3

    ionic cordova build android Cannot find module '../pages/Wallet/'

  4. 4

    Ionic 2 - Runtime error Cannot find module "."

  5. 5

    Ionic => Runtime error: Cannot find module "."

  6. 6

    Runtime Error Cannot find module "ionic-native" IONIC 2

  7. 7

    How do I import global modules in Node? I get "Error: Cannot find module <module>"?

  8. 8

    Ionic 3: Build in “prod” mode: Cannot find module “.”

  9. 9

    Ionic Cordova Build prod: Cannot find module “.” - Typescript version >3

  10. 10

    Module build failed: Error: Cannot find module 'node-sass'

  11. 11

    Error: Cannot find module '/app/__sapper__/build' on Cloud Build

  12. 12

    Dynamic import components error - Uncaught (in promise) Error: Cannot find module

  13. 13

    Ionic: GoogleAnalytics plugin Error: ts: Cannot find name 'GoogleAnalytics'

  14. 14

    How can I resolve Ionic Uncaught Error: Cannot find module "." when adding new page?

  15. 15

    Error: "Cannot find module" when cloning Ionic 4 component with StencilJS

  16. 16

    Ionic Uncaught Error: Cannot find module "." when importing a service provider

  17. 17

    Error: Cannot find module '@ionic/app-scripts'

  18. 18

    Error: Cannot find module "rxjs/operators" in Ionic Angular

  19. 19

    Ionic3 - Uncaught Error: Cannot find module '.'

  20. 20

    Error: Cannot find module 'symbol-observable' in Ionic 4

  21. 21

    Ionic build android --prod cleancss broken @import error

  22. 22

    Cordova can't add plugin: Error: Cannot find module 'unorm'

  23. 23

    Error: Cannot find module 'extract-text-webpack-plugin'

  24. 24

    Uncaught Error: Cannot Find Module When Using Dynamic Import for JavaScript

  25. 25

    Simple absolute import causing 'Cannot find module' error

  26. 26

    How to solve Error : cannot find module "ejs"?

  27. 27

    Cordova 4.3.0 - build command returns error Cannot find module 'Q'

  28. 28

    Error: Cannot find module '../build/Release/bson' on Mac

  29. 29

    npm build error with svelte: Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module 'X' or its corresponding type declarations

HotTag

Archive