Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gradle: Embed assets in baseAssets "Play Asset Delivery" pack #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion xbuild/src/gradle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cargo::CrateType;
use crate::{task, BuildEnv, Format, Opt};
use anyhow::Result;
use anyhow::{Context, Result};
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -67,6 +67,12 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
dependencies.push_str(&format!("implementation '{}'\n", dep));
}

let asset_packs = if config.assets.is_empty() {
""
} else {
r#"assetPacks = [":baseAssets"]"#
};

let app_build_gradle = format!(
r#"
plugins {{
Expand All @@ -83,6 +89,7 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
versionCode {version_code}
versionName '{version_name}'
}}
{asset_packs}
}}
dependencies {{
{dependencies}
Expand All @@ -96,6 +103,51 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
dependencies = dependencies,
);

let pack_name = "baseAssets";
let base_assets = gradle.join(pack_name);
// Make sure that any possibly-obsolete asset pack does not clobber the build
let _ = std::fs::remove_dir_all(&base_assets);

if !config.assets.is_empty() {
std::fs::create_dir_all(&base_assets)?;
let assets = format!(
r#"
plugins {{
id 'com.android.asset-pack'
}}
assetPack {{
packName = "{pack_name}" // Directory name for the asset pack
dynamicDelivery {{
// Use install-time to make assets available to AAssetManager
// https://developer.android.com/guide/playcore/asset-delivery/integrate-native
deliveryType = "install-time"
}}
}}
"#,
);

std::fs::write(base_assets.join("build.gradle"), assets)?;

let target_dir = base_assets.join("src/main/assets");
let _ = std::fs::remove_dir_all(&target_dir);
std::fs::create_dir_all(&target_dir)?;
for asset in &config.assets {
let path = env.cargo().package_root().join(asset.path());
let target = target_dir.join(asset.path().file_name().unwrap());

if !asset.optional() || path.exists() {
// Make this file or directory available to the `gradle` build system
xcommon::symlink(&path, &target).with_context(|| {
format!(
"Failed to make asset file/folder `{}` available to gradle at `{}`",
path.display(),
target.display()
)
})?;
}
}
}

if let Some(icon_path) = env.icon.as_ref() {
let mut scaler = xcommon::Scaler::open(icon_path)?;
scaler.optimize();
Expand Down
1 change: 1 addition & 0 deletions xbuild/src/gradle/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dependencyResolutionManagement {
}

include ':app'
include ':baseAssets'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this folder/file is optional, and I'm a tad confused that gradle doesn't complain when it doesn't exist.

The app would obviously complain when there's assetPacks = [":baseAssets"] to a pack that doesn't exist, obviously :)

Loading