Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin for android latest and add var in strings.xml #461

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions scripts/fcm_config_files_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ var PLATFORM = {
ANDROID_DIR + '/assets/www/google-services.json',
'www/google-services.json'
],
stringsXml: ANDROID_DIR + '/res/values/strings.xml'
stringsXml: [
ANDROID_DIR + '/res/values/strings.xml',
ANDROID_DIR + '/app/src/main/res/values/strings.xml'
]
}
};

Expand All @@ -57,24 +60,46 @@ if (directoryExists(ANDROID_DIR)) {

function updateStringsXml(contents) {
var json = JSON.parse(contents);
var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();
var strings
var path
try {
if (fileExists(PLATFORM.ANDROID.stringsXml[0])) {
path = PLATFORM.ANDROID.stringsXml[0]
strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml[0]).toString();
} else if (fileExists(PLATFORM.ANDROID.stringsXml[1])) {
path = PLATFORM.ANDROID.stringsXml[1]
strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml[1]).toString()
} else {
throw new Error
}

} catch (e) {
console.log('error in project, file not found')
}

const regexApp = new RegExp('<string name="google_app_id">([^\@<]+?)<\/string>', 'i')
const regexApi = new RegExp('<string name="google_api_key">([^\@<]+?)<\/string>', 'i')

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');
if (strings.match(regexApp)) {
strings = strings.replace(regexApp, '');
}

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');
if (strings.match(regexApi)) {
strings = strings.replace(regexApi, '');
}

// strip empty lines
strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');
strings = strings.replace(new RegExp('<resources>', 'i'), '<resources> \n \t<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');
strings = strings.replace(new RegExp('<resources>', 'i'), '<resources> \n \t<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');

fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
fs.writeFileSync(path, strings);
}

function copyKey(platform, callback) {
Expand Down