Skip to content

Instantly share code, notes, and snippets.

@codinronan
Last active February 19, 2018 21:12
Show Gist options
  • Save codinronan/24e93bb7a8717e13279a43882ab5c4f2 to your computer and use it in GitHub Desktop.
Save codinronan/24e93bb7a8717e13279a43882ab5c4f2 to your computer and use it in GitHub Desktop.
cordova-plugin-braintree-gradle-fix.js
#!/usr/bin/env node
// Define hook in your config.xml inside the android platform node:
// <platform name="android">
// <hook src="scripts/gradle-fix.js" type="before_prepare" />
// <hook src="scripts/gradle-fix.js" type="after_prepare" />
// </platform>
// The purpose of this script is to resolve the android.support version conflicts in cordova plugins
const fs = require('fs');
const path = require('path');
// no clue why design is being hardcoded to this version, but we pick this one since it is the lowest required,
// as recommended by the Android docs:
const SUPPORT_PLT_VERSION = '26.0.0';
const buildGradleResolver = `
configurations.all {
resolutionStrategy.eachDependency { details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '${SUPPORT_PLT_VERSION}'
}
}
}
}
`;
module.exports = (ctx) => {
console.log('-----------------------------');
console.log(`> Gradle - fix 'android.support' versions to ${SUPPORT_PLT_VERSION}`);
const Q = ctx.requireCordovaModule('q');
const deferred = Q.defer();
const androidPlatform = ctx.cordova.platforms.android;
const versionInt = parseInt(androidPlatform.version.replace(/[^0-9\.]/g, ''));
console.log('[gradle-fix]==> Found cordova-android v' + versionInt);
if (versionInt < 7) {
return deferred.reject('The gradle-fix only works for cordova-android@>=7.0.0. Your version is ' + androidPlatform.version);
}
// console.log('gms-gradle-fix got context: ', require('util').inspect(ctx, false, Infinity, false));
const gradle = path.join(ctx.opts.projectRoot, 'platforms', 'android', 'app', 'build.gradle');
console.log(gradle);
fs.readFile(gradle, 'utf8', (err, data) => {
if (err) {
console.log(err);
return deferred.reject(err);
}
let result = data;
if (result.indexOf(buildGradleResolver) < 0) {
result = `${data}\n${buildGradleResolver}`;
}
return fs.writeFile(gradle, result, 'utf8', (err) => {
if (err) {
console.log('error');
console.log('-----------------------------');
deferred.reject(err);
return;
}
console.log('completed');
console.log('-----------------------------');
deferred.resolve();
});
});
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment