Skip to content

Fix issues with copying Android files from Packages #709

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

Merged
merged 2 commits into from
Sep 17, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Upcoming
* Android Resolver - Handle package paths that don't include a version hash,
which is no longer present with Unity 6. Fixes #697
* Android Resolver - Handle packages referenced using local file paths.
Fixes #701

# Version 1.2.182 - Aug 2, 2024
* General - Check for gradle version instead of Unity version when determining
the template files to modify.
Expand Down
40 changes: 34 additions & 6 deletions source/AndroidResolver/src/GradleTemplateResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ public static bool UnityChangeMavenRepoInSettingsTemplate {
}
}

/// <summary>
/// Modifies the given path such that the m2repository is placed into the
/// LocalMavenRepoDir/PrefixDirectory/m2repository/...
/// </summary>
/// <param name="path">The original path to modify.</param>
/// <returns>A modified path if m2repository is found, the same path otherwise.</returns>
private static string ReplaceLocalFolderBasedOnM2repo(string path) {
string regexPattern = @"^(.*[/\\])([^/\\]+[/\\]m2repository.*)$";
Match match = Regex.Match(path, regexPattern);
if (match.Success) {
path = Path.Combine(GooglePlayServices.SettingsDialog.LocalMavenRepoDir,
match.Groups[2].Value);
}
return path;
}

/// <summary>
/// Copy srcaar files to aar files that are excluded from Unity's build process.
/// </summary>
Expand Down Expand Up @@ -198,6 +214,15 @@ private static bool CopySrcAars(ICollection<Dependency> dependencies) {
var dir = FileUtils.ReplaceBaseAssetsOrPackagesFolder(
Path.GetDirectoryName(aar),
GooglePlayServices.SettingsDialog.LocalMavenRepoDir);

if (!dir.StartsWith(GooglePlayServices.SettingsDialog.LocalMavenRepoDir)) {
// The directory replace logic failed, likely because the original aar
// is not located under the Assets or Packages folders.
// Try to come up with a sensible destination folder by searching for
// an m2repository within the path, and using that.
dir = ReplaceLocalFolderBasedOnM2repo(Path.GetDirectoryName(aar));
}

var filename = Path.GetFileNameWithoutExtension(aarPath);
var targetFilename = Path.Combine(dir, filename + ".aar");

Expand Down Expand Up @@ -700,15 +725,18 @@ internal static IList<string> GradleMavenReposLinesFromDependencies(
if (repoAndSources.Key.StartsWith(projectFileUri)) {
var relativePath = repoAndSources.Key.Substring(projectFileUri.Length + 1);
// Convert "Assets", "Packages/packageid", or
// "Library/PackageCache/packageid@version" prefix to local maven repo
// path. Note that local maven repo path only exists if the original repo
// path contains .srcaar.
var repoPath = FileUtils.PosixPathSeparators(
FileUtils.ReplaceBaseAssetsOrPackagesFolder(
relativePath, GooglePlayServices.SettingsDialog.LocalMavenRepoDir));
// "Library/PackageCache/packageid@version" prefix (@version optional) to local
// maven repo path. Note that local maven repo path only exists if the original
// repo path contains .srcaar.
var repoPath = FileUtils.ReplaceBaseAssetsOrPackagesFolder(
relativePath, GooglePlayServices.SettingsDialog.LocalMavenRepoDir);
// We also want to just convert any prefixes before a directory/m2repository, since
// they are copied to the LocalMavenRepoDir as well.
repoPath = ReplaceLocalFolderBasedOnM2repo(repoPath);
if (!Directory.Exists(repoPath)) {
repoPath = relativePath;
}
repoPath = FileUtils.PosixPathSeparators(repoPath);

if (useFullPath) {
// build.gradle expects file:/// URI so file separator will be "/" in anycase
Expand Down
70 changes: 39 additions & 31 deletions source/VersionHandlerImpl/src/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ internal class FileUtils {

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

/// <summary>
/// Returns the project directory (e.g contains the Assets folder).
Expand Down Expand Up @@ -448,7 +449,9 @@ public static string GetPackageDirectory(
// work if the package is installed from a local tarball or from a registry
// server.
string absolutePath = Path.GetFullPath(packageDir);
packageDir = absolutePath.Substring(ProjectDirectory.Length + 1);
if (absolutePath.StartsWith(ProjectDirectory)) {
packageDir = absolutePath.Substring(ProjectDirectory.Length + 1);
}
}
} else {
nameMatch = PACKAGES_PHYSICAL_PATH_REGEX.Match(path);
Expand Down Expand Up @@ -640,42 +643,47 @@ internal static bool IsValidGuid(string guidStr) {
/// <param name="path">Path to the file/directory that needs checking.</param>
/// <returns>True if all folders are created successfully.</returns>
public static bool CreateFolder(string path, Google.Logger logger = null) {
if (AssetDatabase.IsValidFolder(path)) {
return true;
}
DirectoryInfo di = new DirectoryInfo(path);
var parentFolder = Path.GetDirectoryName(path);
if (!CreateFolder(parentFolder)) {
return false;
}
try {
if (AssetDatabase.IsValidFolder(path)) {
return true;
}
DirectoryInfo di = new DirectoryInfo(path);
var parentFolder = Path.GetDirectoryName(path);
if (!CreateFolder(parentFolder)) {
return false;
}

// Try to use Unity API to create folder. However, some versions of Unity has issue to
// create folders with version number in it like '9.0.0'. In this case, instead of
// returnig empty guid, it can return guids with all zeroes.
if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) {
return true;
}
// Try to use Unity API to create folder. However, some versions of Unity has issue to
// create folders with version number in it like '9.0.0'. In this case, instead of
// returning empty guid, it can return guids with all zeroes.
if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) {
return true;
}

if (logger != null) {
logger.Log(
String.Format(
"Please ignore Unity error messages similar to '{0}'.\n" +
"Unable to use Unity API `AssetDatabase.CreateFolder()` to " +
"create folder: '{1}'. Switch to use `Directory.CreateDirectory()` " +
"instead. \n\n" +
"See {2} for more information.",
"*** is not a valid directory name.",
path,
"https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-7046"),
LogLevel.Info);
}
if (logger != null) {
logger.Log(
String.Format(
"Please ignore Unity error messages similar to '{0}'.\n" +
"Unable to use Unity API `AssetDatabase.CreateFolder()` to " +
"create folder: '{1}'. Switch to use `Directory.CreateDirectory()` " +
"instead. \n\n" +
"See {2} for more information.",
"*** is not a valid directory name.",
path,
"https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-7046"),
LogLevel.Info);
}

return Directory.CreateDirectory(path) != null;
return Directory.CreateDirectory(path) != null;
} catch (Exception ex) {
logger.Log("Exception thrown trying to CreateFolder. " + ex, LogLevel.Error);
return false;
}
}

/// <summary>
/// Replace "Assets/", "Packages/package-id", or "Library/PackageCache/package-id@version"
/// base in the path with the new base.
/// base (@version optional) in the path with the new base.
/// </summary>
/// <param name="path">Path to the file/directory to be modified.</param>
/// <param name="newBase">New base used to replace the given path.</param>
Expand Down
Loading