UNPKG

appium-adb

Version:

Android Debug Bridge interface

124 lines 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readPackageManifest = readPackageManifest; const support_1 = require("@appium/support"); const teen_process_1 = require("teen_process"); const logger_1 = require("../../logger"); /** * Reads and parses Android manifest metadata from an APK via `aapt2`. * * @param apkPath - Local path to the APK file * @returns Parsed manifest data */ async function readPackageManifest(apkPath) { await this.initAapt2(); const aapt2Binary = this.binaries?.aapt2; if (!aapt2Binary) { throw new Error('aapt2 binary is not available'); } const args = ['dump', 'badging', apkPath]; logger_1.log.debug(`Reading package manifest: '${support_1.util.quote([aapt2Binary, ...args])}'`); let stdout; try { ({ stdout } = await (0, teen_process_1.exec)(aapt2Binary, args)); } catch (e) { const error = e; const prefix = `Cannot read the manifest from '${apkPath}'`; const suffix = `Original error: ${error.stderr || error.message}`; if (error.stderr && error.stderr.includes(`Unable to open 'badging'`)) { throw new Error(`${prefix}. Update build tools to use a newer aapt2 version. ${suffix}`, { cause: e, }); } throw new Error(`${prefix}. ${suffix}`, { cause: e }); } const extractValue = (line, propPattern, valueTransformer) => { const match = propPattern.exec(line); if (match) { return valueTransformer ? valueTransformer(match[1]) : match[1]; } return undefined; }; const extractArray = (line, propPattern, valueTransformer) => { let match; const resultArray = []; while ((match = propPattern.exec(line))) { resultArray.push(valueTransformer ? valueTransformer(match[1]) : match[1]); } return resultArray; }; const toInt = (x) => parseInt(x, 10); const result = { name: '', versionCode: 0, minSdkVersion: 0, compileSdkVersion: 0, usesPermissions: [], launchableActivity: { name: '', }, architectures: [], locales: [], densities: [], }; for (const line of stdout.split('\n')) { if (line.startsWith('package:')) { for (const [name, pattern, transformer] of [ ['name', /name='([^']+)'/, null], ['versionCode', /versionCode='([^']+)'/, toInt], ['versionName', /versionName='([^']+)'/, null], ['platformBuildVersionName', /platformBuildVersionName='([^']+)'/, null], ['platformBuildVersionCode', /platformBuildVersionCode='([^']+)'/, toInt], ['compileSdkVersion', /compileSdkVersion='([^']+)'/, toInt], ['compileSdkVersionCodename', /compileSdkVersionCodename='([^']+)'/, null], ]) { const value = extractValue(line, pattern, transformer); if (value !== undefined) { result[name] = value; } } } else if (line.startsWith('sdkVersion:') || line.startsWith('minSdkVersion:')) { const value = extractValue(line, /[sS]dkVersion:'([^']+)'/, toInt); if (value) { result.minSdkVersion = value; } } else if (line.startsWith('targetSdkVersion:')) { const value = extractValue(line, /targetSdkVersion:'([^']+)'/, toInt); if (value) { result.targetSdkVersion = value; } } else if (line.startsWith('uses-permission:')) { const value = extractValue(line, /name='([^']+)'/, null); if (value) { result.usesPermissions.push(value); } } else if (line.startsWith('launchable-activity:')) { for (const [name, pattern] of [ ['name', /name='([^']+)'/], ['label', /label='([^']+)'/], ['icon', /icon='([^']+)'/], ]) { const value = extractValue(line, pattern, null); if (value) { result.launchableActivity[name] = value; } } } else if (line.startsWith('locales:')) { result.locales = extractArray(line, /'([^']+)'/g, null); } else if (line.startsWith('native-code:')) { result.architectures = extractArray(line, /'([^']+)'/g, null); } else if (line.startsWith('densities:')) { result.densities = extractArray(line, /'([^']+)'/g, toInt); } } return result; } //# sourceMappingURL=manifest.js.map