Can Android Manifest programmatically update at runtime?

What I want to achieve:

I am creating an app that changes the app icon and app name programmatically.

What I have achieved:

 I have already achieved this because there are only 5 names and icons, by declaring the activity alias in the android manifest.

The problem I am facing:

If you only have 5 names, then it is easy to declare it in the manifest. But what if you don't know, no. names and you must update the manifest programmatically.

Below is my code:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.drawerstack">

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity-alias android:label="@string/app_name1"
            android:icon="@drawable/ic_launcher"
            android:name=".MainActivity-Red"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="@string/app_name2"
            android:icon="@drawable/ic_launcher_1"
            android:name=".MainActivity-Pink"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="@string/app_name3"
            android:icon="@drawable/ic_launcher_2"
            android:name=".MainActivity-Blue"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="@string/app_name4"
            android:icon="@drawable/ic_launcher_3"
            android:name=".MainActivity-Grey"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
    </application>
</manifest>

      

In MainActivity.java

    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getTag().equals(0)) {
                changeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity");
                removeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity-Grey");
            } else if (view.getTag().equals(1)) {
                changeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity-Grey");
                removeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity");
            } else if (view.getTag().equals(2)) {
                changeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Grey");
                removeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity");
            } else if (view.getTag().equals(3)) {
                changeIcon("com.drawerstack.MainActivity-Grey");
                removeIcon("com.drawerstack.MainActivity-Pink");
                removeIcon("com.drawerstack.MainActivity-Red");
                removeIcon("com.drawerstack.MainActivity-Blue");
                removeIcon("com.drawerstack.MainActivity");
            }
        }
    });


private void changeIcon(final String pkgName) {

    doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Date now = new Date();
            if(now.after(afterDate)){
                timer.cancel();
                Common.dismissProgressDialog(MainActivity.this);
                finish();
            }
        }
    };
    timer.schedule(doAsynchronousTask, 0, 50000);

    Common.loadProgressDialog(MainActivity.this,false);
    getPackageManager().setComponentEnabledSetting(
            new ComponentName("com.drawerstack", pkgName),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}


private void removeIcon(String removePkg) {
        getPackageManager().setComponentEnabledSetting(
                new ComponentName("com.drawerstack", removePkg),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }

      

+5


source to share


1 answer


Assuming your question has a title:

Can Android Manifest be updated programmatically?

The answer is no , it cannot be done programmatically.



You can enable / disable the components declared in your manifest, but you cannot add or remove anything there because, as the name says, this is the manifest file that the system "reads" when you install your application. So you declare your app interface in this manifest, later you cannot change it unless you reinstall the app with a different manifest.

Let's assume the system has given you the ability to do this. Then you added some permission to your manifest dynamically, whereas the user did not approve it while they downloaded your app from the Play Store.

+5


source







All Articles