Skip to content

Basic Usage

This guide covers the fundamental operations with Node-APK.

Loading an APK

typescript
import { Apk } from "node-apk";
import { promises as fs } from "node:fs";

// From file path
const apk = new Apk("/path/to/app.apk");

// From Buffer
const buffer = await fs.readFile("/path/to/app.apk");
const apk = new Apk(buffer);

Getting Package Information

typescript
const manifest = await apk.getManifestInfo();

console.log("Package:", manifest.package);
console.log("Version:", manifest.versionName);
console.log("Version Code:", manifest.versionCode);

Listing Permissions

typescript
const manifest = await apk.getManifestInfo();

console.log("Required Permissions:");
for (const permission of manifest.permissions) {
  console.log(`  - ${permission}`);
}

// Convert to array for counting
const permissions = [...manifest.permissions];
console.log(`Total permissions: ${permissions.length}`);

Getting App Name

typescript
// Simple - get default label
const label = await apk.getLabel();
console.log(`App Name: ${label}`);

// With locale preference
const frenchLabel = await apk.getLabel({ locale: "fr" });
console.log(`Nom français: ${frenchLabel}`);

Extracting the Launcher Icon

typescript
// Get best available icon
const icon = await apk.getLauncherIcon();
await fs.writeFile("icon.png", icon);

// Get specific density
const hdpiIcon = await apk.getLauncherIcon({ density: "hdpi" });
await fs.writeFile("icon-hdpi.png", hdpiIcon);

Getting Certificate Information

typescript
const certs = await apk.getCertificateInfo();

for (const cert of certs) {
  console.log("Certificate:");
  console.log(`  Subject: ${cert.subject.get("CN")}`);
  console.log(`  Issuer: ${cert.issuer.get("CN")}`);
  console.log(`  Valid until: ${cert.validUntil.toLocaleDateString()}`);
  
  // Check if expired
  if (cert.validUntil < new Date()) {
    console.warn("  ⚠️ Certificate has expired!");
  }
}

Extracting Files

typescript
// Extract any file from the APK
const manifestXml = await apk.extract("AndroidManifest.xml");
console.log(`Manifest size: ${manifestXml.length} bytes`);

// Extract assets
const config = await apk.extract("assets/config.json");
const data = JSON.parse(config.toString("utf8"));
console.log("Config:", data);

// Extract native libraries
const lib = await apk.extract("lib/arm64-v8a/libnative.so");
console.log(`Native lib size: ${lib.length} bytes`);

Error Handling

typescript
import { Apk } from "node-apk";

try {
  const apk = new Apk("app.apk");
  const manifest = await apk.getManifestInfo();
  console.log("Package:", manifest.package);
} catch (error) {
  if (error instanceof Error) {
    if (error.message.includes("Entry not found")) {
      console.error("File not found in APK");
    } else if (error.message.includes("Invalid")) {
      console.error("Invalid APK format");
    } else {
      console.error("Error:", error.message);
    }
  }
}

Complete Example

typescript
import { Apk } from "node-apk";
import { promises as fs } from "node:fs";

async function analyzeApk(path: string) {
  console.log(`\n=== Analyzing ${path} ===\n`);
  
  const apk = new Apk(path);
  
  // Manifest info
  const manifest = await apk.getManifestInfo();
  console.log("Package Information:");
  console.log(`  Package: ${manifest.package}`);
  console.log(`  Version: ${manifest.versionName} (${manifest.versionCode})`);
  
  // App name
  const label = await apk.getLabel();
  console.log(`  App Name: ${label}`);
  
  // Permissions
  const permissions = [...manifest.permissions];
  console.log(`\nPermissions (${permissions.length}):`);
  for (const perm of permissions.slice(0, 5)) {
    console.log(`  - ${perm}`);
  }
  if (permissions.length > 5) {
    console.log(`  ... and ${permissions.length - 5} more`);
  }
  
  // Certificate
  const certs = await apk.getCertificateInfo();
  console.log(`\nCertificates (${certs.length}):`);
  for (const cert of certs) {
    console.log(`  Subject: ${cert.subject.get("CN")}`);
    console.log(`  Valid until: ${cert.validUntil.toLocaleDateString()}`);
  }
  
  // Icon
  const icon = await apk.getLauncherIcon();
  console.log(`\nIcon: ${icon.length} bytes`);
}

// Run
analyzeApk(process.argv[2]).catch(console.error);

Next Steps

Released under the MIT License.