From 3e1f8eac6a07bca2b334c5a6da139cb64c8f31b9 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Mon, 6 May 2024 19:47:55 +0600 Subject: [PATCH] feat: add android build support --- cli/commands/build/android.dart | 90 +++++++++++++++++++++++++++++++++ cli/commands/build/common.dart | 8 +++ pubspec.lock | 2 +- pubspec.yaml | 1 + 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/cli/commands/build/android.dart b/cli/commands/build/android.dart index e69de29b..b6c7e9d6 100644 --- a/cli/commands/build/android.dart +++ b/cli/commands/build/android.dart @@ -0,0 +1,90 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:args/command_runner.dart'; +import 'package:collection/collection.dart'; +import 'package:path/path.dart'; +import 'package:xml/xml.dart'; + +import '../../core/env.dart'; +import 'common.dart'; + +class AndroidBuildCommand extends Command with BuildCommandCommonSteps { + @override + String get description => "Build for android"; + + @override + String get name => "android"; + + @override + FutureOr? run() async { + await bootstrap(); + + await shell.run( + "flutter build apk --flavor ${CliEnv.channel}", + ); + + await dotEnvFile.writeAsString( + "\nENABLE_UPDATE_CHECK=0", + mode: FileMode.append, + ); + + final androidManifestFile = File( + join(cwd.path, "android", "app", "src", "main", "AndroidManifest.xml")); + + final androidManifestXml = + XmlDocument.parse(await androidManifestFile.readAsString()); + + final deletingElement = + androidManifestXml.findAllElements("meta-data").firstWhereOrNull( + (el) => + el.getAttribute("android:name") == + "com.google.android.gms.car.application", + ); + + deletingElement?.parent?.children.remove(deletingElement); + + await androidManifestFile.writeAsString( + androidManifestXml.toXmlString(pretty: true), + ); + + await shell.run( + """ + dart run build_runner build --delete-conflicting-outputs + flutter build appbundle --flavor ${CliEnv.channel} + """, + ); + + final ogApkFile = File( + join( + "build", + "app", + "outputs", + "flutter-apk", + "app-${CliEnv.channel}-release.apk", + ), + ); + + await ogApkFile.copy( + join(cwd.path, "build", "Spotube-android-all-arch.apk"), + ); + + final ogAppbundleFile = File( + join( + cwd.path, + "build", + "app", + "outputs", + "bundle", + "${CliEnv.channel}Release", + "app-${CliEnv.channel}-release.aab", + ), + ); + + await ogAppbundleFile.copy( + join(cwd.path, "dist", "Spotube-playstore-all-arch.aab"), + ); + + stdout.writeln("✅ Built Android Apk and Appbundle"); + } +} diff --git a/cli/commands/build/common.dart b/cli/commands/build/common.dart index 4f6def55..377a2842 100644 --- a/cli/commands/build/common.dart +++ b/cli/commands/build/common.dart @@ -5,6 +5,8 @@ import 'package:path/path.dart'; import 'package:process_run/shell_run.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; +import '../../core/env.dart'; + mixin BuildCommandCommonSteps on Command { final shell = Shell(); Directory get cwd => Directory.current; @@ -29,7 +31,13 @@ mixin BuildCommandCommonSteps on Command { RegExp get versionVarRegExp => RegExp(r"\%\{\{SPOTUBE_VERSION\}\}\%", multiLine: true); + File get dotEnvFile => File(join(cwd.path, ".env")); + Future bootstrap() async { + await dotEnvFile.create(recursive: true); + + await dotEnvFile.writeAsString(CliEnv.dotenv); + await shell.run( """ flutter pub get diff --git a/pubspec.lock b/pubspec.lock index adde3224..80d29545 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -2470,7 +2470,7 @@ packages: source: hosted version: "1.0.4" xml: - dependency: transitive + dependency: "direct dev" description: name: xml sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 diff --git a/pubspec.yaml b/pubspec.yaml index e5d596b0..48fe04a0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -145,6 +145,7 @@ dev_dependencies: custom_lint: ^0.6.4 riverpod_lint: ^2.3.10 process_run: ^0.14.2 + xml: ^6.5.0 dependency_overrides: uuid: ^4.4.0