@@ -514,6 +514,12 @@ private static void SetupAndroidProject()
514514 var appBuildPath = Path . Combine ( androidAppPath , "build.gradle" ) ;
515515 var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
516516
517+ // switch to Kotlin DSL gradle if .kts file is detected (Fluter 3.29+ by default)
518+ if ( File . Exists ( projBuildPath + ".kts" ) ) {
519+ SetupAndroidProjectKotlin ( ) ;
520+ return ;
521+ }
522+
517523 var projBuildScript = File . ReadAllText ( projBuildPath ) ;
518524 var settingsScript = File . ReadAllText ( settingsPath ) ;
519525 var appBuildScript = File . ReadAllText ( appBuildPath ) ;
@@ -567,6 +573,71 @@ implementation project(':unityLibrary')
567573 }
568574 }
569575
576+
577+ // Copy of SetupAndroidProject() adapted to Kotlin DLS .gradle.kts. Generated since Flutter 3.29
578+ private static void SetupAndroidProjectKotlin ( )
579+ {
580+ var androidPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../android" ) ) ;
581+ var androidAppPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../android/app" ) ) ;
582+ var projBuildPath = Path . Combine ( androidPath , "build.gradle.kts" ) ;
583+ var appBuildPath = Path . Combine ( androidAppPath , "build.gradle.kts" ) ;
584+ var settingsPath = Path . Combine ( androidPath , "settings.gradle.kts" ) ;
585+
586+
587+ var projBuildScript = File . ReadAllText ( projBuildPath ) ;
588+ var settingsScript = File . ReadAllText ( settingsPath ) ;
589+ var appBuildScript = File . ReadAllText ( appBuildPath ) ;
590+
591+ // Sets up the project build.gradle files correctly
592+ if ( ! Regex . IsMatch ( projBuildScript , @"flatDir[^/]*[^}]*}" ) )
593+ {
594+ var regex = new Regex ( @"allprojects \{[^\{]*\{" , RegexOptions . Multiline ) ;
595+ projBuildScript = regex . Replace ( projBuildScript , @"
596+ allprojects {
597+ repositories {
598+ flatDir {
599+ dirs(file(""${project("":unityLibrary"").projectDir}/libs""))
600+ }
601+ " ) ;
602+ File . WriteAllText ( projBuildPath , projBuildScript ) ;
603+ }
604+
605+ // Sets up the project settings.gradle files correctly
606+ if ( ! Regex . IsMatch ( settingsScript , @"include("":unityLibrary"")" ) )
607+ {
608+ settingsScript += @"
609+
610+ include("":unityLibrary"")
611+ project("":unityLibrary"").projectDir = file(""./unityLibrary"")
612+ " ;
613+ File . WriteAllText ( settingsPath , settingsScript ) ;
614+ }
615+
616+
617+ // Sets up the project app build.gradle files correctly
618+ if ( ! Regex . IsMatch ( appBuildScript , @"dependencies \{" ) )
619+ {
620+ appBuildScript += @"
621+ dependencies {
622+ implementation(project("":unityLibrary""))
623+ }
624+ " ;
625+ File . WriteAllText ( appBuildPath , appBuildScript ) ;
626+ }
627+ else
628+ {
629+ if ( ! appBuildScript . Contains ( @"implementation(project("":unityLibrary"")" ) )
630+ {
631+ var regex = new Regex ( @"dependencies \{" , RegexOptions . Multiline ) ;
632+ appBuildScript = regex . Replace ( appBuildScript , @"
633+ dependencies {
634+ implementation(project("":unityLibrary""))
635+ " ) ;
636+ File . WriteAllText ( appBuildPath , appBuildScript ) ;
637+ }
638+ }
639+ }
640+
570641 /// <summary>
571642 /// This method tries to autome the build setup required for Android
572643 /// </summary>
@@ -576,6 +647,11 @@ private static void SetupAndroidProjectForPlugin()
576647 var projBuildPath = Path . Combine ( androidPath , "build.gradle" ) ;
577648 var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
578649
650+ if ( File . Exists ( projBuildPath + ".kts" ) ) {
651+ SetupAndroidProjectForPluginKotlin ( ) ;
652+ return ;
653+ }
654+
579655 var projBuildScript = File . ReadAllText ( projBuildPath ) ;
580656 var settingsScript = File . ReadAllText ( settingsPath ) ;
581657
@@ -603,6 +679,40 @@ private static void SetupAndroidProjectForPlugin()
603679 }
604680 }
605681
682+ // Copy of SetupAndroidProjectForPlugin() adapted to Kotlin DLS .gradle.kts. Generated since Flutter 3.29
683+ private static void SetupAndroidProjectForPluginKotlin ( )
684+ {
685+ var androidPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../android" ) ) ;
686+ var projBuildPath = Path . Combine ( androidPath , "build.gradle.kts" ) ;
687+ var settingsPath = Path . Combine ( androidPath , "settings.gradle.kts" ) ;
688+
689+ var projBuildScript = File . ReadAllText ( projBuildPath ) ;
690+ var settingsScript = File . ReadAllText ( settingsPath ) ;
691+
692+ // Sets up the project build.gradle files correctly
693+ if ( Regex . IsMatch ( projBuildScript , @"// BUILD_ADD_UNITY_LIBS" ) )
694+ {
695+ var regex = new Regex ( @"// BUILD_ADD_UNITY_LIBS" , RegexOptions . Multiline ) ;
696+ projBuildScript = regex . Replace ( projBuildScript , @"
697+ flatDir {
698+ dirs(file(""${project("":unityLibrary"").projectDir}/libs""))
699+ }
700+ " ) ;
701+ File . WriteAllText ( projBuildPath , projBuildScript ) ;
702+ }
703+
704+ // Sets up the project settings.gradle files correctly
705+ if ( ! Regex . IsMatch ( settingsScript , @"include("":unityLibrary"")" ) )
706+ {
707+ settingsScript += @"
708+
709+ include("":unityLibrary"")
710+ project("":unityLibrary"").projectDir = file(""./unityLibrary"")
711+ " ;
712+ File . WriteAllText ( settingsPath , settingsScript ) ;
713+ }
714+ }
715+
606716 private static void SetupIOSProjectForPlugin ( )
607717 {
608718 var iosRunnerPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../ios" ) ) ;
0 commit comments