How to debug a cordova plugin for Android

2020-06-16 09:06发布

问题:

Hi I have created a cordova plugin for Android. But it is not functioning properly.

I want to debug it an IDE.

How can I debug the cordova android plugin file when I launch the application in my mobile?

What are the different and easiest ways to do it?

Thanks in advance.

回答1:

Expanding response of laughingpine, i use this method: Debug javascript part with chrome + dev tools + genymotion. You can access it from "inspect devices" and you can debug javascript part. The problem is for debug my own plugins. For this, i build project "cordova build" from console, and import into Android studio the content of platforms\android from project folders. After repair some errors whit gradle, i can run and debug whit android studio + genymotion, to acces the .java files.



回答2:

I think the best way to do this would be with logcat. Logcat is also built into Android Studio, so you can use that IDE to help you debug.

If you are running logcat from the command line, you can limit to a certain tag using the following: adb logcat -s "YOURTAG"

If you are running via Android Studio, see this documentation.. You are able to attach the debugger to a running process, so if you didn't do up your application in Android Studio, you can still attach the Android studio debugger to it. This is done by selecting Attach debugger to Android proccess. Again see the documentation for more.

Android Studio also can also filter based on tag name, log type, etc. This is done by expanding the filters menu, and adding a filter (filter by type, tagname, regex are accepted.)

Lastly, actually getting your plugin to spit out valuable information. First import the Log class: import android.util.Log; and setup a TAG private static final String TAG = "MYTAGNAME". Whenever you wish to write to the log (in this example, the debug level), call Log.d(TAG, "Your message to debug");

See the log documentation for more.

Personally I was right at home using logcat from the command line, but Android Studio does offer a slick interface for debugging your plugins.



回答3:

  1. Build your Android app (via cordova build android) and install the app in your device. For instance, I installed Dropbox on computer and device and transferred the files between them with that free service.

  2. Plug the device into the computer with your USB plug.

  3. Set up the device to enable debugging. In device, Settings:

Allow USB Debugging

Select debug app

  1. In console, type to following so we can read all the messages in full:

adb logcat -v long

If you want to output all the messages to a text file on your Mac desktop (it’s different for a PC), type:

adb logcat > ~/Desktop/logcat.txt

Immediately the console should be outputting a lot of lines of text. If it stops at

-waiting for device -

and freezes, then check your screen to see if there is a popup to answer. If there is none, then unplug the USB from the device and try again. In my Settings > Developer Options, I chose “Select debug app,” then chose the app I installed; that might help too.

When the reporting stops, sometimes I hit Return a few times to mark the place with a large gap of space before doing the next step. This tells me how far to back up.

  1. Tap on app icon in device to start it.

  2. Read the output in logcat.

  3. Tap Ctrl+C to stop logcat.



回答4:

Open Chrome on your dev machine, enter developer tools (right click -> inspect). In the top right of developer tools is an icon with three vertical dots, press it and select "more tools" -> "inspect devices".

If your Android device is plugged into your machine and running the Cordova project, you should see your Android device listed on the left hand side of the "inspect devices" dialog. Click the name and the dialog changes and a button becomes available labelled "inspect". Press this button and a new window comes up with Chrome Developer Tools that you can use to fully debug your Hybrid App.