Skip to content

Commit 811a6d8

Browse files
authored
feat(docs): update docs about fonts + proguard rules (#1479)
1 parent 6af7cce commit 811a6d8

6 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎.changeset/calm-shrinkers-pass.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Ship consumer ProGuard rules for the Android library so release builds with R8 (`minifyEnabled true`) no longer fail with missing `com.google.errorprone.annotations` classes referenced by `nimbus-jose-jwt`.

‎packages/repack/android/build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ android {
108108
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
109109
versionCode 1
110110
versionName "1.0"
111+
consumerProguardFiles "consumer-rules.pro"
111112
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", IS_NEW_ARCHITECTURE_ENABLED.toString()
112113
externalNativeBuild {
113114
cmake {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Applied to apps that depend on Re.Pack when R8/ProGuard is enabled (minifyEnabled true).
2+
3+
# nimbus-jose-jwt (used by code signing) bundles a shaded Gson that references
4+
# Error Prone annotations. They are compile-time only and not on the runtime classpath.
5+
-dontwarn com.google.errorprone.annotations.**

‎website/src/latest/api/loaders/assets-loader.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,29 @@
33
The `AssetsLoader` processes image and other static assets (video, audio, etc.) in your React Native application. It handles asset extraction, copying files to the appropriate platform-specific output directories, and supports additional features like base64 inlining and conversion into remote assets.
44

55
:::info Platform-Specific Output
6-
By default, extracted asset files are copied to `assets/` directory for iOS and `drawable-*` directories (e.g. `drawable-mdpi`, `drawable-hdpi`, etc.) for Android which matches Metro's asset handling behavior.
6+
By default, extracted asset files are copied to `assets/` directory for iOS and to Android resource directories for Android, which matches Metro's asset handling behavior.
7+
:::
8+
9+
### Android resource directories
10+
11+
When bundling for Android without the development server, extracted assets are placed in the following resource directories:
12+
13+
| Asset | Output directory |
14+
| ---------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
15+
| Images (`png`, `jpg`, `gif`, `webp`) | `drawable-*` (e.g. `drawable-mdpi`, `drawable-hdpi`) per scale |
16+
| Font binaries (`ttf`, `otf`, `ttc`) | `font` **and** `raw` |
17+
| XML font families (`<font-family>`) | `font` |
18+
| Other XML files | `drawable-*` |
19+
| Everything else (other image formats such as `jpeg` or `svg`, video, audio, other files) | `raw` |
20+
21+
`xml` and `ttc` are not included in the default [asset extensions](/api/utils/constants#asset_extensions), so `getAssetTransformRules()` doesn't process them. To bundle them, add a rule that uses the Assets loader for these extensions.
22+
23+
Font binaries are emitted twice: the `font` copy keeps native references such as `@font/...` and `R.font` working, while the `raw` copy lets libraries that load fonts by name at runtime (e.g. [React Native Skia](https://shopify.github.io/react-native-skia/)) find them the same way they do with Metro.
24+
25+
Re.Pack also generates `raw/keep.xml` listing all bundled Android resources. Assets loaded by name from JavaScript are invisible to the resource shrinker, so this file keeps them in the APK when `shrinkResources` is enabled.
26+
27+
:::note
28+
Development server builds and [remote assets](/docs/guides/remote-assets) are not copied into Android resource directories, so the layout above and `keep.xml` don't apply to them.
729
:::
830

931
:::tip Guides related to AssetsLoader
@@ -12,6 +34,7 @@ Looking to do more with your assets? Check out the guides on:
1234
- [Inlining assets as base64 strings](/docs/guides/inline-assets)
1335
- [Converting to remote assets](/docs/guides/remote-assets)
1436
- [Adding SVG support](/docs/guides/svg)
37+
- [Using custom fonts](/docs/guides/fonts)
1538

1639
:::
1740

‎website/src/latest/docs/guides/_meta.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"inline-assets",
77
"remote-assets",
88
"svg",
9+
"fonts",
910
"deploy"
1011
]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Using custom fonts
2+
3+
Font files (`ttf` and `otf`) are processed by the [Assets loader](/api/loaders/assets-loader) like any other asset, so you can `require` or `import` them from JavaScript. How you load the font afterwards depends on whether it is used by a JavaScript library or by native `<Text>` components.
4+
5+
## Loading fonts from JavaScript
6+
7+
Libraries that load fonts at runtime from an asset reference, such as [React Native Skia](https://shopify.github.io/react-native-skia/), work without any additional configuration:
8+
9+
```jsx
10+
import { useFont } from '@shopify/react-native-skia';
11+
12+
const font = useFont(require('./assets/fonts/Inter.ttf'), 24);
13+
```
14+
15+
In development, the font is served by the development server. In release builds on Android, Re.Pack emits the font to both the `font` and `raw` resource directories and generates a `raw/keep.xml` file, so the font can be found by name and is kept when resource shrinking (`shrinkResources`) is enabled. See [Android resource directories](/api/loaders/assets-loader#android-resource-directories) for details.
16+
17+
## Using fonts in native `<Text>` components
18+
19+
Requiring a font file doesn't register it with the platform, so it can't be used with the `fontFamily` style yet. This is the same as with Metro. Use one of the following approaches.
20+
21+
### Link fonts natively (recommended)
22+
23+
Add the font files to the native projects so they are available in both debug and release builds:
24+
25+
- **Android**: place the files in `android/app/src/main/assets/fonts/`. The file name (without extension) becomes the `fontFamily` value.
26+
- **iOS**: add the files to your app target in Xcode and list them under `UIAppFonts` in `Info.plist`. The font's family name becomes the `fontFamily` value.
27+
28+
```jsx
29+
<Text style={{ fontFamily: 'Inter' }}>Hello</Text>
30+
```
31+
32+
### Register fonts emitted by Re.Pack (Android release builds)
33+
34+
In Android release builds, fonts bundled by Re.Pack are available as `font` resources. A font is only bundled if it is `require`d or `import`ed somewhere in your JavaScript code. The resource name comes from the font's path relative to the project root, not to the file that requires it (e.g. `assets/fonts/Inter.ttf` becomes `assets_fonts_inter`). You can register these fonts in `MainApplication` with `ReactFontManager`:
35+
36+
```js title="index.js"
37+
// makes Re.Pack bundle the font, even if it's only used by native <Text>
38+
require('./assets/fonts/Inter.ttf');
39+
```
40+
41+
```kotlin title="MainApplication.kt"
42+
import com.facebook.react.common.assets.ReactFontManager
43+
44+
override fun onCreate() {
45+
super.onCreate()
46+
loadReactNative(this)
47+
48+
val fontId = resources.getIdentifier("assets_fonts_inter", "font", packageName)
49+
if (fontId != 0) {
50+
ReactFontManager.getInstance().addCustomFont(this, "Inter", fontId)
51+
}
52+
}
53+
```
54+
55+
:::warning Release builds only
56+
When using the development server, fonts are served over HTTP and are not part of the Android resources, so the resource lookup above returns `0` and the font falls back to the system font. Link the font natively if you need it in debug builds as well.
57+
58+
The lookup also returns `0` in release builds if the font isn't required from JavaScript. Because of the `fontId != 0` check, this fails silently and the text uses the system font.
59+
:::
60+
61+
## Font collections (`.ttc`)
62+
63+
`ttc` files are not included in the default [asset extensions](/api/utils/constants#asset_extensions). To use them, add a rule that processes them with the Assets loader:
64+
65+
```js title="rspack.config.cjs"
66+
const Repack = require('@callstack/repack');
67+
68+
module.exports = {
69+
module: {
70+
rules: [
71+
...Repack.getAssetTransformRules(),
72+
{
73+
test: /\.ttc$/,
74+
use: '@callstack/repack/assets-loader',
75+
},
76+
],
77+
},
78+
};
79+
```
80+
81+
This is enough for libraries that load fonts from JavaScript, such as React Native Skia. Native `<Text>` on Android only looks up `.ttf` and `.otf` files in `assets/fonts/`, so a `.ttc` file can't be linked natively there. In Android release builds, you can still register the `font` resource emitted by Re.Pack with [`addCustomFont`](#register-fonts-emitted-by-repack-android-release-builds).

0 commit comments

Comments
 (0)