Skip to content

Commit

Permalink
Add cli option to replace google maps api key (#98)
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
PlessioTihsrah authored Apr 17, 2022
1 parent 816cd99 commit 8eb2c2d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type TaskOptions = {
outputPath: string
skipPatches: boolean
certificatePath?: string
mapsApiKey?: string
apktool: Apktool
uberApkSigner: UberApkSigner
tmpDir: string
Expand All @@ -39,7 +40,7 @@ const { version } = require('../package.json')

async function main() {
const args = parseArgs(process.argv.slice(2), {
string: ['apktool', 'certificate', 'tmp-dir'],
string: ['apktool', 'certificate', 'tmp-dir', 'maps-api-key'],
boolean: ['help', 'skip-patches', 'wait', 'debuggable', 'keep-tmp-dir'],
})

Expand Down Expand Up @@ -87,6 +88,7 @@ async function main() {

// Initialize and validate certificate path
let certificatePath: string | undefined
const mapsApiKey: string | undefined = args['maps-api-key']
if (args.certificate) {
certificatePath = path.resolve(process.cwd(), args.certificate)
let certificateExtension = path.extname(certificatePath)
Expand Down Expand Up @@ -120,6 +122,7 @@ async function main() {
inputPath,
outputPath,
certificatePath,
mapsApiKey,
tmpDir,
apktool,
uberApkSigner,
Expand Down Expand Up @@ -213,6 +216,7 @@ function showHelp() {
{dim {bold --skip-patches} Don't apply any patches (for troubleshooting)}
{dim {bold --apktool <path-to-jar>} Use custom version of Apktool}
{dim {bold --certificate <path-to-pem/der>} Add specific certificate to network security config}
{dim {bold --maps-api-key <api-key>} Add custom Google Maps API key to be replaced while patching apk}
`)
}

Expand Down
1 change: 1 addition & 0 deletions src/patch-apk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function patchApk(options: TaskOptions) {
applyPatches(decodeDir, {
debuggable: options.debuggable,
certificatePath: options.certificatePath,
mapsApiKey: options.mapsApiKey,
}),
},
{
Expand Down
8 changes: 7 additions & 1 deletion src/tasks/apply-patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export default function applyPatches(
{
debuggable = false,
certificatePath,
}: { debuggable?: boolean; certificatePath?: string } = {},
mapsApiKey,
}: {
debuggable?: boolean
certificatePath?: string
mapsApiKey?: string
} = {},
) {
return new Listr([
{
Expand All @@ -20,6 +25,7 @@ export default function applyPatches(
const result = await modifyManifest(
path.join(decodeDir, 'AndroidManifest.xml'),
debuggable,
mapsApiKey,
)

context.usesAppBundle = result.usesAppBundle
Expand Down
21 changes: 20 additions & 1 deletion src/tasks/modify-manifest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as fs from '../utils/fs'
import xml = require('xml-js')

export default async function modifyManifest(path: string, debuggable = false) {
export default async function modifyManifest(
path: string,
debuggable = false,
mapsApiKey = '',
) {
const document = xml.xml2js(await fs.readFile(path, 'utf-8')) as xml.Element

const manifest = document.elements?.find(el => el.name === 'manifest')!
Expand All @@ -24,6 +28,21 @@ export default async function modifyManifest(path: string, debuggable = false) {
el.attributes?.['android:name'] === 'com.android.vending.splits',
) ?? false

if (mapsApiKey) {
application.elements?.forEach(el => {
if (el.name === 'meta-data') {
const name = el.attributes?.['android:name'] + ''
const mapsApiName = [
'com.google.android.maps.v2.API_KEY',
'com.google.android.geo.API_KEY',
]
if (mapsApiName.includes(name)) {
el.attributes!['android:value'] = mapsApiKey
}
}
})
}

await fs.writeFile(path, xml.js2xml(document, { spaces: 4 }))

return { usesAppBundle }
Expand Down

0 comments on commit 8eb2c2d

Please sign in to comment.