diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a038950d..04955f5da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ * (iOS) Check if file contents for `pn-actions.json` exists before attempting to use it. * Resolves [#512](https://github.com/dpa99c/cordova-plugin-firebasex/issues/512). * Bug introduced by PR [#482](https://github.com/dpa99c/cordova-plugin-firebasex/pull/482). +* (Android) Add the Firebase Performance Monitoring Gradle plugin to monitor network traffic. + * Resolves [#520](https://github.com/dpa99c/cordova-plugin-firebasex/issues/520). # Version 11.0.1 * (iOS) Set the Sign In with Apple capability based on the `IOS_ENABLE_APPLE_SIGNIN` plugin variable. diff --git a/README.md b/README.md index ab9cf3678..f9b475679 100644 --- a/README.md +++ b/README.md @@ -213,11 +213,13 @@ The following plugin variables are used to specify the Firebase SDK versions as - `ANDROID_FIREBASE_CRASHLYTICS_VERSION` - `ANDROID_FIREBASE_CRASHLYTICS_NDK_VERSION` - `ANDROID_GSON_VERSION` +- `ANDROID_FIREBASE_PERF_GRADLE_PLUGIN_VERSION` See [Specifying Android library versions](#specifying-android-library-versions) for more info. - `ANDROID_ICON_ACCENT` - sets the default accent color for system notifications. See [Android Notification Color](#android-notification-color) for more info. - `ANDROID_FIREBASE_CONFIG_FILEPATH` - sets a custom filepath to `google-services.json` file as a path relative to the project root - e.g. `--variable ANDROID_FIREBASE_CONFIG_FILEPATH="resources/android/google-services.json"` +- `ANDROID_FIREBASE_PERF_GRADLE_PLUGIN_VERSION` - overrides the default version of the [Firebase Performance Monitoring Gradle plugin for Android](https://firebase.google.com/docs/perf-mon/get-started-android?authuser=0#add-perfmon-plugin) ### iOS only - `IOS_STRIP_DEBUG` - prevents symbolification of all libraries included via Cocoapods. See [Strip debug symbols](#strip-debug-symbols) for more info. diff --git a/plugin.xml b/plugin.xml index 4647cb4db..6a63e7dec 100644 --- a/plugin.xml +++ b/plugin.xml @@ -70,6 +70,7 @@ + diff --git a/scripts/after_prepare.js b/scripts/after_prepare.js index 3fae713ba..433a574e3 100644 --- a/scripts/after_prepare.js +++ b/scripts/after_prepare.js @@ -9,7 +9,7 @@ */ var fs = require('fs'); var path = require("path"); -var Utilities = require("./lib/utilities"); +var utilities = require("./lib/utilities"); var appName; var pluginVariables = {}; @@ -21,8 +21,8 @@ var PLUGIN_ID; var PLATFORM; var setupEnv = function(){ - appName = Utilities.getAppName(); - PLUGIN_ID = Utilities.getPluginId(); + appName = utilities.getAppName(); + PLUGIN_ID = utilities.getPluginId(); PLATFORM = { IOS: { dest: IOS_DIR + '/' + appName + '/Resources/GoogleService-Info.plist', @@ -44,8 +44,12 @@ var setupEnv = function(){ ANDROID_DIR + '/app/src/main/google-services.json' ], colorsXml: { - src: './plugins/' + Utilities.getPluginId() + '/src/android/colors.xml', + src: './plugins/' + utilities.getPluginId() + '/src/android/colors.xml', target: ANDROID_DIR + '/app/src/main/res/values/colors.xml' + }, + performanceGradlePlugin: { + classDef: 'com.google.firebase:perf-plugin', + pluginDef: 'com.google.firebase.firebase-perf' } } }; @@ -54,10 +58,10 @@ var setupEnv = function(){ module.exports = function(context){ //get platform from the context supplied by cordova var platforms = context.opts.platforms; - Utilities.setContext(context); + utilities.setContext(context); setupEnv(); - pluginVariables = Utilities.parsePluginVariables(); + pluginVariables = utilities.parsePluginVariables(); // set platform key path from plugin variable if(pluginVariables.ANDROID_FIREBASE_CONFIG_FILEPATH) PLATFORM.ANDROID.src = [pluginVariables.ANDROID_FIREBASE_CONFIG_FILEPATH]; @@ -65,15 +69,18 @@ module.exports = function(context){ // Copy key files to their platform specific folders - if(platforms.indexOf('android') !== -1 && Utilities.directoryExists(ANDROID_DIR)){ - Utilities.log('Preparing Firebase on Android'); - Utilities.copyKey(PLATFORM.ANDROID); + if(platforms.indexOf('android') !== -1 && utilities.directoryExists(ANDROID_DIR)){ + utilities.log('Preparing Firebase on Android'); + utilities.copyKey(PLATFORM.ANDROID); + var androidHelper = require("./lib/android"); + + // Apply colours if(!fs.existsSync(path.resolve(PLATFORM.ANDROID.colorsXml.target))){ fs.copyFileSync(path.resolve(PLATFORM.ANDROID.colorsXml.src), path.resolve(PLATFORM.ANDROID.colorsXml.target)); } - const $colorsXml = Utilities.parseXmlFileToJson(PLATFORM.ANDROID.colorsXml.target, {compact: true}); + const $colorsXml = utilities.parseXmlFileToJson(PLATFORM.ANDROID.colorsXml.target, {compact: true}); var accentColor = pluginVariables.ANDROID_ICON_ACCENT, $resources = $colorsXml.resources, existingAccent = false, @@ -113,14 +120,18 @@ module.exports = function(context){ } if(writeChanges){ - Utilities.writeJsonToXmlFile($colorsXml, PLATFORM.ANDROID.colorsXml.target); - Utilities.log('Updated colors.xml with accent color'); + utilities.writeJsonToXmlFile($colorsXml, PLATFORM.ANDROID.colorsXml.target); + utilities.log('Updated colors.xml with accent color'); } + + // Add Performance Monitoring gradle plugin for Android network traffic + androidHelper.addDependencyToRootGradle(PLATFORM.ANDROID.performanceGradlePlugin.classDef+":"+pluginVariables["ANDROID_FIREBASE_PERF_GRADLE_PLUGIN_VERSION"]); + androidHelper.applyPluginToAppGradle(PLATFORM.ANDROID.performanceGradlePlugin.pluginDef); } - if(platforms.indexOf('ios') !== -1 && Utilities.directoryExists(IOS_DIR)){ - Utilities.log('Preparing Firebase on iOS'); - Utilities.copyKey(PLATFORM.IOS); + if(platforms.indexOf('ios') !== -1 && utilities.directoryExists(IOS_DIR)){ + utilities.log('Preparing Firebase on iOS'); + utilities.copyKey(PLATFORM.IOS); var helper = require("./ios/helper"); var xcodeProjectPath = helper.getXcodeProjectPath(); diff --git a/scripts/lib/android.js b/scripts/lib/android.js new file mode 100644 index 000000000..d98c7bac5 --- /dev/null +++ b/scripts/lib/android.js @@ -0,0 +1,37 @@ +/** + * Android-specific utilities + */ +const fs = require('fs'); +const path = require('path'); +const utilities = require("./utilities"); + +const ANDROID_PROJECT_ROOT = 'platforms/android'; +const ROOT_GRADLE_FILEPATH = ANDROID_PROJECT_ROOT + '/build.gradle'; +const APP_GRADLE_FILEPATH = ANDROID_PROJECT_ROOT + '/app/build.gradle'; + +const gradleDependencyTemplate = "classpath '{artifactDef}'"; +const applyPluginTemplate = "apply plugin: '{pluginDef}'"; + +const Android = {}; + +Android.addDependencyToRootGradle = function(artifactDef){ + const gradleDependency = gradleDependencyTemplate.replace("{artifactDef}", artifactDef); + let rootGradle = fs.readFileSync(path.resolve(ROOT_GRADLE_FILEPATH)).toString(); + if(rootGradle.match(gradleDependency)) return; // already exists + + rootGradle = rootGradle.replace("dependencies {", "dependencies {\n"+gradleDependency); + fs.writeFileSync(path.resolve(ROOT_GRADLE_FILEPATH), rootGradle); + utilities.log("Added dependency to root gradle: " + artifactDef); +}; + +Android.applyPluginToAppGradle = function(pluginDef){ + const applyPlugin = applyPluginTemplate.replace("{pluginDef}", pluginDef); + let appGradle = fs.readFileSync(path.resolve(APP_GRADLE_FILEPATH)).toString(); + if(appGradle.match(applyPlugin)) return; // already exists + + appGradle += "\n"+applyPlugin; + fs.writeFileSync(path.resolve(APP_GRADLE_FILEPATH), appGradle); + utilities.log("Applied plugin to app gradle: " + pluginDef); +}; + +module.exports = Android;