Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pack-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2021"

[dependencies]
pack-api = { path = "../pack-api", features = ["cert-gen"] }
xml = "0.8.20"
72 changes: 72 additions & 0 deletions pack-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

use pack_api::{compile_and_sign_aab, compile_and_sign_apk, Keys, PackError, Package, Result};
use res_dir::read_res_dir;
use std::io::Cursor;
use std::path::PathBuf;
use std::{env, fs};
use xml::reader::{EventReader, XmlEvent};

pub mod res_dir;

const ANDROID_NAMESPACE: &str = "http://schemas.android.com/apk/res/android";
const MISSING_VERSION_CODE_WARNING: &str = "AndroidManifest.xml is missing android:versionCode \
on its <manifest> element. Android requires a version code for installable APKs.";

/// Run from a watch face directory to build signed APK and AAB files.
///
/// ```
Expand Down Expand Up @@ -70,6 +76,10 @@ fn pack_main() -> Result<()> {
let android_manifest = fs::read(&in_path)?;
in_path.pop();

for warning in critical_manifest_warnings(&android_manifest)? {
eprintln!("Warning: {warning}");
}

in_path.push("res");
let resources = read_res_dir(&in_path)?;
in_path.pop();
Expand All @@ -90,3 +100,65 @@ fn pack_main() -> Result<()> {

Ok(())
}

fn critical_manifest_warnings(manifest: &[u8]) -> Result<Vec<&'static str>> {
let parser = EventReader::new(Cursor::new(manifest));
for event in parser {
match event.map_err(PackError::XmlParsingFailed)? {
XmlEvent::StartElement {
name, attributes, ..
} if name.local_name == "manifest" => {
let has_version_code = attributes.iter().any(|attr| {
attr.name.local_name == "versionCode"
&& attr.name.namespace.as_deref() == Some(ANDROID_NAMESPACE)
});
return Ok(if has_version_code {
vec![]
} else {
vec![MISSING_VERSION_CODE_WARNING]
});
}
XmlEvent::StartElement { .. } => return Ok(vec![]),
_ => {}
}
}
Ok(vec![])
}

#[cfg(test)]
mod tests {
use super::*;

const MANIFEST_WITH_VERSION_CODE: &[u8] = br#"
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pack"
android:versionCode="1">
<application />
</manifest>
"#;

const MANIFEST_WITHOUT_VERSION_CODE: &[u8] = br#"
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pack">
<application />
</manifest>
"#;

#[test]
fn does_not_warn_when_version_code_is_present() {
assert_eq!(
critical_manifest_warnings(MANIFEST_WITH_VERSION_CODE).unwrap(),
Vec::<&'static str>::new()
);
}

#[test]
fn warns_when_version_code_is_missing() {
assert_eq!(
critical_manifest_warnings(MANIFEST_WITHOUT_VERSION_CODE).unwrap(),
vec![MISSING_VERSION_CODE_WARNING]
);
}
}