Open dorgold opened 7 years ago
Yeah same issue for me also. What is the quick work ardound for this to give update.
For quick fix, create build-extras.gradle with following def minSdkVersion = 21 cdvMinSdkVersion = minSdkVersion ext.cdvMinSdkVersion = minSdkVersion
Copy this file to /platforms/android/ folder. You can either do it manually or can write a hook
This is highly critical and should be resolved.
Here is a hook that fixes the problem:
Add to config.xml:
<platform name="android">
<hook src="scripts/afterAddBrowserTabPlugin.js" type="after_plugin_add" />
</platform>
create file: cordova/scripts/afterAddBrowserTabPlugin.js
const fs = require('fs');
module.exports = function(ctx) {
var Q = ctx.requireCordovaModule('q');
var deferral = new Q.defer();
if (ctx.opts.plugins.includes('cordova-plugin-browsertab')) {
let file = ctx.opts.projectRoot + '/plugins/cordova-plugin-browsertab/src/android/BrowserTab.gradle';
console.log(file);
checkForFix(file);
}
return deferral.promise;
};
function checkForFix(file) {
fs.readFile(file, function read(err, data) {
if (err) {
throw err;
}
if (!data.includes('minSdkFix')) {
writeFix(file);
}
});
}
function writeFix(file) {
fs.appendFile(file, '\n\n// minSdkFix\nminSdkVersion = 21;\ncdvMinSdkVersion = minSdkVersion;\n' +
'ext.cdvMinSdkVersion = minSdkVersion;', function(err) {
if (err) {
console.log('Adding minSdkFix failed: ' + err);
}
console.log("Writing success!");
});
}
I have some issues with ctx.requireCordovaModule('q')
and using Ionic commands, so I find next solution based on @Icety code:
Add next line in config.xml in <platform name="android">
section:
<hook src="hooks/browserTabPluginMinSdkVersionFixHook.js" type="after_plugin_add" />
Create hooks folder in project root and add browserTabPluginMinSdkVersionFixHook.js with next code:
const fs = require('fs');
module.exports = function (ctx) { console.log('[BrowserTabPluginMinSdkVersionFixHook] Activated');
let isBrowserTabPlugin = ctx.opts.plugins.findIndex(pluginName => pluginName.includes('cordova-plugin-browsertab')) !== -1;
if (isBrowserTabPlugin) {
console.log('[BrowserTabPluginMinSdkVersionFixHook] Plugin detected');
let browserTabGradleFilePath = ctx.opts.projectRoot + '/plugins/cordova-plugin-browsertab/src/android/BrowserTab.gradle';
fixBrowserTabGradleFile(browserTabGradleFilePath);
} else {
console.log('[BrowserTabPluginMinSdkVersionFixHook] Plugin not detected');
}
console.log('[BrowserTabPluginMinSdkVersionFixHook] Dectivated');
};
function fixBrowserTabGradleFile(browserTabGradleFilePath) { fs.readFile(browserTabGradleFilePath, function read(error, fileDataBuffer) { if (error) { console.log('[BrowserTabPluginMinSdkVersionFixHook] Failed to read file'); throw error; }
let fileAsString = fileDataBuffer.toString();
const isFixNeeded = fileAsString.includes('def minSdkVersion = 16');
if (!isFixNeeded) {
console.log('[BrowserTabPluginMinSdkVersionFixHook] Fix already applied');
return;
}
fileAsString = fileAsString.replace('def minSdkVersion = 16', 'def minSdkVersion = 19');
fs.writeFileSync(browserTabGradleFilePath, fileAsString);
console.log('[BrowserTabPluginMinSdkVersionFixHook] Fix applied');
});
}
3. Run `cordova plugin remove cordova-plugin-browsertab`
4. Run `cordova plugin add cordova-plugin-browsertab`
5. Run `cordova platform remove android`
6. Run `cordova platform add android`
The above solutions did not work for me so I wrote a little script and added it to my package.json
:
scripts: {
"monkey-patch-cordova-plugin-browsertab": "sed -i 's/def minSdkVersion = 16/def minSdkVersion = 19/g' ./platforms/android/cordova-plugin-browsertab/safepace-BrowserTab.gradle"
}
I run npm run monkey-patch-cordova-plugin-browsertab
just before cordova run android
in another package.json
script.
This plugin causes cordova to disregard the minSdk version set in config.xml.
For example, if i have the following line in my config.xml:
<preference name="android-minSdkVersion" value="21" />
This plugin causes cordova to build my android app with version 16 as the target (and not 21 as i asked).