@@ -539,6 +539,12 @@ private static void SetupAndroidProject()
539539 var appBuildPath = Path . Combine ( androidAppPath , "build.gradle" ) ;
540540 var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
541541
542+ // switch to Kotlin DSL gradle if .kts file is detected (Fluter 3.29+ by default)
543+ if ( File . Exists ( projBuildPath + ".kts" ) ) {
544+ SetupAndroidProjectKotlin ( ) ;
545+ return ;
546+ }
547+
542548 var projBuildScript = File . ReadAllText ( projBuildPath ) ;
543549 var settingsScript = File . ReadAllText ( settingsPath ) ;
544550 var appBuildScript = File . ReadAllText ( appBuildPath ) ;
@@ -592,6 +598,71 @@ implementation project(':unityLibrary')
592598 }
593599 }
594600
601+
602+ // Copy of SetupAndroidProject() adapted to Kotlin DLS .gradle.kts. Generated since Flutter 3.29
603+ private static void SetupAndroidProjectKotlin ( )
604+ {
605+ var androidPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../android" ) ) ;
606+ var androidAppPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../android/app" ) ) ;
607+ var projBuildPath = Path . Combine ( androidPath , "build.gradle.kts" ) ;
608+ var appBuildPath = Path . Combine ( androidAppPath , "build.gradle.kts" ) ;
609+ var settingsPath = Path . Combine ( androidPath , "settings.gradle.kts" ) ;
610+
611+
612+ var projBuildScript = File . ReadAllText ( projBuildPath ) ;
613+ var settingsScript = File . ReadAllText ( settingsPath ) ;
614+ var appBuildScript = File . ReadAllText ( appBuildPath ) ;
615+
616+ // Sets up the project build.gradle files correctly
617+ if ( ! Regex . IsMatch ( projBuildScript , @"flatDir[^/]*[^}]*}" ) )
618+ {
619+ var regex = new Regex ( @"allprojects \{[^\{]*\{" , RegexOptions . Multiline ) ;
620+ projBuildScript = regex . Replace ( projBuildScript , @"
621+ allprojects {
622+ repositories {
623+ flatDir {
624+ dirs(file(""${project("":unityLibrary"").projectDir}/libs""))
625+ }
626+ " ) ;
627+ File . WriteAllText ( projBuildPath , projBuildScript ) ;
628+ }
629+
630+ // Sets up the project settings.gradle files correctly
631+ if ( ! Regex . IsMatch ( settingsScript , @"include("":unityLibrary"")" ) )
632+ {
633+ settingsScript += @"
634+
635+ include("":unityLibrary"")
636+ project("":unityLibrary"").projectDir = file(""./unityLibrary"")
637+ " ;
638+ File . WriteAllText ( settingsPath , settingsScript ) ;
639+ }
640+
641+
642+ // Sets up the project app build.gradle files correctly
643+ if ( ! Regex . IsMatch ( appBuildScript , @"dependencies \{" ) )
644+ {
645+ appBuildScript += @"
646+ dependencies {
647+ implementation(project("":unityLibrary""))
648+ }
649+ " ;
650+ File . WriteAllText ( appBuildPath , appBuildScript ) ;
651+ }
652+ else
653+ {
654+ if ( ! appBuildScript . Contains ( @"implementation(project("":unityLibrary"")" ) )
655+ {
656+ var regex = new Regex ( @"dependencies \{" , RegexOptions . Multiline ) ;
657+ appBuildScript = regex . Replace ( appBuildScript , @"
658+ dependencies {
659+ implementation(project("":unityLibrary""))
660+ " ) ;
661+ File . WriteAllText ( appBuildPath , appBuildScript ) ;
662+ }
663+ }
664+ }
665+
595666 /// <summary>
596667 /// This method tries to autome the build setup required for Android
597668 /// </summary>
@@ -601,6 +672,11 @@ private static void SetupAndroidProjectForPlugin()
601672 var projBuildPath = Path . Combine ( androidPath , "build.gradle" ) ;
602673 var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
603674
675+ if ( File . Exists ( projBuildPath + ".kts" ) ) {
676+ SetupAndroidProjectForPluginKotlin ( ) ;
677+ return ;
678+ }
679+
604680 var projBuildScript = File . ReadAllText ( projBuildPath ) ;
605681 var settingsScript = File . ReadAllText ( settingsPath ) ;
606682
@@ -628,6 +704,40 @@ private static void SetupAndroidProjectForPlugin()
628704 }
629705 }
630706
707+ // Copy of SetupAndroidProjectForPlugin() adapted to Kotlin DLS .gradle.kts. Generated since Flutter 3.29
708+ private static void SetupAndroidProjectForPluginKotlin ( )
709+ {
710+ var androidPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../android" ) ) ;
711+ var projBuildPath = Path . Combine ( androidPath , "build.gradle.kts" ) ;
712+ var settingsPath = Path . Combine ( androidPath , "settings.gradle.kts" ) ;
713+
714+ var projBuildScript = File . ReadAllText ( projBuildPath ) ;
715+ var settingsScript = File . ReadAllText ( settingsPath ) ;
716+
717+ // Sets up the project build.gradle files correctly
718+ if ( Regex . IsMatch ( projBuildScript , @"// BUILD_ADD_UNITY_LIBS" ) )
719+ {
720+ var regex = new Regex ( @"// BUILD_ADD_UNITY_LIBS" , RegexOptions . Multiline ) ;
721+ projBuildScript = regex . Replace ( projBuildScript , @"
722+ flatDir {
723+ dirs(file(""${project("":unityLibrary"").projectDir}/libs""))
724+ }
725+ " ) ;
726+ File . WriteAllText ( projBuildPath , projBuildScript ) ;
727+ }
728+
729+ // Sets up the project settings.gradle files correctly
730+ if ( ! Regex . IsMatch ( settingsScript , @"include("":unityLibrary"")" ) )
731+ {
732+ settingsScript += @"
733+
734+ include("":unityLibrary"")
735+ project("":unityLibrary"").projectDir = file(""./unityLibrary"")
736+ " ;
737+ File . WriteAllText ( settingsPath , settingsScript ) ;
738+ }
739+ }
740+
631741 private static void SetupIOSProjectForPlugin ( )
632742 {
633743 var iosRunnerPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../ios" ) ) ;
0 commit comments