@@ -539,6 +539,12 @@ private static void SetupAndroidProject()
539
539
var appBuildPath = Path . Combine ( androidAppPath , "build.gradle" ) ;
540
540
var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
541
541
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
+
542
548
var projBuildScript = File . ReadAllText ( projBuildPath ) ;
543
549
var settingsScript = File . ReadAllText ( settingsPath ) ;
544
550
var appBuildScript = File . ReadAllText ( appBuildPath ) ;
@@ -592,6 +598,71 @@ implementation project(':unityLibrary')
592
598
}
593
599
}
594
600
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
+
595
666
/// <summary>
596
667
/// This method tries to autome the build setup required for Android
597
668
/// </summary>
@@ -601,6 +672,11 @@ private static void SetupAndroidProjectForPlugin()
601
672
var projBuildPath = Path . Combine ( androidPath , "build.gradle" ) ;
602
673
var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
603
674
675
+ if ( File . Exists ( projBuildPath + ".kts" ) ) {
676
+ SetupAndroidProjectForPluginKotlin ( ) ;
677
+ return ;
678
+ }
679
+
604
680
var projBuildScript = File . ReadAllText ( projBuildPath ) ;
605
681
var settingsScript = File . ReadAllText ( settingsPath ) ;
606
682
@@ -628,6 +704,40 @@ private static void SetupAndroidProjectForPlugin()
628
704
}
629
705
}
630
706
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
+
631
741
private static void SetupIOSProjectForPlugin ( )
632
742
{
633
743
var iosRunnerPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../ios" ) ) ;
0 commit comments