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 ?
-
Maybe add the source code so we can help youTamir Abutbul– Tamir Abutbul2019-03-04 15:41:39 +00:00Commented 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.Marcos Boaventura– Marcos Boaventura2019-03-04 15:44:54 +00:00Commented Mar 4, 2019 at 15:44
-
2When 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.Shruthi Alekha– Shruthi Alekha2019-03-04 15:46:36 +00:00Commented Mar 4, 2019 at 15:46
-
Then try File > Open > yourProjectName > android. In android studio IDE of course.Marcos Boaventura– Marcos Boaventura2019-03-04 15:49:16 +00:00Commented 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.Shruthi Alekha– Shruthi Alekha2019-03-04 15:53:41 +00:00Commented Mar 4, 2019 at 15:53
5 Answers
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.
Comments
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
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
If your flutter app works and you just want to use some breakpoints in Kotlin code you can use the Attach Debugger button
- 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.
- 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).
- 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.
- 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.
