Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f68ab8a

Browse files
Fix issues with copying Android files from Packages (#709)
* Fix issues with copying Android files from Packages * Update CHANGELOG.md
1 parent 12cca2d commit f68ab8a

File tree

3 files changed

+79
-37
lines changed

3 files changed

+79
-37
lines changed

‎CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Upcoming
2+
* Android Resolver - Handle package paths that don't include a version hash,
3+
which is no longer present with Unity 6. Fixes #697
4+
* Android Resolver - Handle packages referenced using local file paths.
5+
Fixes #701
6+
17
# Version 1.2.182 - Aug 2, 2024
28
* General - Check for gradle version instead of Unity version when determining
39
the template files to modify.

‎source/AndroidResolver/src/GradleTemplateResolver.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ public static bool UnityChangeMavenRepoInSettingsTemplate {
170170
}
171171
}
172172

173+
/// <summary>
174+
/// Modifies the given path such that the m2repository is placed into the
175+
/// LocalMavenRepoDir/PrefixDirectory/m2repository/...
176+
/// </summary>
177+
/// <param name="path">The original path to modify.</param>
178+
/// <returns>A modified path if m2repository is found, the same path otherwise.</returns>
179+
private static string ReplaceLocalFolderBasedOnM2repo(string path) {
180+
string regexPattern = @"^(.*[/\\])([^/\\]+[/\\]m2repository.*)$";
181+
Match match = Regex.Match(path, regexPattern);
182+
if (match.Success) {
183+
path = Path.Combine(GooglePlayServices.SettingsDialog.LocalMavenRepoDir,
184+
match.Groups[2].Value);
185+
}
186+
return path;
187+
}
188+
173189
/// <summary>
174190
/// Copy srcaar files to aar files that are excluded from Unity's build process.
175191
/// </summary>
@@ -198,6 +214,15 @@ private static bool CopySrcAars(ICollection<Dependency> dependencies) {
198214
var dir = FileUtils.ReplaceBaseAssetsOrPackagesFolder(
199215
Path.GetDirectoryName(aar),
200216
GooglePlayServices.SettingsDialog.LocalMavenRepoDir);
217+
218+
if (!dir.StartsWith(GooglePlayServices.SettingsDialog.LocalMavenRepoDir)) {
219+
// The directory replace logic failed, likely because the original aar
220+
// is not located under the Assets or Packages folders.
221+
// Try to come up with a sensible destination folder by searching for
222+
// an m2repository within the path, and using that.
223+
dir = ReplaceLocalFolderBasedOnM2repo(Path.GetDirectoryName(aar));
224+
}
225+
201226
var filename = Path.GetFileNameWithoutExtension(aarPath);
202227
var targetFilename = Path.Combine(dir, filename + ".aar");
203228

@@ -700,15 +725,18 @@ internal static IList<string> GradleMavenReposLinesFromDependencies(
700725
if (repoAndSources.Key.StartsWith(projectFileUri)) {
701726
var relativePath = repoAndSources.Key.Substring(projectFileUri.Length + 1);
702727
// Convert "Assets", "Packages/packageid", or
703-
// "Library/PackageCache/packageid@version" prefix to local maven repo
704-
// path. Note that local maven repo path only exists if the original repo
705-
// path contains .srcaar.
706-
var repoPath = FileUtils.PosixPathSeparators(
707-
FileUtils.ReplaceBaseAssetsOrPackagesFolder(
708-
relativePath, GooglePlayServices.SettingsDialog.LocalMavenRepoDir));
728+
// "Library/PackageCache/packageid@version" prefix (@version optional) to local
729+
// maven repo path. Note that local maven repo path only exists if the original
730+
// repo path contains .srcaar.
731+
var repoPath = FileUtils.ReplaceBaseAssetsOrPackagesFolder(
732+
relativePath, GooglePlayServices.SettingsDialog.LocalMavenRepoDir);
733+
// We also want to just convert any prefixes before a directory/m2repository, since
734+
// they are copied to the LocalMavenRepoDir as well.
735+
repoPath = ReplaceLocalFolderBasedOnM2repo(repoPath);
709736
if (!Directory.Exists(repoPath)) {
710737
repoPath = relativePath;
711738
}
739+
repoPath = FileUtils.PosixPathSeparators(repoPath);
712740

713741
if (useFullPath) {
714742
// build.gradle expects file:/// URI so file separator will be "/" in anycase

‎source/VersionHandlerImpl/src/FileUtils.cs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ internal class FileUtils {
4848

4949
/// <summary>
5050
/// Regex to match packages folder like "Library/PackageCache/com.company.pkg"
51+
/// or "Library/PackageCache/com.company.pkg@version"
5152
/// </summary>
5253
private static Regex PACKAGES_PHYSICAL_PATH_REGEX =
53-
new Regex(@"^(Library[/\\]PackageCache[/\\])([^/\\]+)(@[^/\\]+)[/\\](.*)?$");
54+
new Regex(@"^(Library[/\\]PackageCache[/\\])([^/\\]+)(@[^/\\]+)?[/\\](.*)?$");
5455

5556
/// <summary>
5657
/// Returns the project directory (e.g contains the Assets folder).
@@ -448,7 +449,9 @@ public static string GetPackageDirectory(
448449
// work if the package is installed from a local tarball or from a registry
449450
// server.
450451
string absolutePath = Path.GetFullPath(packageDir);
451-
packageDir = absolutePath.Substring(ProjectDirectory.Length + 1);
452+
if (absolutePath.StartsWith(ProjectDirectory)) {
453+
packageDir = absolutePath.Substring(ProjectDirectory.Length + 1);
454+
}
452455
}
453456
} else {
454457
nameMatch = PACKAGES_PHYSICAL_PATH_REGEX.Match(path);
@@ -640,42 +643,47 @@ internal static bool IsValidGuid(string guidStr) {
640643
/// <param name="path">Path to the file/directory that needs checking.</param>
641644
/// <returns>True if all folders are created successfully.</returns>
642645
public static bool CreateFolder(string path, Google.Logger logger = null) {
643-
if (AssetDatabase.IsValidFolder(path)) {
644-
return true;
645-
}
646-
DirectoryInfo di = new DirectoryInfo(path);
647-
var parentFolder = Path.GetDirectoryName(path);
648-
if (!CreateFolder(parentFolder)) {
649-
return false;
650-
}
646+
try {
647+
if (AssetDatabase.IsValidFolder(path)) {
648+
return true;
649+
}
650+
DirectoryInfo di = new DirectoryInfo(path);
651+
var parentFolder = Path.GetDirectoryName(path);
652+
if (!CreateFolder(parentFolder)) {
653+
return false;
654+
}
651655

652-
// Try to use Unity API to create folder. However, some versions of Unity has issue to
653-
// create folders with version number in it like '9.0.0'. In this case, instead of
654-
// returnig empty guid, it can return guids with all zeroes.
655-
if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) {
656-
return true;
657-
}
656+
// Try to use Unity API to create folder. However, some versions of Unity has issue to
657+
// create folders with version number in it like '9.0.0'. In this case, instead of
658+
// returning empty guid, it can return guids with all zeroes.
659+
if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) {
660+
return true;
661+
}
658662

659-
if (logger != null) {
660-
logger.Log(
661-
String.Format(
662-
"Please ignore Unity error messages similar to '{0}'.\n" +
663-
"Unable to use Unity API `AssetDatabase.CreateFolder()` to " +
664-
"create folder: '{1}'. Switch to use `Directory.CreateDirectory()` " +
665-
"instead. \n\n" +
666-
"See {2} for more information.",
667-
"*** is not a valid directory name.",
668-
path,
669-
"https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-7046"),
670-
LogLevel.Info);
671-
}
663+
if (logger != null) {
664+
logger.Log(
665+
String.Format(
666+
"Please ignore Unity error messages similar to '{0}'.\n" +
667+
"Unable to use Unity API `AssetDatabase.CreateFolder()` to " +
668+
"create folder: '{1}'. Switch to use `Directory.CreateDirectory()` " +
669+
"instead. \n\n" +
670+
"See {2} for more information.",
671+
"*** is not a valid directory name.",
672+
path,
673+
"https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-7046"),
674+
LogLevel.Info);
675+
}
672676

673-
return Directory.CreateDirectory(path) != null;
677+
return Directory.CreateDirectory(path) != null;
678+
} catch (Exception ex) {
679+
logger.Log("Exception thrown trying to CreateFolder. " + ex, LogLevel.Error);
680+
return false;
681+
}
674682
}
675683

676684
/// <summary>
677685
/// Replace "Assets/", "Packages/package-id", or "Library/PackageCache/package-id@version"
678-
/// base in the path with the new base.
686+
/// base (@version optional) in the path with the new base.
679687
/// </summary>
680688
/// <param name="path">Path to the file/directory to be modified.</param>
681689
/// <param name="newBase">New base used to replace the given path.</param>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /