'Is it possible to update Android Manifest programmatically at runtime?
What I want to achieve:
I am creating an application which changes the app icon and app name programmatically.
What I have achieved:
I've already achieved this because there are only 5 names and icons by declaring activity-alias in android manifest.
Problem I'm facing:
If you have only 5 names then it's easy to declare it in manifest. But what if you don't know the no. of names and you have to 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);
}
Solution 1:[1]
Assuming your question is the one in title:
Is it possible to update Android Manifest programmatically?
The answer is no, it is not possible to do that programmatically.
You can enable/disable components that are declared in your manifest, but you cannot add or remove something from there, because, as the name says, it's a manifest file, which system "reads" when it installs your app. So you are declaring your app's interface in that manifest, later on you cannot change that, unless you reinstall the app with another manifest.
Assume system provided you ability to do that. Then you'd add some permission to your manifest dynamically, whereas user hasn't approved that while he was downloading your app from Play Store.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | azizbekian |