Skip to content

Conversation

timbotimbo
Copy link
Collaborator

Description

Quite a few JsonDotNet errors have popped up on discord and github recently.
This is because of a change in Unity packages that come included in recent Unity versions.

It all comes down to
Assets\FlutterUnityIntegration\JsonDotNet\Assemblies\AOT\Newtonsoft.Json.dll.txt

  • Unity 2019 and 2020 versions needed the dll to be included in the assets folder.

  • Early Unity 2021 and 2022 versions gave duplicate import errors because of default installed packages (usually collab / version control)
    To avoid this error the .dll file was renamed to .dll.txt, to stop Unity from importing it.

  • Now the latest 2021 and 2022 versions have returned to the old behaviour of needing the dll.

An alternative of including the ddl is having users manually install it from the package manager.

Error messages

Error 1: when the .dll is missing but needed

Assets\FlutterUnityIntegration\UnityMessageManager.cs(3,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
Assets\FlutterUnityIntegration\UnityMessageManager.cs(58,16): error CS0246: The type or namespace name 'JObject' could not be found (are you missing a using directive or an assembly reference?)
Assets\FlutterUnityIntegration\UnityMessageManager.cs(15,26): error CS0246: The type or namespace name 'JToken' could not be found (are you missing a using directive or an assembly reference?)
Assets\FlutterUnityIntegration\UnityMessageManager.cs(34,64): error CS0246: The type or namespace name 'JToken' could not be found (are you missing a using directive or an assembly reference?)

Error 2: when the .dll is included but redundant

Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included on the current platform. Only one assembly with the same name is allowed per platform. (C:/Users/XXXXX/Library/PackageCache/com.unity.nuget.newtonsoft-json@3.0.2/Runtime/Newtonsoft.Json.dll)
Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included on the current platform. Only one assembly with the same name is allowed per platform. (Assets/FlutterUnityIntegration/JsonDotNet/Assemblies/AOT/Newtonsoft.Json.dll)
PrecompiledAssemblyException: Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included on the current platform. Only one assembly with the same name is allowed per platform.

Test

I ran a test importing the unitypackage in a new unity project (without any package manager changes).
This is the out of the box experience for a new user.

Unity Compiles with .dll included
2022.2.12 Yes
2022.2.5 Yes
2022.1.24 No
2022.1.13 No
2021.3.21 Yes
2021.3.13 No
2020.3.46 Yes
2020.3.42 Yes
2019.4.40 Yes
2019.4.19 Yes

It seems like only the early to middle versions of Unity 2021 and 2022 use a Version Control package that includes a conflicting Newtonsoft package.
The latest versions have removed the dependency, just like 2019 and 2020 before.

if some users do have to get an error, error 2 seems easier to solve based on the provided message.

Changes

  • Changed the file extension to .dll to re-enable the import.
  • Add these common errors to the readme
  • Switch the example project version to the latest 2021 LTS. To avoid the implicit suggestion to use 2022.1.13, which is one of the few versions that don't like this change.

In the future this should propbably be resolved by publishing a UPM package with dependencies.
But this should give a smoother setup for now.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@timbotimbo timbotimbo changed the title Re-enable the JsonDotNet to avoid common errors. Re-enable the JsonDotNet dll to avoid common errors. Apr 6, 2023
@timbotimbo timbotimbo mentioned this pull request Apr 6, 2023
7 tasks
Copy link
Collaborator

@RoyalCoder88 RoyalCoder88 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, 100% approve!

@timbotimbo timbotimbo merged commit 6146dd2 into juicycleff:master Apr 10, 2023
@timbotimbo timbotimbo deleted the json.net branch July 8, 2023 19:29
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

Successfully merging this pull request may close these issues.

2 participants