Home » Android » android – Cordova – modify or remove MainActivity

android – Cordova – modify or remove MainActivity

Posted by: admin June 15, 2020 Leave a comment


I am developing a Cordova application which should run in a “Kiosk” mode – the device will be locked to that app and should not be able to exit.

To achieve this I am using a slightly modified version of cordova-plugin-kiosk, which provides an extra activity (KioskActivity) that is defined as a launcher (it has android.intent.category.HOME).

This works reasonably well. However, the app still has the original cordova MainActivity, which causes some confusion, especially as this is what gets launched by the icon in the original launcher, and by the cordova run android command. It also results in two entries in the Chrome remote inspector.

The AndroidManifest.xml looks like this at the moment:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|uiMode" android:label="@string/activity_name" android:launchMode="singleInstance" android:name="jk.cordova.plugin.kiosk.KioskActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="adjustResize">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.HOME" />

I would like to merge these into a single activity and do away with the confusion.

I can see 2 possible ways to do this:

  1. Find a way to remove the MainActivity section from the AndroidManifest.xml, and hopefully also to make cordova run android run KioskActivity instead.
  2. Find a way to modify MainActivity and move the code from KioskActivity into it.

However, I cannot find any sensible way to achieve either of these without causing more confusion by breaking all the cordova tools.

How to&Answers:

If you want to modify an activity’s entry in the Manifest.xml, you can do so from the plugin’s plugin.xml file.

<edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:name='MainActivity']" mode="overwrite">
<activity android:name="MainActivity" android:label="NewLabel" android:configChanges="orientation|keyboardHidden" />

With this,you can remove the MainActivity’s entry or change it so that it’s not longer the launcher activity for the app.



There are two possible solutions to this:

1. Use a hook to modify MainActivity

Use a hook to copy a custom MainActivity.java into platforms/android/src/[packageName]/ and override the default Cordova MainActivity.

For example, a before_build hook can be added to <platform name="android"> section of the config.xml like this:

<hook type="before_build" src="scripts/updateMainActivity.sh" />

Where scripts/updateMainActivity.sh is:

cp MainActivity.java platforms/android/src/[packageName]/

(You can also write hooks with nodeJS, which is good for cross-platform compatibility)

2. Use cordova-custom-config to remove MainActivity

With cordova-custom-config, all you need to do is add the following to the <platform name="android"> section of your config.xml:

<preference name="android-manifest/application/activity[@android:name='MainActivity']" delete="true" />

Note: You will need cordova-custom-config >= 3.0.0.

This is the solution I went with, since I am already using cordova-custom-config.