@@ -514,6 +514,12 @@ private static void SetupAndroidProject()
514
514
var appBuildPath = Path . Combine ( androidAppPath , "build.gradle" ) ;
515
515
var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
516
516
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
+
517
523
var projBuildScript = File . ReadAllText ( projBuildPath ) ;
518
524
var settingsScript = File . ReadAllText ( settingsPath ) ;
519
525
var appBuildScript = File . ReadAllText ( appBuildPath ) ;
@@ -567,6 +573,71 @@ implementation project(':unityLibrary')
567
573
}
568
574
}
569
575
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
+
570
641
/// <summary>
571
642
/// This method tries to autome the build setup required for Android
572
643
/// </summary>
@@ -576,6 +647,11 @@ private static void SetupAndroidProjectForPlugin()
576
647
var projBuildPath = Path . Combine ( androidPath , "build.gradle" ) ;
577
648
var settingsPath = Path . Combine ( androidPath , "settings.gradle" ) ;
578
649
650
+ if ( File . Exists ( projBuildPath + ".kts" ) ) {
651
+ SetupAndroidProjectForPluginKotlin ( ) ;
652
+ return ;
653
+ }
654
+
579
655
var projBuildScript = File . ReadAllText ( projBuildPath ) ;
580
656
var settingsScript = File . ReadAllText ( settingsPath ) ;
581
657
@@ -603,6 +679,40 @@ private static void SetupAndroidProjectForPlugin()
603
679
}
604
680
}
605
681
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
+
606
716
private static void SetupIOSProjectForPlugin ( )
607
717
{
608
718
var iosRunnerPath = Path . GetFullPath ( Path . Combine ( ProjectPath , "../../ios" ) ) ;
0 commit comments