How Do You Set a Flutter Target File Based On Android Product Flavors

How Do You Set a Flutter Target File Based On Android Product Flavors?

Even after trying a lot, you fail to set the target file base on the android product flavors. In this blog, you will get to know everything about the android product flavors and how to set target file base using flutter!

The core principle of the android product flavors is the same as preparing different principles of omelet. For example, the egg is the common element in the omelet preparation, whereas you can add whatever you want, such as potato, onion, and others. Likewise, in Android, the base ingredients are the common code in the main directory accessible to all flavors.

In addition, you will have various code directories for every flavor to develop the flavor specific code, such as the flavor ingredients. Join hands with the experts at the best Flutter app development company to know in-depth about the android product flavors.

When to use android product flavors?

Plenty of situations in which you can use the product flavors in Android. But, some of the critical scenarios are mentioned here. Have you ever witnessed the pro and free versions of the same app in the play store? The app’s pro version has all the features of the free versions.

In addition, it includes the pro features. This is one of the ideal situations to access product flavors. By default, Android accesses two build types: release and debug. If you run the app via Android studio, it will install the debug apk. But, you will see both build types when building apk.

Remember that build types are entirely different from the product flavors. The top-level distribution of the app variants is the product flavors. On the other hand, the build types are the internal variants of every flavor.

How to create flavors?

Google makes the process of creating the product flavors in Android much easier. Now, you have to add the productFlavors block inside the android block in the app-level build.gradle file. It is a simple code to create product flavors.

android {
flavorDimensions 'default'
productFlavors {
dev {
dimension "default"
applicationIdSuffix ".dev"
}
stag {
dimension "default"
}
prod {
dimension "default"
}
}
}

Create flavors Dimension
Create flavors Dimension

How to setup Android studio Flutter?

When using VSCode, you have to change something android specific in some cases. You need to right-click the android folder in VSCode and then choose “open in android studio.” Once the android studio is open, you may confront the error: dart SDK not found? But, there is nothing to panic about.

The solution is straightforward because all you have to do is add the Flutter SDK path. To do so,

Step 1: Tap on the Android Studio on the top left

Step 2: Select preference

Step 3: Unfold languages & frameworks

Step 4: Select flutter

Step 5: Select flutter SDK path

Step 6: Click on apply button.

Then, you should do some minor changes and then create a new app. Here are the changes you have to do.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: '', theme: ThemeData(), home: const MyHomePage());
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo'),
      ),
      body: const Center(
        child: Text(
          'Targety',
          style: TextStyle(
            fontSize: 30,
            fontWeight: FontWeight.bold,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}

How to flavor a flutter in Android?

It is easy and simple to flavor the flutter in Android. Here is what you require at the minimum.

flavorDimensions "release-type"
productFlavors {
dev {
dimension "release-type"
applicationIdSuffix “.dev”
versionNameSuffix “-dev”
}
prod {
dimension "release-type"
}

Run this command on your android device to see the magic.

Flutter – how to set the target file base on android product flavors

Are you accessing flutter to build the application? Have you followed certain guides on building various environment entry files and creating main_dev.dart and main_prod.dart? But, you are confronting issues when trying to develop flavor for both Android and iOS using flavor in the command to develop different flavor applications.

As you need the target result mentioned below, you are now encountering the problem of combining flutter and flavor.

development flavor -> main_dev.dart entry file

production flavor -> main_prod.dart entry file

Description of problem

As mentioned below, it is possible to target the entry file in the .xcconfig file in iOS. But, you need to add –t lib/main_dev.dart after the flutter run command. So, you are searching for the best solution to set the flutter target file on the Android side.

// ios/Flutter/development.xcconfig
#include "Generated.xcconfig"
FLUTTER_TARGET=lib/main_dev.dart

There is nothing to worry about because you can utilize the following codes to fix the problem immediately.

Solution

At this moment, flutter run –t is not the only option to follow. If you open the Flutter project with the Android Studio, you will have the default config/run. Then, you have to click edit configuration. A new window will open where you can set the Dart entry point and Build Flavor.

Of course, you can create several configurations for every flavor. Therefore, it is the right solution for flutter in Android Studio. You can use the –t parameter in VSCode and perform configuration under .vscode/launch.json. Here is the exact code to do so.

"configurations": [
{
"name": "GoodOne",
"request": "launch",
"type": "dart",
"args": ["--flavor",
"good",
"-t",
"./lib/main-good.dart"
]
},
{
"name": "BadOne",
"request": "launch",
"type": "dart",
"args": ["--flavor",
"bad",
"-t",
"./lib/main-bad.dart"
]
}
]

VSCode and perform configuration
VSCode and perform configuration

Press F5 to run your flavors and select your config at the upper-left corner.

Code to set flutter. the target inside Android product flavors

flavorDimensions "app"
productFlavors {
prod {
dimension "app"
flutter.target "lib/main.dart"
}
dev {
dimension "app"
flutter.target "lib/test_main.dart"
}
}

Final words

If you have any doubt about this code, you can hire Flutter developers from Flutter Agency and get assistance from the experts without hesitation. Our experts will help you make the best out of the flutter platform and design your app or complete your project on time. We will guide and support you to set up the target file based on the Android product flavors.

Abhishek Dhanani

Written by Abhishek Dhanani

Abhishek Dhanani, a skilled software developer with 3+ years of experience, masters Dart, JavaScript, TypeScript, and frameworks like Flutter and NodeJS. Proficient in MySQL, Firebase, and cloud platforms AWS and GCP, he delivers innovative digital solutions.

Leave a comment

Your email address will not be published. Required fields are marked *


Discuss Your Project

Connect with Flutter Agency's proficient skilled team for your app development projects across different technologies. We'd love to hear from you! Fill out the form below to discuss your project.

Have Project For Us

Get in Touch

"*" indicates required fields

ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields