Skip to content

Commit 8dab4d3

Browse files
committed
Update docs and build.cs to handle Kotlin .gradle.kts files.
1 parent 3effa7c commit 8dab4d3

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ This requires a flutter_unity_widget version that is newer than 2022.2.1.
194194

195195
```diff
196196
dependencies {
197+
// build.gradle
197198
+ implementation project(':flutter_unity_widget')
199+
// build.gradle.kts (Flutter 3.29+)
200+
+ implementation(project(":flutter_unity_widget"))
198201
}
199202
```
200203
- 3.2. Edit your android MainActivity file.
@@ -241,15 +244,23 @@ But if you want to manually set up the changes made by the export, continue.
241244
5. Open the *android/settings.gradle* file and change the following:
242245

243246
```diff
247+
// build.gradle
244248
+ include ":unityLibrary"
245249
+ project(":unityLibrary").projectDir = file("./unityLibrary")
250+
251+
// build.gradle.kts (Flutter 3.29+)
252+
+ include(":unityLibrary")
253+
+ project(":unityLibrary").projectDir = file("./unityLibrary")
246254
```
247255

248256
6. Open the *android/app/build.gradle* file and change the following:
249257

250258
```diff
251259
dependencies {
260+
// app/build.gradle
252261
+ implementation project(':unityLibrary')
262+
// app/build.gradle.kts (Flutter 3.29+)
263+
+ implementation(project(":unityLibrary"))
253264
}
254265
```
255266

@@ -259,7 +270,10 @@ But if you want to manually set up the changes made by the export, continue.
259270
allprojects {
260271
repositories {
261272
+ flatDir {
273+
// build.gradle
262274
+ dirs "${project(':unityLibrary').projectDir}/libs"
275+
// build.gradle.kts (Flutter 3.29+)
276+
+ dirs(file("${project(":unityLibrary").projectDir}/libs"))
263277
+ }
264278
google()
265279
mavenCentral()
@@ -401,7 +415,11 @@ allprojects {
401415
3. If your `XR Plugin Management` plugin is version 4.4 or higher, Unity also exports the xrmanifest.androidlib folder.
402416
Make sure to include it by adding the following line to `android/settings.gradle`
403417
```
418+
// settings.gradle
404419
include ":unityLibrary:xrmanifest.androidlib"
420+
421+
// settings.gradle.kts (Flutter 3.29+)
422+
include(":unityLibrary:xrmanifest.androidlib")
405423
```
406424
4. With some Unity versions AR might crash at runtine with an error like:
407425
`java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class`.

example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)