Skip to content

Commit

Permalink
fix: Fixed Failed to find repositories on 2022.3.0f1 builds (#1354)
Browse files Browse the repository at this point in the history
* fixes new gradle project in 2022

* Format code

* Updated CHANGELOG.md

* updated for clear & tests

* updates

* fake test project

---------

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
  • Loading branch information
bitsandfoxes and getsentry-bot authored Jun 5, 2023
1 parent a3093a2 commit f751732
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Fixes Android native integration in the gradle output project for builds with Unity 2022.3 and newer ([#1354](https://github.com/getsentry/sentry-unity/pull/1354))

### Dependencies

- Bump Native SDK from v0.6.2 to v0.6.3 ([#1349](https://github.com/getsentry/sentry-unity/pull/1349))
Expand Down
48 changes: 35 additions & 13 deletions src/Sentry.Unity.Editor/Android/GradleSetup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEditor.Build;

namespace Sentry.Unity.Editor.Android
Expand All @@ -14,38 +16,58 @@ internal class GradleSetup
public const string RepositoryScopeName = "repositories";
public const string SdkDependencies = "implementation ('io.sentry:sentry-android:+') { exclude group: 'androidx.core' exclude group: 'androidx.lifecycle' }";
public const string DependencyScopeName = "dependencies";
public static readonly List<string> ScopesToSkip = new() { "buildscript", "pluginManagement" };

private readonly string _rootGradle;
private readonly string _settingsGradle;
private readonly string _unityLibraryGradle;

public GradleSetup(IDiagnosticLogger logger, string gradleProjectPath)
{
_logger = logger;
_rootGradle = Path.Combine(gradleProjectPath, "build.gradle");
_settingsGradle = Path.Combine(gradleProjectPath, "settings.gradle");
_unityLibraryGradle = Path.Combine(gradleProjectPath, "unityLibrary", "build.gradle");
}

public void UpdateGradleProject()
public void UpdateGradleProject(IApplication? application = null)
{
_logger.LogInfo("Modifying the 'build.gradle' file at {0}", _rootGradle);
var rootGradleContent = LoadGradleScript(_rootGradle);
rootGradleContent = InsertIntoScope(rootGradleContent, RepositoryScopeName, LocalRepository);
File.WriteAllText(_rootGradle, rootGradleContent);
_logger.LogInfo("Adding Sentry to the gradle project.");

_logger.LogInfo("Modifying the 'build.gradle' file at {0}", _unityLibraryGradle);
// Starting with 2022.3.0f1 the root build.gradle updated to use the "new" way of importing plugins via `id`
// Instead, dependency repositories get handled in the `settings.gradle` at the root
var gradleFilePath = SentryUnityVersion.IsNewerOrEqualThan("2022.3", application)
? _settingsGradle
: _rootGradle;

_logger.LogDebug("Updating the gradle file at '{0}'", gradleFilePath);

var gradleContent = LoadGradleScript(gradleFilePath);
gradleContent = InsertIntoScope(gradleContent, RepositoryScopeName, LocalRepository);
File.WriteAllText(gradleFilePath, gradleContent);

_logger.LogDebug("Updating the gradle file at '{0}'", _unityLibraryGradle);
var unityLibraryGradleContent = LoadGradleScript(_unityLibraryGradle);
unityLibraryGradleContent = InsertIntoScope(unityLibraryGradleContent, DependencyScopeName, SdkDependencies);
File.WriteAllText(_unityLibraryGradle, unityLibraryGradleContent);
}

public void ClearGradleProject()
public void ClearGradleProject(IApplication? application = null)
{
_logger.LogInfo("Removing modifications from the 'build.gradle' file at {0}", _rootGradle);
var rootGradleContent = LoadGradleScript(_rootGradle);
rootGradleContent = RemoveFromGradleContent(rootGradleContent, LocalRepository);
File.WriteAllText(_rootGradle, rootGradleContent);
_logger.LogInfo("Removing Sentry from the gradle project.");

// Starting with 2022.3.0f1 the root build.gradle updated to use the "new" way of importing plugins via `id`
// Instead, dependency repositories get handled in the `settings.gradle` at the root
var gradleFilePath = SentryUnityVersion.IsNewerOrEqualThan("2022.3", application)
? _settingsGradle
: _rootGradle;

_logger.LogDebug("Removing modifications from '{0}'", gradleFilePath);
var gradleContent = LoadGradleScript(gradleFilePath);
gradleContent = RemoveFromGradleContent(gradleContent, LocalRepository);
File.WriteAllText(gradleFilePath, gradleContent);

_logger.LogInfo("Removing modifications from the 'build.gradle' file at {0}", _unityLibraryGradle);
_logger.LogDebug("Removing modifications from the 'build.gradle' file at {0}", _unityLibraryGradle);
var unityLibraryGradleContent = LoadGradleScript(_unityLibraryGradle);
unityLibraryGradleContent = RemoveFromGradleContent(unityLibraryGradleContent, SdkDependencies);
File.WriteAllText(_unityLibraryGradle, unityLibraryGradleContent);
Expand All @@ -66,7 +88,7 @@ internal string InsertIntoScope(string gradleContent, string scope, string inser
{
var line = lines[i];
// There are potentially multiple, nested scopes. We cannot add ourselves to the ones within 'buildscript'
if (line.Contains("buildscript"))
if (ScopesToSkip.Any(line.Contains))
{
var startIndex = i;

Expand Down
36 changes: 25 additions & 11 deletions test/Sentry.Unity.Editor.Tests/Android/GradleSetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Reflection;
using NUnit.Framework;
using Sentry.Unity.Editor.Android;
using Sentry.Unity.Integrations;
using Sentry.Unity.Tests.SharedClasses;
using Sentry.Unity.Tests.Stubs;

namespace Sentry.Unity.Editor.Tests.Android
{
Expand Down Expand Up @@ -42,13 +44,17 @@ public void LoadGradleScript_FileNotFound_ThrowsFileNotFoundException()
}

[Test]
public void UpdateGradleProject_ModifiesGradleFiles()
[TestCase("2019.3", "build.gradle")]
[TestCase("2020.3", "build.gradle")]
[TestCase("2021.3", "build.gradle")]
[TestCase("2022.3", "settings.gradle")]
public void UpdateGradleProject_ModifiesGradleFiles(string unityVersion, string rootGradleFileName)
{
var sut = new GradleSetup(Logger, GradleProjectPath);

sut.UpdateGradleProject();
sut.UpdateGradleProject(new TestApplication(unityVersion: unityVersion));

var rootGradleFilePath = Path.Combine(GradleProjectPath, "build.gradle");
var rootGradleFilePath = Path.Combine(GradleProjectPath, rootGradleFileName);
var rootGradleContent = File.ReadAllText(rootGradleFilePath);
StringAssert.Contains(GradleSetup.LocalRepository, rootGradleContent);

Expand All @@ -58,14 +64,18 @@ public void UpdateGradleProject_ModifiesGradleFiles()
}

[Test]
public void UpdateGradleProject_GradleAlreadyModified_LogsAndReturns()
[TestCase("2019.3", "build.gradle")]
[TestCase("2020.3", "build.gradle")]
[TestCase("2021.3", "build.gradle")]
[TestCase("2022.3", "settings.gradle")]
public void UpdateGradleProject_GradleAlreadyModified_LogsAndReturns(string unityVersion, string rootGradleFileName)
{
var sut = new GradleSetup(Logger, GradleProjectPath);
sut.UpdateGradleProject();
sut.UpdateGradleProject(new TestApplication(unityVersion: unityVersion));

sut.UpdateGradleProject();
sut.UpdateGradleProject(new TestApplication(unityVersion: unityVersion));

var rootGradleFilePath = Path.Combine(GradleProjectPath, "build.gradle");
var rootGradleFilePath = Path.Combine(GradleProjectPath, rootGradleFileName);
var rootGradleContent = File.ReadAllText(rootGradleFilePath);
StringAssert.Contains(GradleSetup.LocalRepository, rootGradleContent); // Sanity check

Expand All @@ -79,20 +89,24 @@ public void UpdateGradleProject_GradleAlreadyModified_LogsAndReturns()
}

[Test]
public void ClearGradleProject_GradleFilesModified_RemovesModification()
[TestCase("2019.3", "build.gradle")]
[TestCase("2020.3", "build.gradle")]
[TestCase("2021.3", "build.gradle")]
[TestCase("2022.3", "settings.gradle")]
public void ClearGradleProject_GradleFilesModified_RemovesModification(string unityVersion, string rootGradleFileName)
{
var sut = new GradleSetup(Logger, GradleProjectPath);
sut.UpdateGradleProject();
sut.UpdateGradleProject(new TestApplication(unityVersion: unityVersion));

var rootGradleFilePath = Path.Combine(GradleProjectPath, "build.gradle");
var rootGradleFilePath = Path.Combine(GradleProjectPath, rootGradleFileName);
var rootGradleContent = File.ReadAllText(rootGradleFilePath);
StringAssert.Contains(GradleSetup.LocalRepository, rootGradleContent); // Sanity check

var unityLibraryGradleFilePath = Path.Combine(GradleProjectPath, "unityLibrary", "build.gradle");
var unityLibraryGradleContent = File.ReadAllText(unityLibraryGradleFilePath);
StringAssert.Contains(GradleSetup.SdkDependencies, unityLibraryGradleContent); // Sanity check

sut.ClearGradleProject();
sut.ClearGradleProject(new TestApplication(unityVersion: unityVersion));

rootGradleContent = File.ReadAllText(rootGradleFilePath);
StringAssert.DoesNotContain(GradleSetup.LocalRepository, rootGradleContent); // Sanity check
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repositories {
google()
jcenter()
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
}

0 comments on commit f751732

Please sign in to comment.