|
| 1 | +import { execFileSync } from 'node:child_process'; |
1 | 2 | import fs from 'node:fs'; |
2 | 3 | import os from 'node:os'; |
3 | 4 | import path from 'node:path'; |
@@ -255,7 +256,7 @@ ${output |
255 | 256 |
|
256 | 257 | // Upload index.html for Android ad-hoc distribution |
257 | 258 | if (args.adHoc && isArtifactAPK) { |
258 | | - const { version, packageName } = await getManifestFromApk(binaryPath); |
| 259 | + const { version, packageName } = getManifestFromApk(binaryPath); |
259 | 260 | const { url: urlIndexHtml, getResponse: getResponseIndexHtml } = |
260 | 261 | await remoteBuildCache.upload({ |
261 | 262 | artifactName, |
@@ -348,20 +349,54 @@ async function getInfoPlistFromIpa(binaryPath: string) { |
348 | 349 | }; |
349 | 350 | } |
350 | 351 |
|
351 | | -async function getManifestFromApk(binaryPath: string) { |
| 352 | +function findAapt() { |
| 353 | + const sdkRoot = |
| 354 | + process.env['ANDROID_HOME'] || process.env['ANDROID_SDK_ROOT']; |
| 355 | + |
| 356 | + if (!sdkRoot) { |
| 357 | + throw new RockError( |
| 358 | + 'ANDROID_HOME or ANDROID_SDK_ROOT environment variable is not set. Please follow instructions at: https://reactnative.dev/docs/set-up-your-environment?platform=android', |
| 359 | + ); |
| 360 | + } |
| 361 | + |
| 362 | + const buildToolsPath = path.join(sdkRoot, 'build-tools'); |
| 363 | + const versions = fs.readdirSync(buildToolsPath); |
| 364 | + |
| 365 | + for (const version of versions) { |
| 366 | + const aaptPath = path.join(buildToolsPath, version, 'aapt'); |
| 367 | + if (fs.existsSync(aaptPath)) { |
| 368 | + logger.debug(`Found aapt at: ${aaptPath}`); |
| 369 | + return aaptPath; |
| 370 | + } |
| 371 | + } |
| 372 | + |
| 373 | + throw new RockError( |
| 374 | + `"aapt" not found in Android Build-Tools directory: ${colorLink(buildToolsPath)} |
| 375 | +Please follow instructions at: https://reactnative.dev/docs/set-up-your-environment?platform=android`, |
| 376 | + ); |
| 377 | +} |
| 378 | + |
| 379 | +function getManifestFromApk(binaryPath: string) { |
352 | 380 | const apkFileName = path.basename(binaryPath, '.apk'); |
353 | 381 |
|
354 | 382 | try { |
355 | | - const nodeApk = await import('node-apk'); |
356 | | - const { Apk } = nodeApk.default || nodeApk; |
357 | | - const apk = new Apk(binaryPath); |
358 | | - const manifest = await apk.getManifestInfo(); |
359 | | - apk.close(); |
| 383 | + const aaptPath = findAapt(); |
360 | 384 |
|
361 | | - return { |
362 | | - packageName: manifest.package || apkFileName, |
363 | | - version: manifest.versionName || '1.0', |
364 | | - }; |
| 385 | + const output = execFileSync(aaptPath, ['dump', 'badging', binaryPath], { |
| 386 | + encoding: 'utf8', |
| 387 | + }); |
| 388 | + |
| 389 | + const packageMatch = output?.match(/package: name='([^']+)'/); |
| 390 | + const versionMatch = output?.match(/versionName='([^']+)'/); |
| 391 | + |
| 392 | + const packageName = packageMatch?.[1] || apkFileName; |
| 393 | + const version = versionMatch?.[1] || '1.0'; |
| 394 | + |
| 395 | + logger.debug( |
| 396 | + `Extracted APK manifest - package: ${packageName}, version: ${version}`, |
| 397 | + ); |
| 398 | + |
| 399 | + return { packageName, version }; |
365 | 400 | } catch (error) { |
366 | 401 | logger.debug('Failed to parse APK manifest, using fallback', error); |
367 | 402 | return { |
|
0 commit comments