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:
- Add all the font
ttffiles to the font package in thevendorpartition. For example:vendor/[oem]/fonts/[oem]-sans/ - Create the
Android.mkfile for the font package. For example:vendor/[oem]/fonts/[oem]-[fontname]/Android.mk - Make sure the module will be installed in the
productpartition in theetcfolder. 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.
- Create a file named
fonts.mkfor the font package and then add the fontttffiles toPRODUCT_PACKAGES. For example:vendor/[oem]/fonts/[oem]-[fontname]/fonts.mkFor example:
PRODUCT_PACKAGES := \ [font name].ttf \
Create a device overlay package
- Make a module called
fonts_customization.xmlto be added to the build. This module points to thefonts_customization.xmlfile that contains the font families and calls the make files of all the font packages. For example:vendor/[oem]/[device]_overlay/fonts/Android.mk - Create the font families:
vendor/[oem]/[device]_overlay/fonts/fonts_customization.xmlThe root level of the file must be a
fonts-modificationtag. Add font families for each of the font packages under this element. In addition, addcustomizationType="new-named-family"to every font family, and make sure each has anameattribute. This name is used later to access the font family. This file must be added to thefonts_customizations.xmlmodule defined previously inAndroid.mk.The sample provided above contains a valid
fonts_customization.xmlfile. - Create
fonts.mk. For example:vendor/[oem]/[device]_overlay/fonts/fonts.mk - Add
fonts_customization.xmlunder thePRODUCT_PACKAGESflag. - Call into each of the previously created font packages.
$(call inherit-product-if-exists, vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk) - Add the font module to the build under
PRODUCT_PACKAGESand then rebuild. The fonts will be installed on the system. - Verify that the
ttffonts files are in the/product/fontsfolder on the device. - Verify that
fonts_customization.xmlis 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>