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

Plugin overrides android minSdkVersion to 16 #17

Open
dorgold opened this issue Mar 29, 2017 · 6 comments
Open

Plugin overrides android minSdkVersion to 16 #17

dorgold opened this issue Mar 29, 2017 · 6 comments
Labels

Comments

@dorgold
Copy link

dorgold commented Mar 29, 2017

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).

@dorgold dorgold changed the title Plugin overrides min sdk version to 16 Plugin overrides android minSdkVersion to 16 Mar 29, 2017
@knvpk
Copy link

knvpk commented Jun 4, 2017

Yeah same issue for me also. What is the quick work ardound for this to give update.

@ashvinmay
Copy link

ashvinmay commented Nov 14, 2017

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

@ffMathy
Copy link

ffMathy commented Feb 5, 2019

This is highly critical and should be resolved.

@Icety
Copy link

Icety commented Feb 14, 2019

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!");
    });
} 

bobobobo added a commit to notnotse/cordova-plugin-browsertab that referenced this issue Mar 15, 2019
@illja96
Copy link

illja96 commented Jul 16, 2019

I have some issues with ctx.requireCordovaModule('q') and using Ionic commands, so I find next solution based on @Icety code:

  1. Add next line in config.xml in <platform name="android"> section:
    <hook src="hooks/browserTabPluginMinSdkVersionFixHook.js" type="after_plugin_add" />

  2. 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');
    });
}
  1. Run cordova plugin remove cordova-plugin-browsertab
  2. Run cordova plugin add cordova-plugin-browsertab
  3. Run cordova platform remove android
  4. Run cordova platform add android

@robsco-git
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants