Change default App name for flutter project in codemagic

132 views Asked by At

Is there a way by which I can change my default flutter app name to some other name(one that I prefer) in codemagic. I am using the automated workflow provided by codemagic. What I was thinking is to use a script either pre build for which I have written a dart code below :

import 'package:fresh_operator/core/utils/flavor_config.dart';
import 'package:logger/logger.dart';
import 'package:package_info/package_info.dart';

class VersionUtils {
  final logger = Logger(printer: PrettyPrinter(colors: true));

  // Function to generate build name
  Future<String> generateBuildName() async {
    var currentDate = DateTime.now().toLocal();
    String releaseVersionNumber = await getAppVersionNumber();
    late int patchNumber;
    if (releaseVersionNumber == '2') {
      patchNumber = 1;
    }
    String flavorName = Flavors.prod.toString();
    String buildName =
        'fresh_operator_${flavorName}_${currentDate.year}.$releaseVersionNumber.${patchNumber++}';
    return buildName;
  }

  // This creates your app version number
  Future<String> getAppVersionNumber() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String releaseVersionNumber = packageInfo.version;
    return releaseVersionNumber;
  }
}

Problem here is how to use the different app name for different flavors as well a precise logic when a release number is changed then the patchNumber shall start from 1. For example : fresh_operator_prod_2023.1.5 when jumped to release version 2 it should be something like fresh_operator_prod_2023.2.1

I tried putting a block inside the build.gradle from certain source for creating app name pre build but that didn't work because the gradle cannot refer to a dart file.

1

There are 1 answers

0
himeshp On

You can run the dart script via the command line to store the app name as output in your dart file.

e.g.

APP_NAME=$(dart version.dart prod)

here is the sample script version.dart

void main(List<String> arguments) async {
  final versionUtils = VersionUtils();

  // Get the flavor from arguments or default to 'prod'
  String flavor = arguments.isNotEmpty ? arguments.first : 'prod';

  // Generate new build name
  String newBuildName = await versionUtils.generateBuildName(flavor);

  // Print out the generated build name
  print("build name: $newBuildName");
}

class VersionUtils {
  // Use a mock version number or extract it from somewhere else for local testing
  Future<String> getAppVersionNumber() async {
    return '1.0.1'; // For example
  }

  // ... [Rest of the methods remain unchanged]

  Future<String> generateBuildName(String flavor) async {
    var currentDate = DateTime.now().toLocal();
    String releaseVersionNumber = await getAppVersionNumber();
    int patchNumber = 1; // This is just an example; use your logic to determine the patch number

    String buildName = 'fresh_operator_${flavor}_${currentDate.year}.$releaseVersionNumber.$patchNumber';
    return buildName;
  }
}

After running the above command, the output of the Dart script is now stored in the APP_NAME variable. You can use it in subsequent commands as you'd like.

Feel free to edit the dart script as per your requirements.