How to debug a third party flutter plugin

3,373

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.

Share:
3,373
Brett Sutton
Author by

Brett Sutton

Updated on December 17, 2022

Comments

  • Brett Sutton
    Brett Sutton over 1 year

    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.
    
  • abhijat_saxena
    abhijat_saxena over 3 years
    I wanted to debug the dart code of the package, is it possible ?
  • Raghu Mudem
    Raghu Mudem about 3 years
    Thanks for the clue. This idea helped me to keep debug logs for others plugin.
  • david dagan
    david dagan about 2 years
    you are the king!