PTCInc / vuforia-engine

Bug and feedback issue tracker for the Vuforia Engine SDK
https://developer.vuforia.com
19 stars 0 forks source link

[Unity] Vuforia.EditorClasses.GradlePropertiesParser throws IndexOutOfRangeException if gradle.properties ends with an empty line #77

Closed tsoen closed 2 months ago

tsoen commented 6 months ago

Summarize the bug: Hello, Using Vuforia 10.19 on Unity. We have encountered an exception when building for Android in Unity under a particular circumstance. It is not entirely related to Vuforia, but I thought it would be a good idea to point it out. We use the UniWebView 5.0 Unity plugin for Android and Vuforia in the project. When we build, UniWebView modifies the gradle.properties file (Library\Bee\Android\Prj\IL2CPP\Gradle\gradle.properties). When doing so, a new empty line is added to the file (because they use StringBuilder.AppendLine) The problem is that Vuforia then tries to parse the content of the file into a dictionnary, and does not seem to handle empty lines. So an Exception is raised and the build fails: IndexOutOfRangeException: Index was outside the bounds of the array. Vuforia.EditorClasses.GradlePropertiesParser+<>c.<FromString>. We have contacted the UniWebView team to resolve the issue on their side, but since it has been quite complicated to understand why Vuforia was throwing an error, it might be good as well for Vuforia to handle the case of empty lines in the gradle.properties file to avoid other edge-cases like this one.

Expected result GradlePropertiesParser ignores empty lines in the gradle.properties file and do not throw an Exception.

Reproducible steps Steps to reproduce the behavior:

  1. Open simple project with Vuforia
  2. Build the project a first time (to generate the Library\Bee\Android\Prj\IL2CPP\Gradle\gradle.properties file.
  3. Add a new empty line at the end of the gradle.properties file
  4. Try to build the project again
  5. The build fails with an exception

Affected Vuforia Engine version: 10.19

Affected platform:

Device or platform logs Logs from Unity Editor when building:

IndexOutOfRangeException: Index was outside the bounds of the array.
Vuforia.EditorClasses.GradlePropertiesParser+<>c.<FromString>b__3_1 (System.String v) (at <495be959d8de4b98a0b655f3d7e76c85>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (TSource[] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) (at <da2c421c5cee4c5abffcb37d07c3a87c>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) (at <da2c421c5cee4c5abffcb37d07c3a87c>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector) (at <da2c421c5cee4c5abffcb37d07c3a87c>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromString (System.String content) (at <495be959d8de4b98a0b655f3d7e76c85>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromFile (System.String path) (at <495be959d8de4b98a0b655f3d7e76c85>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.ParseGradleProperties (System.String projectPath) (at <495be959d8de4b98a0b655f3d7e76c85>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.OnPostGenerateGradleAndroidProject (System.String path) (at <495be959d8de4b98a0b655f3d7e76c85>:0)
UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.OnGeneratePlatformProjectPostprocess (System.String path, System.Boolean strict) (at <950249aad035483681d10b4c9c3c20d4>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/build/output/unity/unity/Modules/IMGUI/GUIUtility.cs:190)

Workaround We have modified the UniWebView plugin code to not add that empty line a the end of the gradle.properties.

SoundmartellTutorials commented 6 months ago

Hello, is it any way to solve it ?

SoundmartellTutorials commented 6 months ago

I am getting same issue with Unity 2023.2.0f1. IndexOutOfRangeException: Index was outside the bounds of the array. Vuforia.EditorClasses.GradlePropertiesParser+<>c.b__3_1 (System.String v) (at <2958d6c1eb5e4999b33514158a75e425>:0) System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (TSource[] source, System.Func2[T,TResult] keySelector, System.Func2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer1[T] comparer) (at <7bdabb6dfbaa4804838c76fbb93968d4>:0) System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable1[T] source, System.Func2[T,TResult] keySelector, System.Func2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer1[T] comparer) (at <7bdabb6dfbaa4804838c76fbb93968d4>:0) System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable1[T] source, System.Func2[T,TResult] keySelector, System.Func2[T,TResult] elementSelector) (at <7bdabb6dfbaa4804838c76fbb93968d4>:0) Vuforia.EditorClasses.GradlePropertiesParser.FromString (System.String content) (at <2958d6c1eb5e4999b33514158a75e425>:0) Vuforia.EditorClasses.GradlePropertiesParser.FromFile (System.String path) (at <2958d6c1eb5e4999b33514158a75e425>:0) Vuforia.EditorClasses.PostProcessAndroidBuild.ParseGradleProperties (System.String projectPath) (at <2958d6c1eb5e4999b33514158a75e425>:0) Vuforia.EditorClasses.PostProcessAndroidBuild.OnPostGenerateGradleAndroidProject (System.String path) (at <2958d6c1eb5e4999b33514158a75e425>:0) UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.OnGeneratePlatformProjectPostprocess (System.String path, System.Boolean strict) (at <3c54bb7f2c3e4782b3adbd9c545b57b8>:0) UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <3c54bb7f2c3e4782b3adbd9c545b57b8>:0) UnityEditor.EditorApplication:Internal_CallGlobalEventHandler()

ptc-epassaro commented 6 months ago

Hello, the team was able to reproduce the issue and a fix is planned for the next release of Vuforia Engine. Please check the Vuforia Engine release page for official updates regarding this issue.

In the meantime, a quick workaround for this would be to create a class that implements the IPostGenerateGradleAndroidProject interface and remove all the empty lines from the gradle.properties file from there.

An example of such a class could be this:

using System;
using System.IO;
using System.Linq;
using UnityEditor.Android;

public class EarlyPostGenerateGradleAndroidProject: IPostGenerateGradleAndroidProject
{
    public int callbackOrder => 1;

    public void OnPostGenerateGradleAndroidProject(string path)
    {
        var gradlePropertiesPath = Path.Combine(path, "..", "gradle.properties");
        var lines = File.ReadLines(gradlePropertiesPath).Where(l => !string.IsNullOrWhiteSpace(l));
        File.WriteAllText(gradlePropertiesPath, string.Join(Environment.NewLine, lines));
    }
}

Just create an EarlyPostGenerateGradleAndroidProject.cs file into an Editor folder inside your project and paste the code attached above. This should mitigate the issue for now.

Thank you for reporting this, I hope the code above helps!

SoundmartellTutorials commented 6 months ago

Hello, this solution does not solved the issue in my case. I keep getting IndexOutOfRangeException Just to add some more details it started happening when I used Mobile Ads Unity plugin 8.6.0. So I had to check the Custom Main Gradle Template, Custom Gradle Properties Template and Custom Gradle Setting Template. Is it anything else I should do after creating the script in the Editor Folder? Regards.

ptc-pscheper commented 5 months ago

Hi there, sorry for the delay in response as many colleagues were out over the holidays. The workaround above is not expected to work in all cases, but our next release is planning to solve the above issue.

josefgrunig commented 5 months ago

Unfortunately the solution proposed above does not fix the issue on my side and will need to downgrade vuforia version.

ptc-pscheper commented 5 months ago

Hi there, this issue should be solved with v10.20 released last week! @josefgrunig / @SoundmartellTutorials could you check if this solved the issue?

josefgrunig commented 5 months ago

I confirm I have this issue on Vuforia 10.20.3 package after upgrading from 10.10.2

ptc-pscheper commented 5 months ago

Can you elaborate on the issue in this case? Since v10.20 Engine won't fail the build process when empty lines exist in the gradle.properties file. If the issue is different from the one above, please create a new issue.

josefgrunig commented 5 months ago

Sure, I upgraded right now to 10.20.3 and get this error popup on Unity 2022.3.16f1:

Failed to call IPostGenerateGradleAndroidProject callbacks Index was outside the bounds of the array.

On Console I get those two errors:

IndexOutOfRangeException: Index was outside the bounds of the array.
Vuforia.EditorClasses.GradlePropertiesParser+<>c.<FromString>b__3_2 (System.String v) (at <e73cbe22212c469bb6e1a584a028f518>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) (at <2bcf4fe59a1e4290a8c851a306d34b0f>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector) (at <2bcf4fe59a1e4290a8c851a306d34b0f>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromString (System.String content) (at <e73cbe22212c469bb6e1a584a028f518>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromFile (System.String path) (at <e73cbe22212c469bb6e1a584a028f518>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.ParseGradleProperties (System.String projectPath) (at <e73cbe22212c469bb6e1a584a028f518>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.OnPostGenerateGradleAndroidProject (System.String path) (at <e73cbe22212c469bb6e1a584a028f518>:0)
UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.OnGeneratePlatformProjectPostprocess (System.String path, System.Boolean strict) (at <c3a7a35f684a4e72b8a6533d8c702ad7>:0)
UnityEditor.EditorApplication:Internal_CallGlobalEventHandler() (at /Users/bokken/build/output/unity/unity/Editor/Mono/EditorApplication.cs:474)
IndexOutOfRangeException: Index was outside the bounds of the array.
Vuforia.EditorClasses.GradlePropertiesParser+<>c.<FromString>b__3_2 (System.String v) (at <e73cbe22212c469bb6e1a584a028f518>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) (at <2bcf4fe59a1e4290a8c851a306d34b0f>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector) (at <2bcf4fe59a1e4290a8c851a306d34b0f>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromString (System.String content) (at <e73cbe22212c469bb6e1a584a028f518>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromFile (System.String path) (at <e73cbe22212c469bb6e1a584a028f518>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.ParseGradleProperties (System.String projectPath) (at <e73cbe22212c469bb6e1a584a028f518>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.OnPostGenerateGradleAndroidProject (System.String path) (at <e73cbe22212c469bb6e1a584a028f518>:0)
UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.OnGeneratePlatformProjectPostprocess (System.String path, System.Boolean strict) (at <c3a7a35f684a4e72b8a6533d8c702ad7>:0)
UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <c3a7a35f684a4e72b8a6533d8c702ad7>:0)
UnityEditor.EditorApplication:Internal_CallGlobalEventHandler() (at /Users/bokken/build/output/unity/unity/Editor/Mono/EditorApplication.cs:474)

Hope this helps to find the issue. I will now downgrade again, so I can continue to work on my project

ptc-epassaro commented 5 months ago

Hi @josefgrunig, would it be possible for you to send us your gradle.properties file? We cannot reproduce this issue on our side.

josefgrunig commented 5 months ago

Hi @ptc-epassaro,

I have a Custom Gradle Property Template file:

org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
org.gradle.parallel=true
unityStreamingAssets=**STREAMING_ASSETS**
# Android Resolver Properties Start
android.useAndroidX=true
android.enableJetifier=true
# Android Resolver Properties End
**ADDITIONAL_PROPERTIES**

I noticed that if I remove the Custom Template from ProjectSettings->Publishing->Build I get rid of the IndexOutOfRange issue. I have a Custom Template because I have read to do so in the Firebase Resolver Settings

Still cannot build because of the following Duplicate Class error. Looks like a conflict with Firebase SDK, If I remove Vuforia or Firebase (and the Android Resolver) I get rid of the Duplicate Class errors. I have also tried to update Firebase with no luck.

* What went wrong:
Execution failed for task ':launcher:checkReleaseDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class androidx.annotation.AnimRes found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar)
     Duplicate class androidx.annotation.AnimatorRes found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar)
     Duplicate class androidx.annotation.AnyRes found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar)
     Duplicate class androidx.annotation.AnyThread found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar) 

Probably I need to open a new ticket for this separate issue

tsoen commented 5 months ago

Hi @ptc-epassaro,

I have a Custom Gradle Property Template file:

org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
org.gradle.parallel=true
unityStreamingAssets=**STREAMING_ASSETS**
# Android Resolver Properties Start
android.useAndroidX=true
android.enableJetifier=true
# Android Resolver Properties End
**ADDITIONAL_PROPERTIES**

I noticed that if I remove the Custom Template from ProjectSettings->Publishing->Build I get rid of the IndexOutOfRange issue. I have a Custom Template because I have read to do so in the Firebase Resolver Settings

Still cannot build because of the following Duplicate Class error. Looks like a conflict with Firebase SDK, If I remove Vuforia or Firebase (and the Android Resolver) I get rid of the Duplicate Class errors. I have also tried to update Firebase with no luck.

* What went wrong:
Execution failed for task ':launcher:checkReleaseDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class androidx.annotation.AnimRes found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar)
     Duplicate class androidx.annotation.AnimatorRes found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar)
     Duplicate class androidx.annotation.AnyRes found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar)
     Duplicate class androidx.annotation.AnyThread found in modules annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and jetified-androidx.annotation.annotation-1.2.0 (androidx.annotation.annotation-1.2.0.jar) 

Probably I need to open a new ticket for this separate issue

Hello @josefgrunig, Is this the actual content of your Custom Gradle Property Template file or did you add the "# Android Resolver Properties Start" only for us to understand better ? If this is actual content, I suggest you remove the 2 lines starting with a '#'. From what I understand, the GradlePropertiesParser is explicitly trying to split each line by the '=' character, if it's missing in a line it might crash. Also we don't know what comes after "ADDITIONAL_PROPERTIES", I think you should post the whole content. (Did you also add this line ?). Just my two cents though, I don't work at PTC.

josefgrunig commented 5 months ago

Hi @tsoen, the file is generated when enabling the Custom Gradle Property Template flag. I did not modify any of its part. If I export the Android Project, it looks like this:

org.gradle.jvmargs=-Xmx4096M
org.gradle.parallel=true
unityStreamingAssets=.ds_store, .json
# Android Resolver Properties Start
android.useAndroidX=true
android.enableJetifier=true
# Android Resolver Properties End
unityTemplateVersion=5

Removing the lines with the comment "#" it builds without error. Thank you for the suggestion.

Still having the duplicate class issue, I will try to reproduce it on an empty project with only vuforia + firebase and open a new ticket.

Thank you.

ptc-epassaro commented 4 months ago

Hi @josefgrunig, thank you very much for the very detailed reply. And thank you to @tsoen for the support! We will look into it and keep you posted.

Thanks again!

ptc-epassaro commented 4 months ago

Hello @josefgrunig, the team was able to identify and fix the issue, which was indeed caused by the presence of comments in the gradle.properties file - as noted by @tsoen.

The fix for this issue will be included in the next release of Vuforia. We'll keep you posted! Thank you all again for your feedback!

josefgrunig commented 4 months ago

You are welcome, thank you for the support!

ptc-thesbyro commented 2 months ago

The fix for this issue was released with 10.22.5, so I will close this thread. If the problem remains, please re-open or create a new ticket.