pub struct Resources { /* private fields */ }Expand description
An application’s resources: values, files, and XML documents, each defined for one or more configurations.
IDs are assigned when compiling, by type and then by name, so adding a
resource can change the IDs of others. Like aapt2, the compiler adds the
minimum API level a configuration’s qualifiers imply (night is stored
as night-v8).
use apk::res::{Config, Density, Resources, ResourceType};
let mut resources = Resources::new();
resources.string("app_name", "Postcard")?;
resources.localized_string("app_name", "fr".parse()?, "Carte postale")?;
let png = vec![0x89, b'P', b'N', b'G'];
resources.file(ResourceType::Mipmap, "ic_launcher", Config::for_density(Density::XHDPI), "png", png)?;Implementations§
Source§impl Resources
impl Resources
pub fn new() -> Self
pub fn is_empty(&self) -> bool
Sourcepub fn contains(&self, ty: ResourceType, name: &str) -> bool
pub fn contains(&self, ty: ResourceType, name: &str) -> bool
Whether a resource with this type and name exists in any configuration.
Sourcepub fn contains_variant(
&self,
ty: ResourceType,
name: &str,
config: Config,
) -> bool
pub fn contains_variant( &self, ty: ResourceType, name: &str, config: Config, ) -> bool
Whether a resource with this type and name is defined for config.
Sourcepub fn value(
&mut self,
ty: ResourceType,
name: &str,
config: Config,
value: impl Into<Value>,
) -> Result<&mut Self, ResourceError>
pub fn value( &mut self, ty: ResourceType, name: &str, config: Config, value: impl Into<Value>, ) -> Result<&mut Self, ResourceError>
Adds a simple value: a string, bool, integer, color, dimension, fraction, or a reference to another resource.
Sourcepub fn string(
&mut self,
name: &str,
value: impl Into<String>,
) -> Result<&mut Self, ResourceError>
pub fn string( &mut self, name: &str, value: impl Into<String>, ) -> Result<&mut Self, ResourceError>
Adds the default value of string resource name.
Sourcepub fn localized_string(
&mut self,
name: &str,
locale: Locale,
value: impl Into<String>,
) -> Result<&mut Self, ResourceError>
pub fn localized_string( &mut self, name: &str, locale: Locale, value: impl Into<String>, ) -> Result<&mut Self, ResourceError>
Adds a translation of string resource name.
Sourcepub fn file(
&mut self,
ty: ResourceType,
name: &str,
config: Config,
extension: &str,
content: impl Into<Content>,
) -> Result<&mut Self, ResourceError>
pub fn file( &mut self, ty: ResourceType, name: &str, config: Config, extension: &str, content: impl Into<Content>, ) -> Result<&mut Self, ResourceError>
Adds a file stored as is, at res/<type>[-<qualifiers>]/<name>.<extension>.
XML (except in raw files) must be added with xml,
which compiles it.
Sourcepub fn xml(
&mut self,
ty: ResourceType,
name: &str,
config: Config,
document: Document,
) -> Result<&mut Self, ResourceError>
pub fn xml( &mut self, ty: ResourceType, name: &str, config: Config, document: Document, ) -> Result<&mut Self, ResourceError>
Adds an XML file resource, such as an adaptive icon, a color state list, or a network security config, compiled to binary XML. Its references may name other resources in this set.
Sourcepub fn compile(
&self,
package: &PackageName,
min_sdk: ApiLevel,
) -> Result<CompiledResources<'_>, ResourceError>
pub fn compile( &self, package: &PackageName, min_sdk: ApiLevel, ) -> Result<CompiledResources<'_>, ResourceError>
Assigns IDs and writes the resource table for package, for an app
supporting min_sdk onwards.
Like aapt2, version qualifiers the minimum SDK already satisfies are
dropped from the table (night-v8 becomes night); when that makes
variants collide, the one with the highest version is kept.