See what’s stored in the Android manifest file so you can set platform-specific configurations in your .NEW MAUI app.
.NET MAUI development allows you to create applications for multiple platforms, including iOS, MacCatalyst, Android, WPF, and Tizen. .NET MAUI provides a shared code base, but each platform requires specific configuration for your application to run properly. When you create a .NET MAUI project, files containing platform-specific configuration are automatically created and can be found in the Platforms folder.
In this article, we’ll take a look at the AndroidManifest.xml file, where Android configuration information is stored, and look at some of the most frequently used elements.
To help you understand, we will divide the topic into several points as follows:
What is AndroidManifest.xml?
The AndroidManifest.xml file is very important for Android applications, whether native or cross-platform. It defines the configuration properties of your application before execution, allowing you to configure various features, such as:
- authority: Applications must specify all required permissions. This means that when an app requests access to your location, it must first ask for permission. You can configure these in your AndroidManifest file. ✍️Internet permission is one of the most commonly requested permissions.
- General application information: This is where you configure all of your application’s underlying data to be displayed normally to the user. This data includes application name, icon, theme, package name, etc.
- Compatibility and Limitations: This part allows you to ensure app compatibility by defining minimum and maximum requirements for acceptable versions.
Where exactly can I find AndroidManifest.xml?
.NET MAUI on Android generates an AndroidManifest.xml file as part of the build process, which is stored in the Platform/Android folder. (You can see it in the image below.)
However, there is an additional manifest generated by merging the XML from the Platforms\Android\AndroidManifest.xml file with the XML generated from specific functions in the class. The resulting AndroidManifest.xml file is then stored in the obj folder, specifically obj\Debug\net7.0-android\AndroidManifest.xml for debug builds of .NET 7.
- Platform/Android folder: Go to your project ➡ Platform ➡ Android ➡ Double click AndroidManifest.xml
✍️ You can also open it by right-clicking on the manifest, selecting “Open with”, and then selecting the source code editor.
- Auto-generated AndroidManifesrt.xml: Go to project ➡ obj ➡ Debug ➡ net7.0-android ➡ and open: AndroidManifest.xml.
Create manifest
In .NET MAUI applications, MainActivity
The class is derived from: Activity
Through the MauiAppCompatActivity
I have class ActivityAttribute
applied. When building an app, the system searches for all classes provided by the Activity and that have: ActivityAttribute
It is caused by
The resulting AndroidManifest.xml file is generated from these non-abstract classes that derive from: Activity
which ActivityAttribute
applied.
In the MainActivity.cs of the project there is ActivityAttribute
(like this: [Activity]
) Activity Base should be as follows:
using Android.App;
namespace MyMauiApp;
[Activity]
public class MainActivity : Activity
{
}
This example adds the following XML fragment to the manifest file:
<activity android:name="crc641df78fb20bf67739.MainActivity" />
Now let’s take a look at the configuration that contains this file.
activity name
The default value for the activity name is automatically generated. This is derived from a 64-bit cyclic redundancy check of the assembly qualified name associated with the exported type. This assignment type allows you to avoid packaging errors by giving two separate assemblies the same name.
But can I change the activity name value? 🤔
If necessaryYou can name your activity using: Name
property. However, we recommend that you do the following: Rename only for backward compatibility.Doing so may affect type lookup speed at runtime.
One scenario where you might need to do this is to provide a human-readable Java name for your activity. This can be useful if you need to open another Android application or have a script that tests the startup time of an application.
Implemented in code, it looks like this:
using Android.App;
namespace MyMauiApp;
[Activity (Name="companyname.foodapp.activity")]
public class MainActivity : Activity
{
}
This example adds the following XML fragment to the manifest file:
<activity android:name="companyname.MainActivity.activity" />
App title bar
The app bar is the top bar of an interface that can display information and actions related to the current screen. In this case, the title is specific. To achieve this Label
Property may be transferred in the following manner:
using Android.App;
namespace MyMauiApp;
[Activity (Label="Food Ap")]
public class MainActivity : Activity
{
}
This example adds the following XML fragment to the manifest file:
<activity android:label="My Maui App" android:name="crc64bdb9c38958c20c7c.MainActivity" />
Launch from app selector
If your .NET MAUI application has more than one activity, it is important to specify which activity should be launched by the application launcher. This can be done by setting: MainLauncher
property True
.
using Android.App;
namespace MyMauiApp;
[Activity (Label="Food App", MainLauncher = true)]
public class MainActivity : Activity
{
}
This example adds the following XML fragment to the manifest file:
<activity android:label="Food App" android:name="crc64bdb9c38958c20c7c.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
authority
Protecting users’ privacy is a key obligation of the authority. This ensures that applications have access to only the device information and resources they need for their work. By default, the AndroidManifest in the Platforms/Android folder configures permissions for: Internet and ACCESS_NETWORK_STATE. Even if you remove the Internet permission from the manifest, the permission will still remain in the manifest file generated for debug builds.
Use the lines provided below to add the required permissions. Repeat this line for each permission required.
<uses-permission android:name="android.permission.INTERNET" />
application elements
with intention IntentFilterAttribute
Used to describe the functionality of the app. that much IntentFilterAttribute
The constructor specifies the appropriate tasks for the activity, Categories
The attribute specifies the appropriate category.
At least one activity must be provided, which is why it is included in the constructor. majority [IntentFilter]
can be provided and each instance will result in a separate result. <intent-filter/>
elements within <activity/>
.
using Android.App;
using Android.Content;
namespace MyMauiApp;
[Activity(Label = "My Maui App", MainLauncher = true)]
[IntentFilter(new[] {Intent.ActionView},
Categories = new[] {Intent.CategorySampleCode, "my.custom.category"})]
public class MyActivity : Activity
{
}
This example produces the following XML fragment:
<activity android:label="My Maui App"
android:name="crc64bdb9c38958c20c7c.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.SAMPLE_CODE" />
<category android:name="my.custom.category" />
</intent-filter>
</activity>
conclusion
I learned a lot about AndroidManifest.xml. From now on, you can look at this file to get a clearer idea of the elements. I hope this article was helpful to you! Please try practicing it in your daily life! 💚💕
Let’s meet again next time! 💁♀️
References
This article was written based on official documents.