26

I have been trying to debug the native android code that is invoked through platform channel in flutter and the debugging is only in the flutter side. The execution doesn't hit the breakpoint in the native android. The application is working as expected but the debugging doesn't. I am using Android studio. Can some one please tell me how to debug the native android code ?

9
  • Maybe add the source code so we can help you Commented Mar 4, 2019 at 15:41
  • 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. Commented Mar 4, 2019 at 15:44
  • 2
    When I try that the Flutter option in the drop down is greyed out. And I am using the platform_channels sample example that is in flutter. Commented Mar 4, 2019 at 15:46
  • Then try File > Open > yourProjectName > android. In android studio IDE of course. Commented Mar 4, 2019 at 15:49
  • Yep I have that and when I try to debug that - it crashes when I try to debug it. Commented Mar 4, 2019 at 15:53

5 Answers 5

8

Breakpoints are working fine for me when using Android Studio at project/android/, on both emulator and physical device. And remember to Debug instead of Run (^D instead of ^R). Note, it did not work when opening the project from project/.


I don't think making a new android project to write the Native code for a flutter app is a good option (vivek's suggestion). There are many Flutter specific considerations you need to make on the native side, it is not just an Android app.

Sign up to request clarification or add additional context in comments.

Comments

4

If you navigate to MainActivity.kt or MainActivity.java in the android studio then you can see "Open for Editing in Android Studio" on the top right. click on that and it will open you a new window where you can easily write native code.

Well, better if you can write whole code in a new android project and test there then use that code to make flutter plugin. I think this will be faster than to edit in flutter project.

let me know what others think.

2 Comments

This is the only method I have gotten to work. I can then attach to running app or just build from there.
According to the office Doc: flutter.dev/docs/testing/oem-debuggers, but attaching android debug using "attach debugger to android process" does not work now, or it can use current window to switch two different debuggers.
1

The answer given by @Marcos Boaventura to follow this blog helped fix the issue. https://medium.com/flutterpub/flutter-app-couldnt-find-libflutter-so-c95ad81cbccd

After fixing the gradle the debug is successful. Thank you @Marcos Boaventura

1 Comment

I am glad for this :)
1

I had a similar problem. Debugging native code on an emulator worked, while debugging on a physical device (Zebra TC25, Android 7.1) crashed with a Fatal signal 11 (SIGSEGV). Switching from the Flutter beta channel to the Flutter stable channel fixed it for me.

Comments

1

If your flutter app works and you just want to use some breakpoints in Kotlin code you can use the Attach Debugger button

  1. Open the Android package:
  • Open Android Studio and navigate to your Flutter project.
  • Locate the android folder inside your Flutter project and open it as a separate Android project in Android Studio.
  1. Run your Flutter app:
  • Launch your Flutter app as you usually do. You can use a terminal, flutter run, or your preferred IDE (like Visual Studio Code).
  1. Attach the debugger:
  • In the Android Studio instance you opened earlier, go to the top menu bar. Click on Tools and search for Attach Debugger to Android Process. You should see a list of running processes, including your Flutter app.
  1. Select your application:
  • From the list of running processes, select your Flutter application. The debugger will attach to your app, allowing you to debug Kotlin code. Set breakpoints in Kotlin code:

  • Navigate to the Kotlin files you want to debug (e.g., MainActivity.kt or any other files handling platform-specific logic like method channels). Set breakpoints where you need to inspect the behavior or data. Interact with your app as needed to trigger the Kotlin code.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.