Skip to content

Commit fef61dc

Browse files
committed
Merge branch 'master' into experimental/unity_6000
2 parents dd52805 + 8dab4d3 commit fef61dc

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

+18
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

+110
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)