shakalaca / MagiskOnEmulator

Install Magisk on Official Android Emulator
Apache License 2.0
672 stars 113 forks source link

[Working] Clearer rewrite of the guide #71

Open Smooth-E opened 5 months ago

Smooth-E commented 5 months ago

The documentation might be confusing. Here is a quick rewrite. Since the author of this repository ignores pull requests with documentation improvements I will place an issue with a rewrite here.

[!NOTE] Only Android 11 is supported. As of now I only succeeded to install Magisk on an emulator running Android 11 with Google APIs using this guide. Tried installing on Android 14 and Android 12 S, it did not work. Android 11 is enough for my development needs. If you manage to apply this guide with newer versions of Android, report here and I will update the post. @yafyz mentioned that you can use Magisk 25.2, but I did not test it.

  1. Clone this repository. The directory will be referred to as the root of cloned repository.
  2. Download Magisk 21.4. You will need Magisk-v21.4.zip. As this is the version of Magisk that is mentioned in the document, I decided to use it. I did not test newer versions.
  3. Rename downloaded file to magisk.zip amd put it into the root of cloned repository, next to the patch.bat file.
  4. Create a new virtual device using Android Studio. I used Google Pixel 7a running Android 11 R with Google APIs.
  5. Navigate to your system-images folder and then proceed to an according folder depending on what image you used to create the AVD. This directory will be later referred to as emulator directory.
    • You can check the location of your Android SDK in Android Studio: Settings -> Languages & Frameworks -> Android SDK. This view can also be opened with a gear icon on the top right, select "SDK Manager".
    • For our example with Android 11 (+ Google APIs), the default directory will be as follows:
      C:\Users\smooth-e\AppData\Local\Android\Sdk\system-images\android-30\google_apis\x86
  6. You will find an unpatched ramdisk.img file in the emulator directory. Create a backup of this file.
  7. Copy the unpatched ramdisk.img into the root of cloned repository.
  8. Right now the root of cloned repository should contain ramdisk.img, magisk.zip and the emulator should be running.
  9. Run patch.bat. The process will take some time, be patient and look through the logs.
  10. If you didn't see any [FAILED] throughout the process, assume everything went well.
  11. The ramdisk.img in the root of cloned repository is now patched.
  12. Copy ramdisk.img from the root of cloned repository to the emulator directory.
  13. Press the Stop (red square) button in Android Studio's Device Manager tab or close the emulator window to shut down the emulator.
  14. In Android Studio's Device Manager tab click the three-dots menu next to subject device and select Cold boot.
  15. Wait for some time. If you done everything correctly, the emulator will boot, but it will take a bit longer than usual.
  16. Done! You now have Magisk 21.4 on your Android 11 AVD.

[!IMPORTANT]

  • Ignore additional setup. Magisk may display a notification that it requires additional setup. Simply ignore it.
  • Do not update. Keep the old version of both the Magisk Manager app and Magisk itself.
  • Untested. I do not need any modules installed, I only need my app to have root access. Therefore I did not test anything besides that.
Daniel15 commented 1 month ago

I can confirm that Magisk 25.2 works, but none of the newer versions. I suspect something in process.sh is incompatible with newer versions. One final step that's missing (at least for Magisk 25.2) is that you need to open Magisk in the emulator, and click the "Install" button next to "Magisk"

I can confirm that modules work properly. I was able to use mitmproxy's Magisk module to install its root cert as a trusted certificate in order to MitM HTTPS requests (as documented at https://docs.mitmproxy.org/stable/howto-install-system-trusted-ca-android/).

Daniel15 commented 4 weeks ago

Alternatively, there's this repo: https://gitlab.com/newbit/rootAVD which supports Android 13 (API level 33) and Magisk v26.4.