Introduction

Android is one of the most popular mobile platforms for cell phones and tablets everywhere.

Since its release, Android handsets have constantly been "rooted", a process similar to the jailbreaking of iOS devices. The main idea behind rooting a device is to simply change how part of the device works to do more than one could before. Whether the reason to do this is to unlock a cell phone so that it can be used across various carriers, install a tethering application to act as a router, or load up a new ROM, rooting is a vital capability of the Android platform.

Background

Before we jump into writing applications that make use of "root" capability, I am going to assume two things:

  • The reader is sufficient in Android application development
  • A suitable IDE is available, such as Eclipse, with the Android SDK

Explaining su and root

For those familiar with Linux and UNIX, you've probably seen the "su" command. While it's used to switch between accounts, the one most users will switch to with su is the "root" account.

The root account is similar to Window's administrator, or SYSTEM account. It is full access to the hardware. It is unbound as far as software permissions go, which is why rooting a device allows one to do so much.

Rooting Your Device

In order to gain root access, the user must "root" his or her device. This might be done using something like Gingerbreak or SuperOneClick.

These applications work by exploiting a vulnerability in some Android system application or component, so that they may access root. Then, an application, "Superuser," is installed. By having Superuser installed, whenever an app requests root access, rather than having the request denied, the user is notified that an application wants root, and has the opportunity to decide to allow or deny that request.

Runtime Class

Now that you understand su, root, and how to root your device, we can write an app that requests root.

To take advantage of root, you must use the Runtime class to execute the "su" command.

Runtime runtime = Runtime.getRuntime();
try {
  runtime.exec("su");
}  catch (Exception e) {
  // Error occurred in executing command, do not have root
}

By-the-way: When no parameters are provided to "su," it's assumed the user wants root access.

I recommend trying the above code out on a live device. Build and export the APK to a live device, after tossing the code into an Activity class, like so:

A screenshot of the root source code in Eclipse

Congratulations, you've written your first app that uses root!

Real-world Examples

Let's explore the possibilities of using root access:

  • android-wifi-tether uses root to allow tethering of your Android device, which would allow it to act as an access point for a Wifi network, or to share its cellular wireless connection.
  • android_bootable_recovery, aka Clockwork Mod, allows some devices using the CyanogenMod ROM to be recovered should they be found unable to boot.
  • RootTools is a fantastic set of classes and interfaces that help developer write applications using root much more easily.

Where You Go From Here

While the developers that use root access on Android might seem scarce, that's simply not the case. Many applications with hundreds of thousands of downloads are in the Android Market that require root, such as SetCPU and Root Explorer.

Also, there's a booming community of support, such as on StackOverflow and xda-developers.

Conclusion

Rooting is one of many things that makes Android such a powerful platform, next to the fact that it's open source. As long as future versions of Android remain open source, the hope of this operating system lies not only in the hands of the companies that use it, but to the users themselves.

History

  • 28th August, 2011: Initial version
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"