Skip to main content

Creating Environment Specific Configurations

Platforms: iOS, Android, Windows, Mac Catalyst

Many software development teams use different environments throughout the software development lifecycle. Configuration may differ between environments, such as bundle IDs, deep-link schemes, or icons and splash screens.

Napacitor's configuration file (napacitor.config.json) handles high-level options for the Napacitor tooling and plugin configuration. The host's native values (package name, app name, etc.) are MSBuild properties in maui/App.csproj. By combining the two, developers can use the nap CLI to build apps for different environments.

This guide will walk you through setting up a QA environment configuration alongside the default environment configuration provided out-of-the-box. To demonstrate the differences between the two environments, the app name and package name will differ between them.

Prepare a Napacitor App​

You will need a Napacitor app (the MAUI host generated by npx nap add maui). If you already have one, skip this section.

Depending on your preference, you can add Napacitor to an existing web application.

Before adding any native platform to the project, you must build the web app at least once:

npm run build

Once built, generate the MAUI host:

npx nap add maui

Distinguish Environments with MSBuild Conditional Properties​

The MAUI host is a single project targeting multiple platforms (one App.csproj targeting all TFMs), so environment differences can all be expressed with conditional property groups — there is no need to duplicate Xcode targets or configure Gradle product flavors as you would in Capacitor.

Open maui/App.csproj and split the app name and package name by build configuration:

<PropertyGroup Condition="'$(Configuration)' != 'Release'">
<ApplicationTitle>My App - QA</ApplicationTitle>
<ApplicationId>com.mycompany.myapp.qa</ApplicationId>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<ApplicationTitle>My App</ApplicationTitle>
<ApplicationId>com.mycompany.myapp</ApplicationId>
</PropertyGroup>

Here is how this works:

  1. Configuration is a built-in MSBuild property; the -c argument of nap build (default Debug) is passed straight through to it;
  2. The QA environment's package name is distinguished by the .qa suffix on ApplicationId, and the app name carries a - QA suffix;
  3. ApplicationTitle / ApplicationId apply to all platforms (Android/iOS/Windows) — defining them in one place is enough.

Note: You are free to modify the package name and display name values to your liking.

You can also distinguish environments with a custom property (instead of Configuration), for example:

<PropertyGroup Condition="'$(Env)' == 'qa'">
<ApplicationTitle>My App - QA</ApplicationTitle>
<ApplicationId>com.mycompany.myapp.qa</ApplicationId>
</PropertyGroup>

Then pass it at build time:

npx nap build --target android -c Release -p:Env=qa

Environment-Specific Napacitor Configuration​

If your environments also need different plugin configuration or a different webDir, you can maintain multiple napacitor.config.json files. After nap init generates the default configuration, switch between them with a script:

npx nap init com.mycompany.myapp "My App" --web-dir dist # default environment
npx nap init com.mycompany.myapp.qa "My App - QA" --web-dir dist # QA environment
note

nap init overwrites napacitor.config.json. It's recommended to save each environment's configuration as a template file (e.g. napacitor.config.qa.json) and, in CI, copy the appropriate one per environment before running npx nap sync.

Dynamically Build for Different Environments​

With all the pieces in place, switching environments comes down to just two actions:

  1. Use the matching napacitor.config.json (if needed);
  2. Pass the environment condition with -c / -p::
# QA environment
npx nap build --target android -c Debug # Debug build → QA values
# or pass a custom property explicitly
npx nap build --target android -c Release -p:Env=qa

# default (production) environment
npx nap build --target android -c Release

Go ahead and test it out! If you've followed the guide correctly, you'll be able to build for both environments and see that the app name / package name differ depending on the environment-specific configuration used.

Additional Environments and Configuration Options​

Use the information provided in this guide as a foundation to build upon. MSBuild conditional properties have no limit on how many you can define, and you can configure each environment as deeply as .NET allows. You can also provide different environment-specific configuration values for Napacitor plugins (via the plugins section of napacitor.config.json)! The sky's the limit.