@smbcloud / smbcloud-ascapi Mirror · read-only / commits / 02c6b57
Fork 0

Read-only mirror of github.com/smbcloudXYZ/smbcloud-ascapi.

Mirrored from https://github.com/smbcloudXYZ/smbcloud-ascapi.git

Synced Nov 25, 09:15 UTC

Add asc play generated-apks to download Google-signed APKs

Stores other than Google Play (Xiaomi GetApps PSV, for one) want an APK signed with the same key as the Play install. With Play App Signing that key is Google's, so the APK has to come from Play. - asc play generated-apks list: generatedapks.list for a version code, one entry per signing certificate. - asc play generated-apks download-universal: downloads the universal APK, checks that it is a zip, writes it to --out (never overwriting), and prints its size, SHA-256 and the signing certificate. With more than one certificate, --certificate picks one. --dry-run lists and writes nothing. The download id is percent-encoded in the path because Play treats it as opaque. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Seto Elkahfi committed Nov 19, 2026 at 10:49 UTC 02c6b57f69411fd9255b1d0c558d323822c609d2
3 files changed +278
crates/cli/src/main.rs
+61
@@ -195,6 +195,12 @@ enum PlayCommand {
195 #[command(subcommand)]
196 command: PlayBundlesCommand,
197 },
198 + /// APKs Play generated from an uploaded bundle, including the universal
199 + /// APK signed with Play's app signing key that other stores ask for.
200 + GeneratedApks {
201 + #[command(subcommand)]
202 + command: PlayGeneratedApksCommand,
203 + },
204 /// One-time products (in-app products): list, read, create, update, activate, deactivate.
205 Products {
206 #[command(subcommand)]
@@ -748,6 +754,34 @@ enum PlayBundlesCommand {
754 },
755 }
756
757 +#[derive(Subcommand)]
758 +enum PlayGeneratedApksCommand {
759 + /// The generated APKs for a version code, one entry per signing
760 + /// certificate, as Play returns them.
761 + List {
762 + package: String,
763 + #[arg(long)]
764 + version_code: u64,
765 + },
766 + /// Download the universal APK for a version code. It is signed with the
767 + /// app signing key Play holds, not the upload key, which is what Xiaomi
768 + /// GetApps (PSV) and other stores ask for. The version code must be a
769 + /// bundle Play has processed; a draft release on internal testing is
770 + /// enough. Refuses to overwrite `--out`. With `--dry-run` it lists and
771 + /// writes nothing.
772 + DownloadUniversal {
773 + package: String,
774 + #[arg(long)]
775 + version_code: u64,
776 + /// Where to write the APK.
777 + #[arg(long)]
778 + out: PathBuf,
779 + /// SHA-256 of the signing certificate, when Play lists more than one.
780 + #[arg(long)]
781 + certificate: Option<String>,
782 + },
783 +}
784 +
785 #[derive(Subcommand)]
786 enum PlayProductsCommand {
787 /// Every one-time product of the app.
@@ -1887,6 +1921,33 @@ async fn run_play(
1921 print_json(&result)
1922 }
1923 },
1924 + PlayCommand::GeneratedApks { command } => match command {
1925 + PlayGeneratedApksCommand::List {
1926 + package,
1927 + version_code,
1928 + } => print_json(
1929 + &play::read_generated_apks(&client, &package, version_code)
1930 + .await
1931 + .map_err(fail)?,
1932 + ),
1933 + PlayGeneratedApksCommand::DownloadUniversal {
1934 + package,
1935 + version_code,
1936 + out,
1937 + certificate,
1938 + } => print_json(
1939 + &play::download_universal_apk(
1940 + &client,
1941 + &package,
1942 + version_code,
1943 + &out,
1944 + certificate.as_deref(),
1945 + dry_run,
1946 + )
1947 + .await
1948 + .map_err(fail)?,
1949 + ),
1950 + },
1951 PlayCommand::Products { command } => match command {
1952 PlayProductsCommand::List { package } => {
1953 print_json(&play::read_products(&client, &package).await.map_err(fail)?)
crates/frontend/src/play.rs
+143
@@ -611,6 +611,121 @@ pub async fn upload_bundle(
611 })
612 }
613
614 +/// The APKs Play generated for a version code, as Play returns them.
615 +pub async fn read_generated_apks(
616 + client: &PlayClient,
617 + package: &str,
618 + version_code: u64,
619 +) -> Result<serde_json::Value, String> {
620 + client
621 + .list_generated_apks(package, version_code)
622 + .await
623 + .map_err(err)
624 +}
625 +
626 +#[derive(Debug, Serialize)]
627 +#[serde(rename_all = "camelCase")]
628 +pub struct UniversalApk {
629 + pub dry_run: bool,
630 + pub version_code: u64,
631 + /// SHA-256 of the certificate the APK is signed with (Play's app signing
632 + /// key), as Play reports it.
633 + pub certificate_sha256: String,
634 + pub download_id: String,
635 + pub path: PathBuf,
636 + /// Size and SHA-256 of the written file; absent on a dry run.
637 + pub bytes: Option<usize>,
638 + pub sha256: Option<String>,
639 +}
640 +
641 +/// Pick the universal APK out of a `generatedapks.list` answer. With more
642 +/// than one signing certificate, `certificate` (a SHA-256, any case) must
643 +/// name one.
644 +fn pick_universal(
645 + list: &serde_json::Value,
646 + certificate: Option<&str>,
647 +) -> Result<(String, String), String> {
648 + let entries = list
649 + .get("generatedApks")
650 + .and_then(|v| v.as_array())
651 + .filter(|a| !a.is_empty())
652 + .ok_or("Play generated no APKs for this version code (is it a bundle Play processed?)")?;
653 + let candidates: Vec<(String, String)> = entries
654 + .iter()
655 + .filter_map(|e| {
656 + let cert = e.get("certificateSha256Hash")?.as_str()?.to_string();
657 + let id = e
658 + .pointer("/generatedUniversalApk/downloadId")?
659 + .as_str()?
660 + .to_string();
661 + Some((cert, id))
662 + })
663 + .collect();
664 + let chosen: Vec<_> = match certificate {
665 + Some(want) => candidates
666 + .into_iter()
667 + .filter(|(c, _)| c.eq_ignore_ascii_case(want))
668 + .collect(),
669 + None => candidates,
670 + };
671 + match chosen.len() {
672 + 0 => Err(match certificate {
673 + Some(want) => format!("no universal APK signed with certificate {want}"),
674 + None => "Play listed no universal APK for this version code".to_string(),
675 + }),
676 + 1 => Ok(chosen.into_iter().next().expect("one element")),
677 + n => Err(format!(
678 + "{n} signing certificates have a universal APK; pass --certificate with one of: {}",
679 + chosen
680 + .iter()
681 + .map(|(c, _)| c.as_str())
682 + .collect::<Vec<_>>()
683 + .join(", ")
684 + )),
685 + }
686 +}
687 +
688 +/// Download the universal APK Play generated and signed for `version_code`
689 +/// and write it to `out`. Refuses to overwrite a file. A dry run lists the
690 +/// APKs and writes nothing.
691 +pub async fn download_universal_apk(
692 + client: &PlayClient,
693 + package: &str,
694 + version_code: u64,
695 + out: &Path,
696 + certificate: Option<&str>,
697 + dry_run: bool,
698 +) -> Result<UniversalApk, String> {
699 + if out.exists() {
700 + return Err(format!("{} exists; not overwriting it", out.display()));
701 + }
702 + let list = read_generated_apks(client, package, version_code).await?;
703 + let (certificate_sha256, download_id) = pick_universal(&list, certificate)?;
704 + let mut result = UniversalApk {
705 + dry_run,
706 + version_code,
707 + certificate_sha256,
708 + download_id,
709 + path: out.to_path_buf(),
710 + bytes: None,
711 + sha256: None,
712 + };
713 + if dry_run {
714 + return Ok(result);
715 + }
716 + let bytes = client
717 + .download_generated_apk(package, version_code, &result.download_id)
718 + .await
719 + .map_err(err)?;
720 + if bytes.len() < 4 || &bytes[..4] != b"PK\x03\x04" {
721 + return Err("Play's download is not a zip, so not an APK; nothing was written".to_string());
722 + }
723 + std::fs::write(out, &bytes).map_err(|e| format!("cannot write {}: {e}", out.display()))?;
724 + result.sha256 = Some(format!("{:x}", Sha256::digest(&bytes)));
725 + result.bytes = Some(bytes.len());
726 + Ok(result)
727 +}
728 +
729 #[derive(Debug, Serialize)]
730 #[serde(rename_all = "camelCase")]
731 pub struct Promoted {
@@ -1082,6 +1197,34 @@ pub async fn update_product(
1197 mod tests {
1198 use super::*;
1199
1200 + #[test]
1201 + fn picks_the_only_universal_apk() {
1202 + let list = serde_json::json!({"generatedApks": [{
1203 + "certificateSha256Hash": "AB12",
1204 + "generatedUniversalApk": {"downloadId": "id-1"},
1205 + "generatedSplitApks": []
1206 + }]});
1207 + assert_eq!(
1208 + pick_universal(&list, None).unwrap(),
1209 + ("AB12".to_string(), "id-1".to_string())
1210 + );
1211 + assert_eq!(pick_universal(&list, Some("ab12")).unwrap().1, "id-1");
1212 + assert!(pick_universal(&list, Some("ffff")).is_err());
1213 + }
1214 +
1215 + #[test]
1216 + fn several_certificates_need_a_choice() {
1217 + let list = serde_json::json!({"generatedApks": [
1218 + {"certificateSha256Hash": "AA", "generatedUniversalApk": {"downloadId": "a"}},
1219 + {"certificateSha256Hash": "BB", "generatedUniversalApk": {"downloadId": "b"}}
1220 + ]});
1221 + assert!(pick_universal(&list, None)
1222 + .unwrap_err()
1223 + .contains("--certificate"));
1224 + assert_eq!(pick_universal(&list, Some("BB")).unwrap().1, "b");
1225 + assert!(pick_universal(&serde_json::json!({}), None).is_err());
1226 + }
1227 +
1228 #[test]
1229 fn image_types_parse_in_both_spellings() {
1230 assert_eq!(
crates/play/src/client.rs
+74
@@ -98,6 +98,32 @@ pub(crate) fn product_update_path(
98 path
99 }
100
101 +/// `GET` target for `generatedapks.list`: the APKs Play generated from the
102 +/// bundle with this version code, one entry per signing certificate.
103 +pub(crate) fn generated_apks_path(package: &str, version_code: u64) -> String {
104 + format!("/androidpublisher/v3/applications/{package}/generatedApks/{version_code}")
105 +}
106 +
107 +/// `GET` target for `generatedapks.download`, as media. The download id is
108 +/// percent-encoded: it is opaque and may hold `/`, `+` or `=`.
109 +pub(crate) fn generated_apk_download_path(
110 + package: &str,
111 + version_code: u64,
112 + download_id: &str,
113 +) -> String {
114 + let mut id = String::with_capacity(download_id.len());
115 + for b in download_id.bytes() {
116 + if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
117 + id.push(b as char);
118 + } else {
119 + id.push_str(&format!("%{b:02X}"));
120 + }
121 + }
122 + format!(
123 + "/androidpublisher/v3/applications/{package}/generatedApks/{version_code}/downloads/{id}:download?alt=media"
124 + )
125 +}
126 +
127 pub struct PlayClient {
128 http: reqwest::Client,
129 account: ServiceAccount,
@@ -348,6 +374,42 @@ impl PlayClient {
374 Ok(response.json().await?)
375 }
376
377 + /// The APKs Play generated for a version code, as Play returns them:
378 + /// `{"generatedApks": [{"certificateSha256Hash", "generatedUniversalApk":
379 + /// {"downloadId"}, "generatedSplitApks", …}]}`. Needs no edit.
380 + pub async fn list_generated_apks(
381 + &self,
382 + package: &str,
383 + version_code: u64,
384 + ) -> Result<serde_json::Value> {
385 + let text = self
386 + .send(
387 + Method::GET,
388 + &generated_apks_path(package, version_code),
389 + None,
390 + )
391 + .await?
392 + .text()
393 + .await?;
394 + if text.trim().is_empty() {
395 + return Ok(serde_json::json!({}));
396 + }
397 + Ok(serde_json::from_str(&text)?)
398 + }
399 +
400 + /// Download one generated APK. A universal APK is signed with the app
401 + /// signing key Play holds, which is what other stores ask for.
402 + pub async fn download_generated_apk(
403 + &self,
404 + package: &str,
405 + version_code: u64,
406 + download_id: &str,
407 + ) -> Result<Vec<u8>> {
408 + let path = generated_apk_download_path(package, version_code, download_id);
409 + let response = self.send(Method::GET, &path, None).await?;
410 + Ok(response.bytes().await?.to_vec())
411 + }
412 +
413 /// Attach a ProGuard/R8 `mapping.txt` to an uploaded version code.
414 pub async fn upload_deobfuscation(
415 &self,
@@ -558,6 +620,18 @@ impl PlayClient {
620 mod tests {
621 use super::*;
622
623 + #[test]
624 + fn generated_apk_paths() {
625 + assert_eq!(
626 + generated_apks_path("com.example.app", 2026111110),
627 + "/androidpublisher/v3/applications/com.example.app/generatedApks/2026111110"
628 + );
629 + assert_eq!(
630 + generated_apk_download_path("com.example.app", 7, "Ab+c/d=_-"),
631 + "/androidpublisher/v3/applications/com.example.app/generatedApks/7/downloads/Ab%2Bc%2Fd%3D_-:download?alt=media"
632 + );
633 + }
634 +
635 #[test]
636 fn edit_urls_follow_the_v3_layout() {
637 assert_eq!(