Install xbuild
masterInstall the xbuild CLI tool using cargo install.
cargo install xbuildrepository·master·Indexed 20 days ago
https://github.com/rust-mobile/xbuildA build tool for Rust projects designed to simplify cross-compiling and publishing native applications to major mobile and desktop stores. It includes a CLI for managing connected devices, diagnosing environment issues via `x doctor`, and a library for creating, signing, and verifying Android APKs, including manifest compilation and resource management.
Install the xbuild CLI tool using cargo install.
cargo install xbuildThe PRI decision model is composed of the following structures:
Decision: Represents a top-level decision, containing a list of indices to QualifierSet objects.QualifierSet: A collection of indices to Qualifier objects.Qualifier: The leaf node containing:qualifier_type: A QualifierType enum.priority: A u16 value.fallback_score: An f32 value.value: A String representing the qualifier's value.In xbuild, certain package fields (like version and description) use the Inheritable<T> type. This allows a package to either define its own value or inherit a value from the parent workspace.
An Inheritable field can be represented in TOML in two ways:
version = "0.1.0").version = { workspace = true }).The xbuild manifest parser follows the standard Cargo Cargo.toml structure, supporting both virtual workspaces and individual packages.
members (paths to package directories) and default_members. A workspace can also define a package section (of type WorkspacePackage) to provide root-level values like version and description.name. Fields like version and description use the Inheritable pattern.Package can either be a direct value or marked for inheritance from the workspace. This is represented by the Inheritable<T> type.| Section | Key | Type | Description |
|---|---|---|---|
[workspace] | members | Vec<String> | List of paths to workspace members. |
[workspace] | default_members | Vec<String> | List of default workspace members. |
[workspace] | package | WorkspacePackage | Root values for the workspace. |
[workspace].package | version | Option<String> | Workspace-level version. |
[workspace].package | description | Option<String> | Workspace-level description. |
[package] | name | String | The name of the package. |
[package] | version | Inheritable<String> | The package version (can be inherited). |
[package] | description | Option<Inheritable<String>> | The package description (can be inherited). |
When using x build, certain platform-specific constraints apply:
src/lib.rs file (it must be a library crate, not just a binary).android_gradle configuration. Direct packaging is not supported for AABs.xbuild will attempt to notarize the AppBundle or the resulting .dmg.Dmg, xbuild will create a disk image and can sign it if a signer is provided.AppBundle.Assets.car files via the configuration.Exe (simple binary copy) and Msix (packaged application). Other formats are unsupported.Assets in xbuild can be defined using two formats in the configuration:
path: The file path.optional: (Boolean) Whether the asset is optional.alignment: Controls how the file is stored in the zip/package. Options include:Aligned(n): Align the file to n bytes.Unaligned: Do not align.Compressed: Standard compression (default).assets:
- path: "assets/config.json"
- extended:
path: "assets/large_data.bin"
optional: true
alignment: 4When configuring environment variables in .cargo/config.toml, you can use the relative = true option within an extended object. This tells Cargo to resolve the provided value relative to the directory containing the .cargo/config.toml file.
This is useful for pointing to local assets, source directories, or toolchains that are part of your workspace structure without hardcoding absolute paths.
[env]
# If config.toml is in /project/.cargo/config.toml
# This will resolve to /project/assets/data.bin
ASSET_PATH = { value = "../assets/data.bin", relative = true }A Section in a PRI file contains metadata (flags, qualifiers) and a payload defined by the SectionData enum. When reading a PRI file, the library automatically identifies the section type based on its 16-byte identifier.
Supported SectionData variants:
DataItemPriDescriptorResourceMapDecisionInfoHierarchicalSchemaUnknown (used when the identifier does not match known types, preserving the raw bytes)This allows the library to be extensible; unknown sections are preserved during read/write cycles even if they cannot be parsed into structured types.
The BuildEnv struct is the primary orchestrator for an xbuild session. It encapsulates the Cargo configuration, the BuildTarget requirements, and paths to SDKs and build directories.
build_dir, cache_dir, output, and executable based on the target.android_sdk(), macos_sdk(), windows_sdk()).cargo_build method prepares a CargoBuild instance configured with the correct linker arguments, RPATHs, and SDK paths for the target platform.cargo_artefact to locate the resulting compiled files.impl BuildEnv {
pub fn new(args: BuildArgs) -> Result<Self>;
pub fn cargo_build(&self, target: CompileTarget, target_dir: &Path) -> Result<CargoBuild>;
pub fn output(&self) -> PathBuf;
pub fn executable(&self) -> PathBuf;
}The AppImage struct provides a programmatic interface to construct an AppImage bundle. The process involves initializing a new AppImage directory, populating it with required files (like .desktop entries, icons, and application binaries), and finally calling build to compress the directory into a squashfs image wrapped with the xbuild runtime.
Workflow:
AppImage::new(build_dir, name) to initialize the .AppDir structure.add_desktop(), add_icon(), and add_file() to populate the bundle.build(out_path, signer) to generate the final executable.Requirements:
mksquashfs command must be installed on the host system to perform the compression step.use std::path::Path;
use appimage::AppImage;
fn main() -> anyhow::Result<()> {
let build_dir = Path::new("./build");
let app_name = "my-app".to_string();
let output_path = Path::new("./my-app.AppImage");
// 1. Initialize
let appimage = AppImage::new(build_dir, app_name.clone())?;
// 2. Populate
appimage.add_desktop()?;
appimage.add_icon(Path::new("assets/icon.png"))?;
appimage.add_file(Path::new("target/release/my-app"), Path::new("my-app"))?;
appimage.add_apprun()?;
// 3. Build
appimage.build(output_path, None)?;
Ok(())
}The x build command automates the process of compiling Rust code for multiple platforms and packaging them into distributable formats (like APK, AppBundle, AppImage, MSIX, etc.).
Key behaviors:
--offline mode.cargo to build binaries or libraries (cdylib) for the specified target architectures.android_gradle flag to be set, as they must be built via gradle rather than direct packaging.lib.rs must exist) to create APKs or AABs.libc++_shared.so if needed) and bundles them into the package.AppImage by adding apprun, desktop files, icons, and the main binary/library.AppBundle (.app), handles signing, notarization (if an API key is provided), and can create .dmg files.AppBundle (.app) and packages it into an .ipa file, supporting provisioning profiles and Assets.car..exe (copying the binary) and .msix (packaging with manifest and icon).For iOS development, you must manage Apple signing certificates and mobile provisioning profiles: