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
ttf
files to the font package in thevendor
partition. For example:vendor/[oem]/fonts/[oem]-sans/
- Create the
Android.mk
file for the font package. For example:vendor/[oem]/fonts/[oem]-[fontname]/Android.mk
- Make sure the module will be installed in the
product
partition in theetc
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.
- Create a file named
fonts.mk
for the font package and then add the fontttf
files toPRODUCT_PACKAGES
. For example:vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk
For example:
PRODUCT_PACKAGES := \ [font name].ttf \
Create a device overlay package
- Make a module called
fonts_customization.xml
to be added to the build. This module points to thefonts_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
- 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, addcustomizationType="new-named-family"
to every font family, and make sure each has aname
attribute. This name is used later to access the font family. This file must be added to thefonts_customizations.xml
module defined previously inAndroid.mk
.The sample provided above contains a valid
fonts_customization.xml
file. - Create
fonts.mk
. For example:vendor/[oem]/[device]_overlay/fonts/fonts.mk
- Add
fonts_customization.xml
under thePRODUCT_PACKAGES
flag. - 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_PACKAGES
and then rebuild. The fonts will be installed on the system. - Verify that the
ttf
fonts files are in the/product/fonts
folder on the device. - 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>