[Miscellaneous Android Development] ameinild:
DISCLAIMER:
I am not responsible for any lost files, bricked devices, thermonuclear war,
zombie holocaust or your dog dropping dead after using these scripts.
You take full responsibility for your actions when using these scripts, and
consequently you should read the instructions, and gather enough knowledge
to know what these scripts are doing before running them on your device.

This is a series of scripts I call Android System Scripts for PowerShell, which I intend to expand upon over time. My goal is to provide some easy to use scripts to perform more or less trivial tasks that makes it easier to customize your Android system, and especially in relation to Android system upgrades on a custom ROM. (These scripts are completely ROM and device independent.)

I have chosen PowerShell because it’s a powerful scripting language that enables you to do most of the things you could in another .NET programming language (except I’m no programmer).

These scripts are made to easily replace some of your Android system files with alternate versions, primarily intended for use when upgrading your ROM. In this way you can easily further customize your ROM with alternate system APK’s, bootanimation.zip, or any other file you can think of. These scripts were primarily written out of my own need to replace Camera, MMS, Calendar and bootanimation.zip etc. in my custom ROM.

BEFORE USING THIS, please consider carefully which system files you want to replace !! Just know what you're doing, this is intended for experienced users only. This is why the script makes a backup of the old files so you can roll back. (For instance, I replaced the browser.apk with another version, which resulted in force closes, and I had to roll back to the previous version - be prepared to do so!)

Prerequisites

You must have ADB root access on your device.

A custom recovery is highly recommended.

Windows PowerShell 2.0 or higher.

Installing and preparing PowerShell

Most importantly, you need PowerShell 2.0 or higher - if you haven't already got it, download it from Microsoft here: (PowerShell is installed by default from Windows 7 and onwards)

http://support.microsoft.com/kb/968929

To use this script you also need to have the proper ADB executable present on your system, and you need to enable PowerShell to run scripts (which is disabled by default).

First, choose the folder in which you wish to install this script. This could very well be the same folder where your ADB executable files are, and it also has to be this folder where you later put the APK files to be installed.

I have installed mine into this folder: C:\Unified_Android_ToolKit\ADK tools

So if you haven't already, go grab a copy of ADB/Fastboot executables (although I don't use fastboot for anything yet), you can get them here:

http://xdaforums.com/showthread.php?p=42407269

Install them into the chosen folder, and unzip the attached file with the script, so that the file android-system-scripts.ps1 is in the same folder as adb.exe. You can add this folder to your path, if you want to be able to execute the commands from anywhere.

Ok, so now ADB and the script is installed, but PowerShell still needs to be enabled to run any scripts at all. This is done by setting the Execution Policy in PowerShell, so you need to open a PowerShell console (found in Start->All Programs->Accessories->Windows PowerShell).

In PowerShell, type: Get-ExecutionPolicy

This will show you the current policy, which would be Restricted if you haven't set anything else. Now you have to set the execution policy to Unrestricted, meaning that all scripts will run.

So type: Set-ExecutionPolicy Unrestricted

Now the script is ready to be run in PowerShell.

The System-File-Replacer script

This script contains a function called System-File-Replacer. When this function is run, the script looks for a file called ass-filetable.tbl in the script folder, and any files listed in the data file will be written to the system of the attached device. A backup will be provided, both on the devices and pulled to your PC, so you can manually roll back at any time (revert function not included yet). The data file have the following format where 3 parameters are given, and each filename has its own line:

Filename Folder Permissions
firstfile.apk /system/app 644
etc.

An example ass-filetable.tbl is included in the zip. You can also define your own data file name as a parameter to the System-File-Replacer function, if you wish to maintain different file tables, like this:

System-File-Replacer mydatafile

What's important to understand about PowerShell is that running the script file android-system-scripts.ps1 only loads the function System-File-Replacer into memory. Afterwards, the function needs to be called by name (or by its alias sysfilerepl) to actually perform the task.

It is recommended to run this script while in your custom recovery, because you should clear your caches after the new system filess are installed to ensure everything works correctly.

Here is a screenshot of my first run on my N7:

The screenshot shows 3 system files being replaced - or rather 1 installed and 2 replaced, as LockClock does not exist on the device (which is why the pull fails), while Apollo.apk and bootanimation.zip gets pulled and then replaced.

The System-App-Replacer script

This is a much simpler script that reads a list of APK's in the current directory, and then performs the same operation as the System-File-Replacer script. This is simpler if you only need to install system APK's, and don't want to list them in the data file.

Step-by-step guide

Start Windows PowerShell

Go to the folder where the files you want to install on your device (and other required files) are located

Make sure that ADB and this script is in the folder as well (or added to your path)

Connect your device

Boot your device into recovery (or if you just flashed a ROM, you're already in recovery)

Mount your system partition from recovery (Important!)

From PowerShell run:

. android-system-scripts.ps1

(Yes the above command is: dot space <filename>)

From PowerShell run:

sysfilerepl

Confirm the onscreen instructions

Clear cache and dalvik cache (if you use dalvik that is)

Reboot your device

Done!

Possible developments

I plan to expand upon this collection of scripts over time, and some possible future developments could be:

Bootanimation installer (provided with System-File-Replacer)

Install more file types (provided with System-File-Replacer)

Data files and directory for more devices (provided with System-File-Replacer)

Revert function

Update.zip maker

I'm also open to ideas and suggestions if you have any particular needs, and I'll see if it fits into the toolbox to be added in the future.

:: AM ::