Thursday 8 February 2018

Know RRO (Runtime Resource Overlay) in Android AOSP

Hello Guys hope you all doing well.  !!!!
Today I am going to discuss a very important android resource framework.
Run-time Resource Overlay as it name suggest, it is resource overlay and it can be applied at run-time. I may guess not many of you know about it and google never advertise this AOSP resource framework. Android operating system has had this theming framework (RRO) built into AOSP for a few years now. With the help of RRO, we can quickly create themes that can change the look and feel of almost every app that's installed on an Android device.
As for example: - system user interface components, including navigation bar, status bar, notifications tray, and quick settings tray, themselves belong to an app, you can change their looks too. And the best part about it is that you don't have to write any Java code whatsoever for this RRO. 

What's the catch, you ask? Well, this framework(RRO) can be used only by privileged apps i.e. apps that are installed in a location that can be accessed only by the root user, or by someone who's (like us 😎) creating a custom ROM.
RRO work Mechanisms
  • Android app's business logic is written in Java or Kotlin and its user interface is primarily created using XML files. 
  • A well-written Android app will have separate XML resources that define its layouts, colors, strings, and attributes.
  • The RRO framework, as its name suggests, lets us overlay those XML resources with your own custom XML resources. 
  • It's not limited to just XML resources, It also allows you to change an app's drawables and fonts.
RRO Project Contain
  • An app that uses the RRO framework usually doesn't contain any Java code. 
  • It is composed only of XML files and, if necessary, fonts and images.
  • Like all Android apps, it must be a valid and signed APK.
Customize the Build with RRO
By using RRO or say resource overlays, we can customize android os product at build time. So that same same Android OS behave differently in different device. Resource overlays specify resource files that are applied on top of the defaults.  To use resource overlays, we have to modify the project buildfile to set PRODUCT_PACKAGE_OVERLAYS to a path relative to our top-level directory. The most commonly customized settings are contained in the file frameworks/base/core/res/res/config.xml. To set up a resource overlay on this file, add the overlay directory to the project buildfile, as follows:

PRODUCT_PACKAGE_OVERLAYS := device/DEVICE_IMPLEMENTER/DEVICE_NAME/overlay
or
PRODUCT_PACKAGE_OVERLAYS := vendor/VENDOR_NAME/overlay

Then, add an overlay file to the directory, for example:

vendor/foobar/overlay/frameworks/base/core/res/res/config.xml

Any strings or string arrays found in the overlay config.xml file replace those found in the original file.
Build a Product with overlay
for organizing the source files for our device(custom ROM), generally we have to deal with makesfile as for example 
  • device.mk
  • device-vendor.mk
  • AndroidProducts.mk
  • BoardConfig.mk and 
  • vendorsetup.sh
For more detail we can take Android Developer site as reference.
Example project to modify Quick setting tray

Step 1. Create a new project
I am using Linux(ubuntu) system and CLI is my favorite . So run the following command to create a new project 
mkdir MyOverlays && cd MyOverlays
touch AndroidManifest.xml

  • manifest file must contain the package name of our application
  • we are creating overlays for quick setting, so we must use com.android.systemui as the target package name

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.tutsplus.myoverlays">
    <overlay android:targetPackage="com.android.systemui"
             android:priority="1"/>
</manifest>
Step 2. Create a Theme
Actually we are creating a overlay for existing quick setting. Here is some point to keep in mind:-
  • To overlay a resource of the target application, our application must have a resource with the same name. 
  • To change the colors of the target application, we have to overlay its colors.xml file with our own colors.xml file.
  • We can keep only those details that we want to change
  • In this example we want to change the background color of the quick settings tray from the default blue-grey to a deep orange.
  • The value of the color is specified in the res/values/colors.xml file of the system UI app
  • you can take a look at the file on the official Android Git repository or androidxref
To change the color, we must now create a res/values/colors.xml file in our project.

mkdir -p res/values
touch res/values/colors.xml

Inside the colors.xml file, to change the background color of the tray, we must target a color named system_primary_color. Therefore, add the following XML to the file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="system_primary_color">#FFBF360C</color>
                                <!-- deep orange 900 -->
</resources>
Our simple theme is ready! If you want to, you can add more <color> tags to the file to change other colors of the system UI.
Step 3. Generate an APK
This is the last step, in this step we have to create .apk file. Android Studio automatically create .apk file for your application. But for our command line project we use AAPT(Android Asset Packaging Tool) which is a part of the Android SDK.
aapt package -M AndroidManifest.xml -S res/ \
    -I ~/Android/Sdk/platforms/android-23/android.jar \
    -F myoverlays.apk.u
In the above command, you can see that I've chosen to name the APK file myoverlays.apk.u.  That's because our APK is currently both unsigned and unaligned.
To sign our generated .apk, We use the jarsigner tool. For now, we are signing it with the default Android debug keystore. But for custom ROM development we use platform key for signing.
jarsigner -keystore ~/.android/debug.keystore \
        myoverlays.apk.u androiddebugkey
Finally, we must align—and thus optimize—the APK using the zipalign tool. As inputs, the tool expects the names of the unaligned and aligned APKs, along with a number that specifies the alignment boundaries. As of 2017, the number can be nothing else but 4.
zipalign 4 myoverlays.apk.u myoverlays.apk
Step 4. Install the APK
  • To install an APK that uses the RRO framework, we must simply place it inside the /system/vendor/overlay directory. 
  • The directory, by default, belongs to a read-only filesystem and is only accessible to the root user. 
  • If you are a custom ROM developer(😎), or have rooted your Android device, you should have no trouble installing the APK.
however, I'll be showing you how to install the APK on an emulator. Start an emulator in the writable filesystem mode using the emulator command-line tool.
emulator -avd Nexus_5X_API_23 -writable-system
adb tool to gain root privileges on the emulator
adb root
adb remount
The /system/vendor/overlay directory doesn't exist on the emulator. We must create it manually
adb shell
mkdir -p /system/vendor/overlay
exit
Finally, push the APK to the directory using adb
adb push myoverlays.apk /system/vendor/overlay
Wait for a few seconds for Android to detect and install the APK, and then restart the emulator for the theme to take effect. After the restart, if you pull down the quick settings tray, you should be able to see the theme in action.This is the way we can create a new resource overlay or edit existing one. 


 Thanks
Saurabh 
Happy Coding!!!!!

24 comments:


  1. If you’re looking to leverage mobile technology for your business, our best app developers are at your service. We provide iOS Mobile App Development Services India and Android Mobile App Development Services India, so you can reach your customers on their favorite devices.

    ReplyDelete
  2. Very good post. Thank you for sharing with us.
    bigo live pc

    ReplyDelete
  3. Thanks for sharing with us, This article gives more useful information to me.
    Software development company
    Software development company in india

    ReplyDelete
  4. This is Pretty Good information.Universal Garbage Collection log analyzer that parses any format of Garbage collection logs and generates WOW graphs & AHA metrics. Inbuilt intelligence has ability to discover any sort of memory problems.Excellence & Simplicity Devops tools for cloud. Gceasy

    ReplyDelete
  5. This post will be very useful to us. I like your blog and helpful to me. Nice thoughts for your great work.

    Android application Development in Mohali
    Android application Development in Chandigarh

    ReplyDelete
  6. Hello!! It's a posibility to apply an overlay to multiple apks?

    ReplyDelete
  7. Thanks for sharing this blog.This article gives lot of information.
    Full Stack Training in Ameerpet

    ReplyDelete
  8. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!
    mac productivity app

    ReplyDelete
  9. The best services of Website designing company in Noida & NCR. One of the best website developer companies with a affordable price. Website Designing Company in Noida

    ReplyDelete
  10. If you are searching for an expert Math tutor in Delhi NCR especially for a CBSE math tutor in NCR for your child, for more details visit here

    ReplyDelete
  11. The best Skin Specialist in Noida. Dermanext Skin & Hair Clinic is a boutique clinic specialising in dermatology, trichology and aesthetics. For more details call: 8860523726

    ReplyDelete
  12. Best Body Piercings in Delhi. There are so many types of tattoo like Body Tattoo, Back Tattoo, Wrist Tattoo, Full Body Tattoo, Tattoo for Men, Tattoo for Women in Delhi Tattoo gives a different look to your body. Lots of design of tattoo in the market. For more details call: 8745801112 or visit Which is the best tattoo shop nearby me

    ReplyDelete
  13. We are providing one of the best Color Printer on Hire in Delhi. For more Details call: 9999924096 or visit

    ReplyDelete
  14. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing. Buy instagram followers India

    ReplyDelete
  15. As a trusted partner with technology industry leaders, DataDukan offers state-of-the-art Business Intelligence & Analytics solutions which cater to both the business and IT infrastructure needs of
    our clients. Our certified consultants strategically build a BI roadmap by deploying solutions which include platform testing and rolling them out.
    As the Best Business Analytics Agencies in Mumbai , we leverage a tested methodology for Data Warehousing & Analytics Solutions which enables enterprises to generate an
    enterprise-wide central repository for their critical data and allows the KPI to be in alignment with their business needs.Our data visualization experts are trained in the deployment of data visualization tools
    such as Power BI, SSRS, Tableau, etc. Data Dukan AI Platform for the enterprise blends AI, technology, data and analytics to serve as the intelligence layer for forward-thinking companies.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Hey!
    What an awesome post! This is so chock full of useful information I can’t wait to dig deep and start utilizing the resources u have given me. Your exuberance is refreshing.
    Thank you so much.
    Afluex Multiservices LLP
    Web Development
    Software Development

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...