Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a third party flutter plugin

I'm building a flutter app which uses a third party plugin.

The plugin has a bug in its android java code.

The problem is that I'm having trouble finding documentation on how to setup a development environment to debug the plugin from within my app.

I should note that the original developer can't reproduce the bug and hence I need to get it running within my app.

The instructions I've found so far involve having to build an apk, but this is time consuming and seems like its shouldn't be necessary.

I'm an experience java and dart programmer so I'm not having trouble with the basics (i.e. I have Android studio and vs code running and I can debug the plugin using its own sample code).

This is the process I have so far:

git clone the plugin to my local system.

Within my application add an override in my apps pubspec.yaml to 
import the plugin from my local system.

dependency_overrides:
  flutter_sound:
    path: ../flutter_sound

Build my app so we can open in android studio.


    Note: I had to delete my .pub-cache/hosted folder and then run 
   'flutter pub get' as some older (unused?) packages seem to stop 
    the build. I also ensure that I had the latest packages for 
    every package I was using.

flutter build apk


In android studio

Import my app project
- open the android project contained with my app project folder.
 e.g.
  ~/git/app/android

Wait for the gradle build to complete.

Start the android studio debugger and select a simulator.

Open the 'Android Monitor' tab at the bottom of Android Studio.
like image 230
Brett Sutton Avatar asked Jan 23 '20 22:01

Brett Sutton


People also ask

How do I debug a plugin in flutter?

The first thing to know to properly debug a Flutter plugin, is to debug the Dart code. If you're using VSCode or Android Studio, it is straightforward. In the lib folder, you can just click on the left end of a line to add a breakpoint.

How do you debug the native code in flutter?

Well if you're using android studio IDE you can open only android module of your app and then you can write and debug your code as a native android app. You can do this with a right click on your project root folder and go to Flutter > Open android module in android studio option.


Video Answer


1 Answers

So I managed to get this working.

There may be some steps superfluous to the process but I've included them all here as I've not had time to go back and work out which ones aren't required.

Note: a plugin is always contained within a dart 'package'.

git clone the third party package (flutter_sound in this example) to your local system.

Within your application's pubspec.yaml add an override to import the package from your local system.

    dependency_overrides:
      flutter_sound:
        path: ../flutter_sound

Update your dependencies:

pub upgrade

Build your app so we can open in android studio.

Within the apps project root dir (where your pubspec.yaml is located) run:

flutter build apk

From android studio

Import your app project

Wait for the gradle build to complete (this took a couple of minutes).

Find the 'android' folder within your application project (from within the Project panel in studio).

Right click the android folder. Select 'Flutter -> Open Android module in Android Studio'

When prompted tell Android Studio to open the Android Module in a new window.

Wait for the gradle build to complete.

Start the android studio debugger and select a emulator.

Open the 'Android Monitor' tab at the bottom of Android Studio.

You should now be able to set break points in your java code.

Good luck.

like image 143
Brett Sutton Avatar answered Oct 10 '22 16:10

Brett Sutton