Ionic 4应用Android Billing Key配置问题:构建后内购失效求助
Hey there! Let’s work through this tricky issue where your Billing Key works fine in dev mode (ionic cordova run android -l) but vanishes when you do a release build. The root problem here is that the www folder gets cleared or overwritten during the release build process, so storing your Billing Key in www/manifest.json isn’t reliable long-term.
Here are a few solid alternative ways to configure your Billing Key that won’t break in release builds:
1. Add Billing Key Directly to config.xml
This is the simplest method, since the plugin natively supports reading the Billing Key from your Cordova config. Just open your project’s config.xml, find the <platform name="android"> section, and add this preference:
<platform name="android"> <!-- Other platform settings --> <preference name="BILLING_KEY" value="your_full_billing_key_string_here" /> </platform>
The plugin will automatically pick up this value during initialization, so you won’t need to rely on any files in the www directory anymore.
2. Manually Configure in Android Manifest (With a Catch)
If you prefer working directly with the native Android project, you can add the Billing Key to the AndroidManifest.xml:
- Open
platforms/android/app/src/main/AndroidManifest.xml - Inside the
<application>tag, add this meta-data entry:
<meta-data android:name="com.android.vending.BILLING_KEY" android:value="your_full_billing_key_string_here" />
Heads up: Running ionic cordova prepare android might overwrite this change later. To avoid that, pair this with a build hook (see next method) to auto-inject the key every time you prepare the platform.
3. Use a Build Hook to Auto-Inject the Billing Key
Build hooks let you automate changes to the native project so you don’t have to manually fix things after every prepare. Here’s how to set one up:
- Create a
hooks/after_preparefolder in your project root if it doesn’t exist. - Inside that folder, create a file named
010_inject_billing_key.jswith this code:
const fs = require('fs'); const path = require('path'); module.exports = function(ctx) { // Only run for Android platform if (!ctx.opts.platforms.includes('android')) return; const manifestPath = path.join(ctx.opts.projectRoot, 'platforms/android/app/src/main/AndroidManifest.xml'); const billingKey = 'your_full_billing_key_string_here'; let manifestContent = fs.readFileSync(manifestPath, 'utf8'); // Check if the key is already present to avoid duplicates if (!manifestContent.includes('com.android.vending.BILLING_KEY')) { // Insert the meta-data right before the closing </application> tag manifestContent = manifestContent.replace( '</application>', ` <meta-data android:name="com.android.vending.BILLING_KEY" android:value="${billingKey}" />\n</application>` ); fs.writeFileSync(manifestPath, manifestContent); console.log('Billing Key injected successfully into AndroidManifest.xml'); } };
- Give the script execution permissions (run this in your terminal):
chmod +x hooks/after_prepare/010_inject_billing_key.js
Now every time you run ionic cordova prepare android or ionic cordova build android, the hook will automatically add your Billing Key to the manifest if it’s missing.
4. Verify the Configuration
After setting up any of these methods, do a test release build and verify the key is present:
- You can unpack the APK (using a tool like Apktool) and check the
AndroidManifest.xmlfor the Billing Key entry. - Or add a quick debug log in your app to print the plugin’s initialized config, just to confirm the key is being read correctly.
内容的提问来源于stack exchange,提问作者lzeller

