How to write a script that removes a certain set of apps everytime you flash an update file?


Question

I run a LineageOS device with MicroG, but since the default messaging app is not able to be disabled I need to remove it everytime I flash an update file. This is becoming more and more tedious as more apps I need to remove on update increase.



How do you write a script that removes these apps whenever you flash so the removal changes persists? Something like addon.d but that is for backing up apps.



Edit : Both answers are correct, the one I posted and the one by Death Mask Salesman. I accepted the one by Death Mask Salesman because it is relevant to the question, however the one I posted solved my problem.


Answer

As an alternative to the other answer, I bring a more definitive approach, based on addon.d and targeted at system apps deletion.



Remember that, unlike the pm based method, which can be reversed at will, apps deleted with this approach can only be reobtained by commenting out the appropriate lines in the debloater script and reapplying the OTA update.






Introduction



Any shell script in the /system/addon.d directory is executed right after an OTA update has been applied. The order of execution depends on the integer at the beginning of a file's name, since scripts are evaluated in increasing order.






The code



The removal of a system app is simply a matter of issuing rm -rf on the app's parent directory. We can thus write a script so that these removals are carried out seamlessly after each update.



If, for example, we want to remove the stock Email app, our script will look like



#!/sbin/sh

rm -rf "/system/app/Email"


Here, the #!/sbin/sh is a mandatory line that instructs TWRP about which program will evaluate the script. Do not remove it.



rm -rf is a command used to forcibly remove whatever follows it in a recursive fashion. Thus, rm -rf "/system/app/Email" removes the /system/app/Email directory and everything inside it, thereby deleting the Email app altogether.



To add more apps to the list, simply append more rm -rf statements as per the example, replacing /system/app/Email with the path of the app you want to remove.






Finalizing



Once you're done writing the script, you'll need to copy it to /system/addon.d. In order to be executed, its name should begin with an integer, followed by a hyphen. For the sake of this answer, I'll call it 99-debloat.sh, which makes it be evaluated after the other addon scripts.



After that, you'll likely need to change the script's permissions and ownership. To change the permissions, use



chmod 755 /system/addon.d/99-debloat.sh


To alter the ownership, use



chown 0.0 /system/addon.d/99-debloat.sh





A full example



The method described in this answer is the one I myself use; I'll add my personal 99-debloat.sh script here for reference.



#!/sbin/sh

app="/system/app"
priv_app="/system/priv-app"

rm -rf $app/Calendar
rm -rf $app/Email
rm -rf $app/FM2
rm -rf $app/PicoTts
rm -rf $app/Stk
rm -rf $priv_app/FlipFlap
rm -rf $priv_app/Gallery2
rm -rf $priv_app/Snap
rm -rf $priv_app/WeatherProvider

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes