๐Ÿ› ๏ธ How to Add Toast & Dialogue Box to Flutter Apps via Smali ๐Ÿ“ฑ

Posted on Sep 21, 2024

If you’re familiar with modifying Android apps, you know that smali can be an excellent tool for tinkering with app behavior. Today, we’re diving into adding Toast messages and Dialogue Boxes in Flutter apps by modifying the MainActivity.smali file. You may have encountered crashes when trying to do this on your own, but donโ€™t worry โ€” Iโ€™ll guide you through the correct way to do it manually.

Although there are external tools like Cloud Injection that can help with this, doing it manually offers more control. Let’s explore how you can manually add a Toast message and different types of Dialogue Boxes, including ones with buttons, in Flutter apps!


๐Ÿ“œ Step-by-Step Smali Code Modifications

In your <path/to/package/name>/MainActivity.smali file, you need to add the following methods based on what you want to display. Below, Iโ€™ll explain how to add different elements such as Toast, Dialogue Box with a title, Dialogue Box with a message, and Dialogue Box with two buttons.

โ˜• 1. Adding a Toast

A Toast is a simple pop-up message that appears on the screen for a short duration. Here’s how you can implement it:

.method protected onCreate(Landroid/os/Bundle;)V
    .registers 3

    invoke-super {p0, p1}, Lio/flutter/embedding/android/f;->onCreate(Landroid/os/Bundle;)V

    const-string p1, "Mod by Abhi"

    const/4 v0, 0x0

    invoke-static {p0, p1, v0}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;

    move-result-object p1

    invoke-virtual {p1}, Landroid/widget/Toast;->show()V

    return-void
.end method

๐Ÿ—ฃ๏ธ 2. Adding a Dialogue Box with Title

You can display a dialogue box with a title by adding the following code:

.method protected onCreate(Landroid/os/Bundle;)V
    .registers 5

    invoke-super {p0, p1}, Lio/flutter/embedding/android/f;->onCreate(Landroid/os/Bundle;)V

    new-instance v0, Landroid/app/AlertDialog$Builder;

    invoke-direct {v0, p0}, Landroid/app/AlertDialog$Builder;-><init>(Landroid/content/Context;)V

    const-string v1, "Mod by Abhi"

    invoke-virtual {v0, v1}, Landroid/app/AlertDialog$Builder;->setMessage(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;

    const-string v2, "OK"

    invoke-virtual {v0, v2, 0x0}, Landroid/app/AlertDialog$Builder;->setPositiveButton(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;

    invoke-virtual {v0}, Landroid/app/AlertDialog$Builder;->create()Landroid/app/AlertDialog;
    move-result-object v0

    invoke-virtual {v0}, Landroid/app/AlertDialog;->show()V

    return-void
.end method

๐Ÿ“ 3. Adding Dialogue with Title and Message

To display a Dialogue Box with both a title and a message, add this code:

.method protected onCreate(Landroid/os/Bundle;)V
    .registers 5

    invoke-super {p0, p1}, Lio/flutter/embedding/android/f;->onCreate(Landroid/os/Bundle;)V

    new-instance v0, Landroid/app/AlertDialog$Builder;

    invoke-direct {v0, p0}, Landroid/app/AlertDialog$Builder;-><init>(Landroid/content/Context;)V

    const-string v1, "Mod by Abhi"

    invoke-virtual {v0, v1}, Landroid/app/AlertDialog$Builder;->setTitle(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;

    const-string v2, "Thank you for using my mods"

    invoke-virtual {v0, v2}, Landroid/app/AlertDialog$Builder;->setMessage(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;

    const-string v3, "OK"

    invoke-virtual {v0, v3, 0x0}, Landroid/app/AlertDialog$Builder;->setPositiveButton(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;

    invoke-virtual {v0}, Landroid/app/AlertDialog$Builder;->create()Landroid/app/AlertDialog;
    move-result-object v0

    invoke-virtual {v0}, Landroid/app/AlertDialog;->show()V

    return-void
.end method

๐Ÿ”˜ 4. Adding Dialogue with Two Buttons

You can add a Dialogue Box with two buttons (e.g., OK and Cancel) using the following smali code:

.method protected onCreate(Landroid/os/Bundle;)V
    .registers 7

    invoke-super {p0, p1}, Lio/flutter/embedding/android/f;->onCreate(Landroid/os/Bundle;)V

    new-instance v0, Landroid/app/AlertDialog$Builder;

    invoke-direct {v0, p0}, Landroid/app/AlertDialog$Builder;-><init>(Landroid/content/Context;)V

    const-string v1, "Mod by Abhi"

    invoke-virtual {v0, v1}, Landroid/app/AlertDialog$Builder;->setTitle(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;

    const-string v2, "Thank you for using my mods"

    invoke-virtual {v0, v2}, Landroid/app/AlertDialog$Builder;->setMessage(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;

    const-string v3, "OK"

    const/4 v4, 0x0

    invoke-virtual {v0, v3, v4}, Landroid/app/AlertDialog$Builder;->setPositiveButton(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;

    const-string p0, "Cancel"

    const/4 p1, 0x0

    invoke-virtual {v0, p0, p1}, Landroid/app/AlertDialog$Builder;->setNegativeButton(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;

    invoke-virtual {v0}, Landroid/app/AlertDialog$Builder;->create()Landroid/app/AlertDialog;

    move-result-object v0

    invoke-virtual {v0}, Landroid/app/AlertDialog;->show()V

    return-void
.end method

๐Ÿ“ Important Note: Package Names

One critical thing to note is that the package name Lio/flutter/embedding/android/f; used in the above code snippets might be different in your app. You can find the correct package name by looking for the .super tag in your MainActivity.smali file and replacing the package name accordingly.


๐Ÿ”ง Troubleshooting Common Issues

  • Crashes on App Launch: If your app crashes after these modifications, check for any package name mismatch. Also, ensure the smali syntax is accurate, as even a small error can cause a crash.
  • Package Confusion: Always double-check the package path youโ€™re using, as it can differ based on your appโ€™s structure and setup.

Happy Modding! ๐Ÿ˜‰


๐Ÿ“ Additional Notes

  • You can further customize and modify the Toast messages and Dialogue Boxes as per your preferences. Feel free to experiment and add different types of UI elements with your skills!
  • To protect your smali code changes, you can use dex2c. This tool can convert dex bytecode into C code, making reverse engineering harder. Apply the following filter to protect the MainActivity modifications:
      com.example.myapp.MainActivity;onCreate\(.*
    
    Make sure to replace com.example.myapp.MainActivity with the path of your app’s actual MainActivity.
  • All the modifications provided in this guide have been carefully tested on devices A8, A9, A13, and A14. However, please double-check your implementation before reporting any issues, as the device environment and setup can vary.

๐ŸŽฅ Video Tutorial

If you’d prefer a visual guide, Iโ€™ve created a short video that walks you through the process of adding Toast messages and Dialogue Boxes in Flutter apps using smali. Watch the video below for instructions:


๐Ÿ“š References

  • Java2Smali mod by leyzymoy: This mod helped generate the smali code snippets used in this guide. You can find more about it here.
  • Flutter: Official Flutter documentation can be found here.
  • Project IDX by Google: Used for generating test apps.