Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d5ebb6e

Browse files
committedOct 10, 2023
gradle: Embed assets in baseAssets "Play Asset Delivery" pack
Assets for AABs could be embedded in the application pack but have significant limitations and size constraints. Implement the most simple form of an asset pack that comprises all assets listed in `manifest.yaml` in an `install-time` pack that is a fixed dependency of the app. This makes the files available to `AAssetManager` as soon as the app is started while being exempt of the 150MB limit. This matches the existing "asset embedding" functionality provided by our native APK build. https://developer.android.com/guide/playcore/asset-delivery/integrate-native
1 parent f58cf19 commit d5ebb6e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
 

‎xbuild/src/gradle/mod.rs

+56
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
6767
dependencies.push_str(&format!("implementation '{}'\n", dep));
6868
}
6969

70+
let asset_packs = if config.assets.is_empty() {
71+
""
72+
} else {
73+
r#"assetPacks = [":baseAssets"]"#
74+
};
75+
7076
let app_build_gradle = format!(
7177
r#"
7278
plugins {{
@@ -83,6 +89,7 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
8389
versionCode {version_code}
8490
versionName '{version_name}'
8591
}}
92+
{asset_packs}
8693
}}
8794
dependencies {{
8895
{dependencies}
@@ -96,6 +103,55 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
96103
dependencies = dependencies,
97104
);
98105

106+
let pack_name = "baseAssets";
107+
let base_assets = gradle.join(pack_name);
108+
// Make sure that any possibly-obsolete asset pack does not clobber the build
109+
let _ = std::fs::remove_dir_all(&base_assets);
110+
111+
if !config.assets.is_empty() {
112+
std::fs::create_dir_all(&base_assets)?;
113+
let assets = format!(
114+
r#"
115+
plugins {{
116+
id 'com.android.asset-pack'
117+
}}
118+
assetPack {{
119+
packName = "{pack_name}" // Directory name for the asset pack
120+
dynamicDelivery {{
121+
// Use install-time to make assets available to AAssetManager
122+
// https://developer.android.com/guide/playcore/asset-delivery/integrate-native
123+
deliveryType = "install-time"
124+
}}
125+
}}
126+
"#,
127+
);
128+
129+
std::fs::write(base_assets.join("build.gradle"), assets)?;
130+
131+
let target_dir = base_assets.join("src/main/assets");
132+
let _ = std::fs::remove_dir_all(&target_dir);
133+
std::fs::create_dir_all(&target_dir)?;
134+
for asset in &config.assets {
135+
let path = env.cargo().package_root().join(asset.path());
136+
let target = target_dir.join(asset.path().file_name().unwrap());
137+
138+
if !asset.optional() || path.exists() {
139+
// Make this file or directory available to the `gradle` build system
140+
141+
// Windows has special functions for files and directories:
142+
// https://doc.rust-lang.org/std/fs/fn.soft_link.html
143+
#[cfg(windows)]
144+
if path.is_dir() {
145+
std::os::windows::fs::symlink_dir(path, target)?;
146+
} else {
147+
std::os::windows::fs::symlink_file(path, target)?;
148+
}
149+
#[cfg(unix)]
150+
std::os::unix::fs::symlink(path, target)?;
151+
}
152+
}
153+
}
154+
99155
if let Some(icon_path) = env.icon.as_ref() {
100156
let mut scaler = xcommon::Scaler::open(icon_path)?;
101157
scaler.optimize();

‎xbuild/src/gradle/settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ dependencyResolutionManagement {
1414
}
1515

1616
include ':app'
17+
include ':baseAssets'

0 commit comments

Comments
 (0)
Please sign in to comment.