Dynamic AndroidManifest.xml - android

Is it possible to dynamically define aspects of the AndroidManifest.xml? For example, is it possible to register or edit the definition of activities, services and receivers on the fly, using Java code?
If so, are there any limitations on where this code should be placed? What else can be dynamically defined?

I believe most of the available operations are described by the PackageManager documentation
You can add and remove permissions (addPermission()) and you can enable or disable components (services, receivers etc.) with setComponentEnabledSetting().

Related

Is it possible to set android permission level for a broadcast receiver at runtime?

Is it possible to programatically set the protection level of an IntentFilter/Intent in java similar to how we can define it in the AndroidManifest.xml:
<receiver
android:name="my.test.receiver"
android:protectionLevel="signature" />
We have a core library for our apps which facilitates dynamic broadcasts and receivers at runtime and we would like to make them secure here.
I have found no information on this method online and not sure if it is possible. if we could set an intentFilters permission level = PermissionInfo.PROTECTION_SIGNATURE it would work but going by the docs it seems PermissionInfo is only used for reading. The only alternative method I can find is declaring a special permission, sending it with our broadcasts and adding it to all our manifests: https://stackoverflow.com/a/15316487/1810256
We may fall back on this approach but we would prefer the first method as the signature approach seems more secure.
Any help/suggestions appreciated!
Quoting one of the users(Naveed Ahmad):
"Guys now from Android 6.0 Marshmellow onward, you can ask users at runtime to allow permissions dynamically, I found of it, have a look at this link. I hope it will help you getting idea."
https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
I found it in here:
get Android permission dynamiclly

Is it possible to add provider in AndroidManifest dynamically?

Wondering if its possible to add <provider> element in AndroidManifest from code rather having it specified in static AndroidManifest?
I see that such thing is possible for <receiver>, but couldn't find anyway to achieve run-time addition for <provider>.
Wondering if its possible to add element in AndroidManifest from code rather having it specified in static AndroidManifest?
No, sorry. The closest things are:
Enabling and disabling components (e.g., activities) via PackageManager and setComponentEnabledSetting()
Registering a BroadcastReceiver via registerReceiver(), as opposed to it having a manifest entry
Is it possible to add provider in AndroidManifest dynamically?
No, sorry.

What is the purpose of disabling an Android service in the manifest?

In Android it is possible to create a Service to do background tasks, etc by creating a subclass of Service. It order to use the Service it must be specified in the manifest for the app:
All services must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
One of the parameters for a Service in the manifest is the 'enabled' option:
Whether or not the service can be instantiated by the system — "true" if it can be, and "false" if not.
What is the purpose in declaring a Service to be disabled - if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?
The only use I can see for disabling a Service in the manifest, and it seems of limited value, is if it's a Service used only for debugging, and I want it disabled for production. Am I missing something?
The android:enabled attribute set to a boolean value defined in a resource file. The purpose of this attribute is to enable or disable the service on devices running on different Android OS version.
For example, to disable the service on devices running Android 4.3 or lower include menifest attribute android:enabled="#bool/atLeastKitKat".
In addition to including this attribute in the manifest, you need to do the following:
In your bool.xml resources file under res/values/, add this line:
<bool name="atLeastKitKat">false</bool>
In your bool.xml resources file under res/values-v19/, add this line:
<bool name="atLeastKitKat">true</bool>
if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?
In the very specific case of a Service, I agree that it would be rare for you to want to disable it. One possibility would be for a service that plugs into the system (e.g., input method editor, accessibility service) that you only want to enable at runtime (via PackageManager and setComponentEnabledSetting()) if the user make an in-app purchase that unlocks the feature. I am sure that there are other Service scenarios for this, though none are leaping to mind at this early hour of the day (yawn!).
However, I suspect that Service "inherits" its android:enabled setting by virtue of being one of the Android component types, along with activities, providers, and receivers. Other scenarios for android:enabled will be a bit more common with other component types. For example, it is considered good form to have your BOOT_COMPLETED receiver be disabled until you know that you need it. So, for example, if the BOOT_COMPLETED receiver is only used to resume a download interrupted by a reboot, you only need that receiver enabled if you are doing a download. At all other times, you may as well leave it disabled, so you don't waste the user's time during "normal" reboots.

Do I HAVE to declare every activity in a manifest file?

I want to create an Activity but not have to declare it in the manifest file. Is this possible? Everywhere I've seen it seems that every activity must be declared in the manifest, yet I notice that some activities, such as the built-in ChooserActivity, is not declared in my manifest file.
Short answer: yes, every Activity in your application must be declared in the manifest. As described in the Android docs, the purpose of the manifest (among other things) is:
It describes the components of the application — the activities, services, broadcast receivers, and content providers that the
application is composed of. It names the classes that implement each
of the components and publishes their capabilities (for example, which
Intent messages they can handle). These declarations let the Android
system know what the components are and under what conditions they can
be launched.)
http://developer.android.com/guide/topics/manifest/manifest-intro.html#ifs
Therefore any Activity class in your application must be defined in your Manifest. The same goes with Intents, Services etc. even if those components aren't accessible from outside of your application.
As for ChooserActivity and any other Activity which you didn't define in code, they will have their own definitions in another Manifest. If for whatever reason you decide to subclass an existing Activity outside of your application, then you will have to define it in your Manifest too.
I want to create an Activity but not have to declare it in the manifest file. Is this possible?
No, sorry.
I notice that some activities, such as the built-in ChooserActivity, is not declared in my manifest file
That activity is not part of your application. It is part of the core operating system.

Register Android service in source code instead in Android Manifest

Does anyone know that is it possible to register (add) declaration of service in source code instead in AndroidManifest.xml if yes... how?
Thanks for any suggestions or help.
It's not possible. You have to have your Service registered in AndroidManifest.xml.
As stated here:
Like activities (and other components), you must declare all services in your application's manifest file.
So you will allways have to list your Service in the manifest file.
AndroidManifest.xml is part of your source code. This is the place where Android expects to find your services and activities declared, and there's no way around it.
The manifest presents essential information about the application to the Android system, information the system must have before it can run any of the application's code.
So it is mandatory to declare all your components (with their intent filters i.e Category , Action , Data) in your manifest file.

Categories

Resources