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

Bug with AndroidPostBuildProcessor on v5.0.0 #144

Open
jzapdot opened this issue Feb 5, 2025 · 3 comments
Open

Bug with AndroidPostBuildProcessor on v5.0.0 #144

jzapdot opened this issue Feb 5, 2025 · 3 comments
Assignees

Comments

@jzapdot
Copy link

jzapdot commented Feb 5, 2025

The AndroidPostBuildProcessor throws an IOException when building for .aab or .apk Android projects as the destination path does not exist. It appears that processor is intended to copy information to nested files in the Android studio project, but the destination directories/files won't exist for .aab or .apk targets.

Offending code

private static void AndroidPostProcess(string path)
{
    string androidProjectPath = path + "/unityLibrary/clevertap-android-wrapper.androidlib";

    // copy assets to the android project
    EditorUtils.DirectoryCopy(Path.Combine(Application.dataPath, EditorUtils.CLEVERTAP_ASSETS_FOLDER),
        Path.Combine(androidProjectPath, $"assets/{EditorUtils.CLEVERTAP_APP_ASSETS_FOLDER}"),
        true, true, new System.Collections.Generic.HashSet<string>() { EditorUtils.CLEVERTAP_CUSTOM_TEMPLATES_FOLDER });
    // copy CleverTapSettings to the project's AndroidManifest
    CopySettingsToAndroidManifest(androidProjectPath);
}

I fixed this locally with a workaround by checking the build path and early returning if the build path ends in .aab or .apk.

Fixed code

private static void AndroidPostProcess(string path)
{
  if (path.EndsWith(".aab") || path.EndsWith(".apk"))
  {
    Debug.Log($"Skipping modifying AndroidStudio project as the build path: [{path}] does not indicate " +
              $"an AndroidStudio project.");
    return;
  }

    var androidProjectPath = path + "/unityLibrary/clevertap-android-wrapper.androidlib";

    // copy assets to the android project
    var srcPath = Path.Combine(Application.dataPath, EditorUtils.CLEVERTAP_ASSETS_FOLDER);
    var destPath = Path.Combine(androidProjectPath, $"assets/{EditorUtils.CLEVERTAP_APP_ASSETS_FOLDER}");

    // If this directory does not exist, return.
    if (!Directory.Exists(destPath))
    {
      Debug.LogWarning($"Could not find AndroidStudio project at [{destPath}] to copy files to.");
      return;
    }

    var subdirectories = new System.Collections.Generic.HashSet<string>
    {
      EditorUtils.CLEVERTAP_CUSTOM_TEMPLATES_FOLDER
    };

    EditorUtils.DirectoryCopy(
      srcPath,
      destPath,
      copyChangedOnly: true,
      copySubDirs: true,
      includeOnlySubDirsNamed: subdirectories);

    // copy CleverTapSettings to the project's AndroidManifest
    CopySettingsToAndroidManifest(androidProjectPath);
}
@vasct
Copy link
Contributor

vasct commented Feb 6, 2025

Hi @jzapdot. Thank you for raising this issue.

Can you include the full console output of the exception (with the full stack trace) and the exact steps you take before it appears ? Do you overwrite an existing apk/aar file when building or creating a new one ?

@jzapdot
Copy link
Author

jzapdot commented Feb 6, 2025

Hi @jzapdot. Thank you for raising this issue.

Can you include the full console output of the exception (with the full stack trace) and the exact steps you take before it appears ? Do you overwrite an existing apk/aar file when building or creating a new one ?

Hi @vasct ,

The issue is that the copy destPath in this case only works if the output project is an AndroidStudio project. If the output Android build is an AAB file, then there is not a folder directory path like destPath to copy to. Its just a single file.

The result is the same whether there is an existing AAB file or not.

StackTrace

IOException: The file '/Users/jcampbell/Repositories/terc-zoombinis-dev/Zoombinis/Builds/Android/Zoombinis.aab' already exists.
System.IO.FileSystem.CreateDirectory (System.String fullPath) (at <4cd4c67de2d54e76993cd773cd169e46>:0)
System.IO.Directory.CreateDirectory (System.String path) (at <4cd4c67de2d54e76993cd773cd169e46>:0)
CleverTapSDK.Private.EditorUtils.DirectoryCopy (System.String sourceDirName, System.String destDirName, System.Boolean copyChangedOnly, System.Boolean copySubDirs, System.Collections.Generic.HashSet`1[T] includeOnlySubDirsNamed) (at Assets/CleverTap/Editor/EditorUtils.cs:25)
CleverTapSDK.Private.AndroidPostBuildProcessor.AndroidPostProcess (System.String path) (at Assets/CleverTap/Editor/AndroidPostBuildProcessor.cs:28)
CleverTapSDK.Private.AndroidPostBuildProcessor.OnPostprocessBuild (UnityEditor.BuildTarget target, System.String path) (at Assets/CleverTap/Editor/AndroidPostBuildProcessor.cs:19)
System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <4cd4c67de2d54e76993cd773cd169e46>:0)
UnityEditor.BuildPipeline:BuildPlayer(BuildPlayerOptions)
Zapdot.BuildForge.Editor.BuildForgeTarget:BuildPlayer(Dictionary`2) (at Assets/External/Zapdot/BuildForge/Editor/BuildForgeTarget.cs:379)
Zapdot.BuildForge.Editor.BuildForgeEditorWindow:Update() (at Assets/External/Zapdot/BuildForge/Editor/BuildForgeEditorWindow.cs:65)
UnityEditor.EditorApplication:Internal_CallUpdateFunctions() (at /Users/bokken/build/output/unity/unity/Editor/Mono/EditorApplication.cs:381)

It seems like the folder copy portion is intended to support a custom templates feature which we do not use on this project. It might be wise to also avoid attempting to copy files if there are not any custom templates in the srcPath

Here is a different log where I've modified this post-processor to print the srcPath and destPath to the console before attempting the original logic.

Modified source

private static void AndroidPostProcess(string path)
{
    string androidProjectPath = path + "/unityLibrary/clevertap-android-wrapper.androidlib";

   // I've changed this to print out the source and destination paths, but the original copy logic has been preserved.
    var srcPath = Path.Combine(Application.dataPath, EditorUtils.CLEVERTAP_ASSETS_FOLDER);
    var destPath = Path.Combine(androidProjectPath, $"assets/{EditorUtils.CLEVERTAP_APP_ASSETS_FOLDER}");

    Debug.Log($"srcPath => {srcPath}");
    Debug.Log($"destPath => {destPath}");

    // copy assets to the android project
    EditorUtils.DirectoryCopy(
      srcPath,
      destPath,
        true, true, new System.Collections.Generic.HashSet<string>() { EditorUtils.CLEVERTAP_CUSTOM_TEMPLATES_FOLDER });
    // copy CleverTapSettings to the project's AndroidManifest
    CopySettingsToAndroidManifest(androidProjectPath);
}

Output
srcPath => /Users/jcampbell/Repositories/terc-zoombinis-dev/Zoombinis/Assets/CleverTap
destPath => /Users/jcampbell/Repositories/terc-zoombinis-dev/Zoombinis/Builds/Android/Zoombinis.aab/unityLibrary/clevertap-android-wrapper.androidlib/assets/CleverTapSDK

@vasct
Copy link
Contributor

vasct commented Feb 7, 2025

Hi @jzapdot. Thank you for the detailed report. We have been able to find the issue and fix it. You can see the change in this PR: #145. It will take some time to be released, but you can apply it directly in your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants