Repository: flutter
Issue: Support for Kotlin 1.9 and 2.0 in flutter module template
Labels: c: new feature,platform-android,tool,t: gradle,a: existing-apps,c: proposal,team-android

### Use case

Hi! 

Our Android project is integrated with Flutter trough [flutter module](https://docs.flutter.dev/add-to-app/android/project-setup); it's a large native codebase project where we are migrating to 100% Flutter.

# Context
The native side setup is built with `Kotlin 2.0 and AGP 8.2.2` and we are doing well  using flutter `3.24.0`, which uses `Kotlin 1.7.10 and agp 7.3.0`, defined from default variable [templateKotlinGradlePluginVersion](https://github.com/flutter/flutter/blob/80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819/packages/flutter_tools/lib/src/android/gradle_utils.dart#L33) and [templateAndroidGradlePluginVersionForModule](https://github.com/flutter/flutter/blob/80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819/packages/flutter_tools/lib/src/android/gradle_utils.dart#L32C14-L32C57) respectively, variables used to generate the `.android` folder for every run of `flutter pub get` or `flutter build aar`.

# Problem
We are bumping our code base Flutter (plugins and third-party libraries) to Kotlin 2.0 and AGP 8.2.2 (like the native project setup), but we are not able to build the aar after upgrading the `dynatrace_flutter_plugin`. Which is reproducing the following output:

```
e: file:///Users/xxx/.pub-cache/hosted/pub.dev/dynatrace_flutter_plugin-3.305.2/android/src/main/kotlin/com/dynatrace/android/agent/SettingsApiImpl.kt:32:21 This declaration needs opt-in. Its usage must be marked with '@kotlin.ExperimentalStdlibApi' or '@OptIn(kotlin.ExperimentalStdlibApi::class)'
e: file:///Users/xxxx/.pub-cache/hosted/pub.dev/dynatrace_flutter_plugin-3.305.2/android/src/main/kotlin/com/dynatrace/android/agent/SettingsApiImpl.kt:32:41 This declaration needs opt-in. Its usage must be marked with '@kotlin.ExperimentalStdlibApi' or '@OptIn(kotlin.ExperimentalStdlibApi::class)'
e: file:///Users/xxx/.pub-cache/hosted/pub.dev/dynatrace_flutter_plugin-3.305.2/android/src/main/kotlin/com/dynatrace/android/agent/SettingsApiImpl.kt:32:41 The feature "enum entries" is only available since language version 1.9
```

This error indicates that the build of aar is using a Kotlin version below than 1.9.0; that is true, because in Flutter `3.27.0` the Kotlin version for templates is [1.8.22.](https://github.com/flutter/flutter/blob/8495dee1fd4aacbe9de707e7581203232f591b2f/packages/flutter_tools/lib/src/android/gradle_utils.dart#L32)

# Steps to reproduce
The error above is reproduced using flutter `3.27.0`.

1. Create module: `flutter create -t module sample_module`
2. Add dynatrace plugin: `cd sample_module && flutter pub add dynatrace_flutter_plugin`
3. Build aar: `flutter build aar`

* *Note:* The same occur using Flutter versions `3.24.0` and `3.24.3`

# Our proposals
Following the above scenario, we are requesting some of the following options to solve this problem:
## 1. Flexibility template options
Extend the functionality of pubspec yaml field `module` to support the definition of Kotlin, AGP, Gradle and the android.compileOptions.targetCompatibility/sourceCompatibility through YAML. Example:

```yaml
name: sample_module
description: lorem epsum
version: 1.0.0+1

environment: ....

dependencies: ...

flutter:
   module:
        androidX: true
        androidPackage: br.com.sample_module
        ...
        kotlinVersion: 2.0.0
        agpVersion: 8.2.2
        gradleVresion: 8.2-all
        javaTargetCompatiblity: 17
        javaSourceCompatibility: 17
```

## 2. Create a toggle to prevent .android folder recreation
[The Android setup has a note warning about the auto generated .android folder](https://docs.flutter.dev/add-to-app/android/project-setup). It will be great if exists an toggle to turn off this auto-generation, so we can manually adjust the project and push to our repository.

## 3. Create a template that's supports kotlin 2.0 and AGP 8.x
Create a new template, adjusting setting up JAVA_17 and removing AndroidManifest.xml `package` field. Details [here](https://medium.com/@kacper.wojciechowski/kotlin-2-0-android-project-migration-guide-b1234fbbff65)
 

# Conclusion
I hope you guys understand our scenario and the steps to reproduce the error. Any doubts, touch me through e-mail: reberth.kelvin@xpi.com.br.

Best Regards,
Reberth Kelvin