Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flutter 3.29] Update docs and build.cs to handle Kotlin .gradle.kts files. #1004

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ This requires a flutter_unity_widget version that is newer than 2022.2.1.

```diff
dependencies {
// build.gradle
+ implementation project(':flutter_unity_widget')
// build.gradle.kts (Flutter 3.29+)
+ implementation(project(":flutter_unity_widget"))
}
```
- 3.2. Edit your android MainActivity file.
Expand Down Expand Up @@ -241,15 +244,23 @@ But if you want to manually set up the changes made by the export, continue.
5. Open the *android/settings.gradle* file and change the following:

```diff
// build.gradle
+ include ":unityLibrary"
+ project(":unityLibrary").projectDir = file("./unityLibrary")

// build.gradle.kts (Flutter 3.29+)
+ include(":unityLibrary")
+ project(":unityLibrary").projectDir = file("./unityLibrary")
```

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

```diff
dependencies {
// app/build.gradle
+ implementation project(':unityLibrary')
// app/build.gradle.kts (Flutter 3.29+)
+ implementation(project(":unityLibrary"))
}
```

Expand All @@ -259,7 +270,10 @@ But if you want to manually set up the changes made by the export, continue.
allprojects {
repositories {
+ flatDir {
// build.gradle
+ dirs "${project(':unityLibrary').projectDir}/libs"
// build.gradle.kts (Flutter 3.29+)
+ dirs(file("${project(":unityLibrary").projectDir}/libs"))
+ }
google()
mavenCentral()
Expand Down Expand Up @@ -401,7 +415,11 @@ allprojects {
3. If your `XR Plugin Management` plugin is version 4.4 or higher, Unity also exports the xrmanifest.androidlib folder.
Make sure to include it by adding the following line to `android/settings.gradle`
```
// settings.gradle
include ":unityLibrary:xrmanifest.androidlib"

// settings.gradle.kts (Flutter 3.29+)
include(":unityLibrary:xrmanifest.androidlib")
```
4. With some Unity versions AR might crash at runtine with an error like:
`java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class`.
Expand Down
110 changes: 110 additions & 0 deletions example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ private static void SetupAndroidProject()
var appBuildPath = Path.Combine(androidAppPath, "build.gradle");
var settingsPath = Path.Combine(androidPath, "settings.gradle");

// switch to Kotlin DSL gradle if .kts file is detected (Fluter 3.29+ by default)
if (File.Exists(projBuildPath + ".kts")) {
SetupAndroidProjectKotlin();
return;
}

var projBuildScript = File.ReadAllText(projBuildPath);
var settingsScript = File.ReadAllText(settingsPath);
var appBuildScript = File.ReadAllText(appBuildPath);
Expand Down Expand Up @@ -567,6 +573,71 @@ implementation project(':unityLibrary')
}
}


// Copy of SetupAndroidProject() adapted to Kotlin DLS .gradle.kts. Generated since Flutter 3.29
private static void SetupAndroidProjectKotlin()
{
var androidPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android"));
var androidAppPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android/app"));
var projBuildPath = Path.Combine(androidPath, "build.gradle.kts");
var appBuildPath = Path.Combine(androidAppPath, "build.gradle.kts");
var settingsPath = Path.Combine(androidPath, "settings.gradle.kts");


var projBuildScript = File.ReadAllText(projBuildPath);
var settingsScript = File.ReadAllText(settingsPath);
var appBuildScript = File.ReadAllText(appBuildPath);

// Sets up the project build.gradle files correctly
if (!Regex.IsMatch(projBuildScript, @"flatDir[^/]*[^}]*}"))
{
var regex = new Regex(@"allprojects \{[^\{]*\{", RegexOptions.Multiline);
projBuildScript = regex.Replace(projBuildScript, @"
allprojects {
repositories {
flatDir {
dirs(file(""${project("":unityLibrary"").projectDir}/libs""))
}
");
File.WriteAllText(projBuildPath, projBuildScript);
}

// Sets up the project settings.gradle files correctly
if (!Regex.IsMatch(settingsScript, @"include("":unityLibrary"")"))
{
settingsScript += @"

include("":unityLibrary"")
project("":unityLibrary"").projectDir = file(""./unityLibrary"")
";
File.WriteAllText(settingsPath, settingsScript);
}


// Sets up the project app build.gradle files correctly
if (!Regex.IsMatch(appBuildScript, @"dependencies \{"))
{
appBuildScript += @"
dependencies {
implementation(project("":unityLibrary""))
}
";
File.WriteAllText(appBuildPath, appBuildScript);
}
else
{
if (!appBuildScript.Contains(@"implementation(project("":unityLibrary"")"))
{
var regex = new Regex(@"dependencies \{", RegexOptions.Multiline);
appBuildScript = regex.Replace(appBuildScript, @"
dependencies {
implementation(project("":unityLibrary""))
");
File.WriteAllText(appBuildPath, appBuildScript);
}
}
}

/// <summary>
/// This method tries to autome the build setup required for Android
/// </summary>
Expand All @@ -576,6 +647,11 @@ private static void SetupAndroidProjectForPlugin()
var projBuildPath = Path.Combine(androidPath, "build.gradle");
var settingsPath = Path.Combine(androidPath, "settings.gradle");

if (File.Exists(projBuildPath + ".kts")) {
SetupAndroidProjectForPluginKotlin();
return;
}

var projBuildScript = File.ReadAllText(projBuildPath);
var settingsScript = File.ReadAllText(settingsPath);

Expand Down Expand Up @@ -603,6 +679,40 @@ private static void SetupAndroidProjectForPlugin()
}
}

// Copy of SetupAndroidProjectForPlugin() adapted to Kotlin DLS .gradle.kts. Generated since Flutter 3.29
private static void SetupAndroidProjectForPluginKotlin()
{
var androidPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android"));
var projBuildPath = Path.Combine(androidPath, "build.gradle.kts");
var settingsPath = Path.Combine(androidPath, "settings.gradle.kts");

var projBuildScript = File.ReadAllText(projBuildPath);
var settingsScript = File.ReadAllText(settingsPath);

// Sets up the project build.gradle files correctly
if (Regex.IsMatch(projBuildScript, @"// BUILD_ADD_UNITY_LIBS"))
{
var regex = new Regex(@"// BUILD_ADD_UNITY_LIBS", RegexOptions.Multiline);
projBuildScript = regex.Replace(projBuildScript, @"
flatDir {
dirs(file(""${project("":unityLibrary"").projectDir}/libs""))
}
");
File.WriteAllText(projBuildPath, projBuildScript);
}

// Sets up the project settings.gradle files correctly
if (!Regex.IsMatch(settingsScript, @"include("":unityLibrary"")"))
{
settingsScript += @"

include("":unityLibrary"")
project("":unityLibrary"").projectDir = file(""./unityLibrary"")
";
File.WriteAllText(settingsPath, settingsScript);
}
}

private static void SetupIOSProjectForPlugin()
{
var iosRunnerPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../ios"));
Expand Down