Add custom fonts

You can now use runtime resource overlays (RROs) to change fonts. By adding custom fonts to make a font package, you can use fonts in themes and use RROs to apply the customizations.

Complete these tasks:

A sample file structure and sample files are provided in custom-fonts.zip. Download and then extract this file locally for your own use.

Make a font package

To make a font package:

  1. Add all the font ttf files to the font package in the vendor partition. For example:

    vendor/[oem]/fonts/[oem]-sans/

  2. Create the Android.mk file for the font package. For example:

    vendor/[oem]/fonts/[oem]-[fontname]/Android.mk

  3. Make sure the module will be installed in the product partition in the etc folder. To do so, confirm the module has the following flags:
    LOCAL_MODULE_CLASS := ETC
    LOCAL_PRODUCT_MODULE := true
    LOCAL_MODULE_PATH := $(TARGET_OUT_PRODUCT)/fonts
    

    The sample code provided above illustrates a complete definition of a font package module.

  4. Create a file named fonts.mk for the font package and then add the font ttf files to PRODUCT_PACKAGES. For example:

    vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk

    For example:

    PRODUCT_PACKAGES := \
    [font name].ttf \
    

Create a device overlay package

  1. Make a module called fonts_customization.xml to be added to the build. This module points to the fonts_customization.xml file that contains the font families and calls the make files of all the font packages. For example:

    vendor/[oem]/[device]_overlay/fonts/Android.mk

  2. Create the font families:

    vendor/[oem]/[device]_overlay/fonts/fonts_customization.xml

    The root level of the file must be a fonts-modification tag. Add font families for each of the font packages under this element. In addition, add customizationType="new-named-family" to every font family, and make sure each has a name attribute. This name is used later to access the font family. This file must be added to the fonts_customizations.xml module defined previously in Android.mk.

    The sample provided above contains a valid fonts_customization.xml file.

  3. Create fonts.mk. For example:

    vendor/[oem]/[device]_overlay/fonts/fonts.mk

  4. Add fonts_customization.xml under the PRODUCT_PACKAGES flag.
  5. Call into each of the previously created font packages.

    $(call inherit-product-if-exists, vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk)

  6. Add the font module to the build under PRODUCT_PACKAGES and then rebuild. The fonts will be installed on the system.
  7. Verify that the ttf fonts files are in the /product/fonts folder on the device.
  8. Verify that fonts_customization.xml is in /product/etc/ on the device.

Use the new system fonts family

To use the new system fonts family:

<style name="customstyle">
    <item name="android:fontFamily">customfontfamily</item>
</style>