We are looking at some Java source code often say we want to add something in the Androidmanifest.xml. And what we do with Xamarin is actually tagged with the feature attribute of C #.
We created a Xamarin Android project mainactivity will have a default tag feature
" App " true )] publicclass mainactivity:activity
Detailed content is from
https://developer.xamarin.com/guides/android/advanced_topics/working_with_androidmanifest.xml/
Below we will explain in detail how to use in Xamarin Android.
Overview
Androidmanifest.xml is a powerful manifest file in the Android platform that allows you to describe the app's capabilities and requirements for Android. However, it is not easy to use it well. xamarin.android is used to automatically generate manifest files by adding custom attribute to the corresponding class to reduce the difficulty of using Androidmanifest.xml. Our goal is that 99% of users do not need to manually modify androidmanifest.xml.
Androidmanifest.xml As part of the build build, custom attribute will be merged with the files in properties/androidmanifest.xml . (So don't worry if you're androidmanifest.xml manually defined in the manual). The resulting merged XML file can be seen in the project's obj directory: For example, it exists in the obj/debug/android/androidmanifest.xml file. The process of merging XML files is straightforward:
1, by scanning the specific tags in the assembly and using custom attribute to generate XML elements, 2, and then insert these elements into the androidmanifest.xml .
Basis
At compile time, non-abstract classes derived from the activity class are scanned and the activity attribute is declared on them. It then uses these classes and attributes to build the manifest. For example, refer to the following code:
namespace demo{ publicclass myactivity:activity { }}
But doing so does not generate anything in androidmanifest.xml . If you want to build the <activity/> element, you need to use the [activity] custom attribute:
namespace demo{ [Activity] publicclass myactivity:activity { }}
This code will generate the following XML fragment and insert it into the androidmanifest.xml:
<android:name= "md5a7a3c803e481ad8926683588c7e9031b. Mainactivity "/>
The [Activity] attribute has no effect on the abstract type, and the abstract type is ignored.
Activity Name
Starting from Xamarin.android 5.1 The activity's Name property default value is based on the md5sum of the collection-qualified name of the exported type. This allows the same fully qualified name to be provided from two different assemblies without a package error. (before Xamarin.android 5.1 , the default type name for an activity was created by a lowercase namespace and class name.) )
If you want to override this default and explicitly specify the name of the activity, use the Name property:
[Activity (Name="awesome.demo.activity")] Public class myactivity:activity{}
This is useful for third-party invocation and so on. If you do not have this requirement, you can use the default value without setting it.
The code above generates the following XML fragment
<activity android:name="awesome.demo.activity" />
Note: You should use the Name property only for backwards compatibility, because such a setting slows down type lookups at run time. If you have legacy code that expects the default type name of the activity based on the underlying namespace and class name, see Android Callable wrapper naming to learn how to maintain compatibility.
Activity Title Bar
By default, at run time, Android will give your app a title bar. This value will be used to/manifest/application/activity/@android:label中。在大多数情况下,这个值与类名不同。要在标题栏上指定应用程序的标签,请使用标签属性。例如:
[Activity (label="Awesome Demo App")] Public class myactivity:activity{}
The following is the generated XML fragment
<android:label= "Awesome Demo App" android:name= " md5a7a3c803e481ad8926683588c7e9031b. Mainactivity "/>
Launchable from Application Chooser
By default, your activity does not appear on the Android application startup screen. This is because there may be a lot of activity in your application, and you can use the Mainlauncher property to specify which one should be started from the Application Launcher. For example:
[Activity (label="Awesome Demo App", mainlauncher=true Public class myactivity:activity{}
The generated XML fragment
<ActivityAndroid:label= "Awesome Demo App"Android:name= "md5a7a3c803e481ad8926683588c7e9031b." Mainactivity "> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter></Activity>
Activity Icon
By default, your activity will get the default boot icon provided by the system. to use a custom icon, first add your . png to resources/drawable, set the build action of the file to Androidresource, and then use the Icon property to specify the icon to use. For example:
[Activity (label="Awesome Demo App", mainlauncher=true, icon="@ Drawable/myicon"publicclass myactivity:activity{}
The generated XML fragment
<ActivityAndroid:icon= "@drawable/myicon"Android:label= "Awesome Demo App"Android:name= "md5a7a3c803e481ad8926683588c7e9031b." Mainactivity "> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter></Activity>
Permissions
When you add permissions to the Android manifest (androidmanifest in permissions are described). These permissions are saved in properties/androidmanifest.xml. For example, if you set the network license element properties/androidmanifest.xml:
<android:name= "Android.permission.INTERNET"/>
Debug mode automatically adds some permissions to simplify debugging (for example, network rights read extended storage permissions)--These settings will only appear in the obj/debug/android/androidmanifest.xml Debug mode will not appear in the Red box when you configure the permissions interface
If you open the manifest file Obj/debug/android/androidmanifest.xml, you can see the following permissions configured
<android:name= "Android.permission.INTERNET"/>< android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
The permissions above in the release build (obj/debug/android/androidmanifest.xml) will not be automatically configured. If you find that the debug version is available when you switch to release version, check that this permission has been explicitly set in your application's permissions configuration.
Please check the specific operation (VS MAC is similar).
Advanced features Intent Actions and Features
Androidmanifest.xml provides a way to describe activity capabilities. This is achieved through Intents and [Intentfilter] custom properties. You can use the Categories property in the Intentfilter constructor to specify which action types are appropriate for your activity. At a minimum, you need to provide an actions (which is why you are asked to provide actions in the constructor). [IntentFilter]allows you to define multiple times in an activity. For example:
[Activity (label="Awesome Demo App", mainlauncher=true, icon="@ Drawable/myicon")] [Intentfilter (new[]{intent.actionview}, Categories =new"my.custom.category"})] Public Class myactivity:activity{}
The following is the generated XML fragment
<ActivityAndroid:icon= "@drawable/myicon"Android:label= "Awesome Demo App"Android:name= "md5a7a3c803e481ad8926683588c7e9031b." Mainactivity "> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> <Intent-filter> <ActionAndroid:name= "Android.intent.action.VIEW" /> <categoryAndroid:name= "Android.intent.category.SAMPLE_CODE" /> <categoryAndroid:name= "My.custom.category" /> </Intent-filter></Activity>
Application Element
Androidmanifest.xml also provides a way to declare properties for an entire application. This is done through the <application> element and its corresponding application custom attributes. Note that these are application-scoped (assembly-scoped) settings, not each activity setting. Typically, you will declare <application> properties for your entire application, and then override these settings on a per-activity basis (as needed).
For example, the following application attribute is added to AssemblyInfo.cs to indicate that the application can be debugged, that its user-readable name is My App, and that it uses the Theme.light Style as the default theme for all activity:
[Assembly:application (debuggable=true, Label="My App", Theme="@android: style/theme.light")]
This will generate the following XML fragment in the following location Obj/debug/android/androidmanifest.xml
<android:label= "My App" android:debuggable= "true" Android:theme = "@android: Style/theme.light" />
In this example, the activity in all apps will use the Theme.light theme by default. If you set up an activity theme for Theme.dialog then only the activity will take effect, and the other activity remains the default Theme.light theme.
Setting the [application] feature is not the only way to configure it; You can also modify the <applicaiton> node in the properties/androidmanifest.xml directly, Your settings will be merged with other attribute and saved in Obj/debug/android/androidmanifest.xml . It is important to note that the manual configuration in XML will always overwrite the settings of the custom attribute
Here are a lot of the entire app global properties can be configured in <application>, specific can view the following connection to view public properties Applicationattribute.
List of Custom Attributes
Some of the other related dependencies on configuration items can be answered here.
- Android.App.ActivityAttribute: Generating/manifest/application/activity XML
- Android.App.ApplicationAttribute: Generate/manifest/application XML Fragment
- Android.App.InstrumentationAttribute: Generate/manifest/instrumentationxml Fragment
- Android.App.IntentFilterAttribute: Generate//intent-filter XML Fragment
- Android.App.MetaDataAttribute: Generate//meta-data XML Fragment
- Android.App.PermissionAttribute: Generate//permission XML Fragment
- Android.App.PermissionGroupAttribute: Generate//permission-group XML Fragment
- Android.App.PermissionTreeAttribute: Generate//permission-tree XML Fragment
- Android.App.ServiceAttribute: Generate/manifest/application/service XML Fragment
- Android.App.UsesLibraryAttribute: Generate/manifest/application/uses-library XML Fragment
- Android.App.UsesPermissionAttribute: Generate/manifest/uses-permissionxml Fragment
- Android.Content.BroadcastReceiverAttribute: Generate/manifest/application/receiver XML Fragment
- Android.Content.ContentProviderAttribute: Generate/manifest/application/provider XML Fragment
- Android.Content.GrantUriPermissionAttribute: Generate/manifest/application/provider/grant-uri-permission XML Fragment
5, how to configure and adjust Androidmanifest.xml in xamarin.android