appBuildGradleEditFunc method
void
appBuildGradleEditFunc()
Implementation
void appBuildGradleEditFunc() {
try {
final filePath = "${Directory.current.path}/android/app/build.gradle";
final file = File(filePath);
List<String> lines = file.readAsLinesSync();
final int applyFormIndex = lines.indexWhere(
(line) => line.contains('id "dev.flutter.flutter-gradle-plugin"'));
if (applyFormIndex != -1) {
final additionalCode = '''
def dartEnvironmentVariables = [
APP_FLAVOR: project.hasProperty('mode')
];
if (project.hasProperty('dart-defines')) {
dartEnvironmentVariables = dartEnvironmentVariables +
project.property('dart-defines')
.split(',')
.collectEntries { entry ->
def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')
if (pair.first() == 'mode') {
project.ext.APP_FLAVOR = pair.last()
}
[(pair.first()): pair.last()]
}
}
def renamePath = { outputFileName ->
gradle.projectsEvaluated {
tasks.whenObjectAdded { task ->
task.doLast {
def flutterApkDir = new File("\${project.buildDir}/outputs/flutter-apk/app-release.apk")
if (flutterApkDir.exists()) {
flutterApkDir.renameTo(new File("\${project.buildDir}/outputs/flutter-apk/\$outputFileName"))
}
}
}
}
}
def appFlavor() {
if (project.hasProperty('APP_FLAVOR')) {
return "\${project.ext.APP_FLAVOR}_"
}
}
''';
lines.insert(applyFormIndex + 2, additionalCode);
file.writeAsStringSync(lines.join('\n'));
int index = lines.indexWhere(
(line) => line.contains('signingConfig') && line.contains('debug'));
if (index != -1) {
final additionalCode = '''
android.applicationVariants.all { variant ->
variant.outputs.all {
if(appFlavor() != null){
def appName = variant.getMergedFlavor().applicationId
int lastIndex = appName.lastIndexOf('.')
def modifiedAppName = lastIndex != -1 ? appName.substring(lastIndex + 1) : appName
outputFileName = "\${modifiedAppName}_\${appFlavor()}\${flutter.versionName}(\${flutter.versionCode}).apk"
renamePath(outputFileName)
}
}
}
''';
lines.insert(index + 1, additionalCode);
file.writeAsStringSync(lines.join('\n'));
'ssl_cli build setup successfully.'
.printWithColor(status: PrintType.success);
} else {
'Error: Line pattern not found in the specified file.'
.printWithColor(status: PrintType.error);
}
}
} catch (e) {
print('Error: $e');
}
}