Flutter Unity - EhiltonKazuo/configuration-manual GitHub Wiki

This guide is for Android and was tested with Unity 2020.3.4f1 and Gradle 6.8.3

This guide add some lines of code and make some changes. Original post was made by gisinator and found here

1. Create a new flutter project

In visual studio code, type ctrl+shift+P and type flutter: new application project and hit enter

Choose the folder of your new flutter project

Choose the name of your new flutter project

🔗 resources

https://flutter.dev/docs/get-started/test-drive?tab=androidstudio

2. Add the flutter_unity_widget package as a dependency to your flutter project

Open the pubspec.yaml file in your flutter project and add the following dependency.

dependencies:
  flutter:
    sdk: flutter
  flutter_unity_widget:

If you then save the file, your IDE should run 'flutter pub get' and the package will be imported.

🔗 resources

https://pub.dev/packages/flutter_unity_widget

3. Create the 'unity' directory in your flutter project.

Create a unity folder at the same level as the lib/test/android folders in your flutter project.

4. Move your Unity project into flutter

Take your unity project and move it under the 'unity' folder your created in your flutter project

5. Set up your Unity project

Now that you moved your Unity project, open it in Unity and configure the following settings by going to Edit>Project Settings>Player then select the android icon to go into android player settings.

  • Under other settings>configuration, set scripting backend to IL2CPP.
  • Under other settings>configuration, set target architectures to ARMv7 and ARM64

6. Import the FlutterUnityIntegration package into Unity

Import the FlutterUnityIntergration.unitypackage file (you can find it in the repo under the scripts folder) into Unity (double click on the file which will open unity, or in unity do Assets>Import Package>Custom Package and then select the package). Wait for the import to complete.

If the import was successful, you should now see a 'Flutter' menu on the top bar.

7. Export Unity as a library

With the FlutterUnityIntergration.unitypackage package imported, click Flutter>Export Android to export Unity as a library (might take a while). After the export is done, there will be a unityLibrary directory in your flutter project under the android folder.

⁉️ possible errors

  • The build target does not support build appending (Unity 2019.4)

    ✅ Apply the following fix https://github.com/juicycleff/flutter-unity-view-widget/issues/234#issuecomment-735307249

    Open the Assets>FlutterUnityIntegration>Editor>Build.cs script and make these changes

    // in DoBuildIOS() (or BuildIOS()) -> for iOS
    var options = BuildOptions.AcceptExternalModificationsToPlayer;
    // should become
    var options = BuildOptions.AllowDebugging;
    
    // in DoBuildAndroid() -> for Android
    var options = BuildOptions.AcceptExternalModificationsToPlayer;
    // should become
    var options = BuildOptions.AllowDebugging;
    EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
    

    Then build again and you should see the unityLibrary folder in your unity project.

  • There is no unityLibrary folder but a unityExport folder

    ✅ You are using an older version of the build scripts, use a more up to date version.

  • FileNotFoundException UnsafeNativeMethods.cpp

    FileNotFoundException: C:\Users\alexa\Documents\Flutter + Unity Version 4\flutter_application\unity\AR-Tour-Application\Builds/AR Tour.apk\unityLibrary\src\main\Il2CppOutputProject\IL2CPP\libil2cpp\icalls\mscorlib\System.Runtime.InteropServices.WindowsRuntime\UnsafeNativeMethods.cpp does not exist
    

🔗 resources

https://github.com/juicycleff/flutter-unity-view-widget/issues/234#issuecomment-735307249

7. Build an .apk file to get access to the library files

In unity go to File>Build Settings and in the android platform disable the export project toggle, then click build to build your .apk file.

⁉️ possible errors

🔗 resources

https://developers.google.com/ar/develop/unity/android-11-build

8. Copy the library files

Once your .apk is built, go to the directory of where your .apk file is and open the file like a .zip file. Open the folder you got by unzipping and go to the lib/arm64-v8a directory (your exact version might be different). In here, you see the libUnityARCore.so and libarpresto_api.so library files. Copy these files into the android/unityLibrary/libs folder in your flutter project

9. Update build.gradle

So we just added these new libraries, but now we need to update the android/unityLibrary/build.gradle file in flutter in order to include these library files.

dependencies {
    // existing code

    ...

    // add these 2
    implementation(name: 'libarpresto_api', ext:'so')
    implementation(name: 'libUnityARCore', ext:'so')
}

10. Build the flutter project

Now you can build the flutter project and put it on your device.

⁉️ possible errors

  • Missing 'package' key attribute on element package at [:arcore_client:] AndroidManifest.xml:30:9-54"

    ✅ Update your gradle versions

    Go into android/gradle/wrapper/gradle-wrapper.properties and change the distributionURL to use a higher version.

    distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
    

    Go to android/build.gradle and change the classpath to use a higher version.

    classpath 'com.android.tools.build:gradle:4.1.0
    
  • Issue with minSdkVersion

    ✅ Your Unity Minimum API Level and your flutter project minSdkVersion should match

    In unity go to Edit>Project Settings>Player>Settings for android>Other settings>Identification and set your minimum API level. (For AR projects this should be ≥ API level 24).

    In your flutter project open android/app/build.gradle and also set the minSdkVersion to 24 or higher.

  • NDK is not installed

    Execution failed for task ':unityLibrary:BuildIl2CppTask'.
    > NDK is not installed
    

    Open the flutter project in Android Studio and go to Tools/SDKManager/SDK Tools. There select the NDK (Side by side) package and click apply to install it.

    https://github.com/juicycleff/flutter-unity-view-widget/issues/322#issuecomment-783153071

    Please intall the exact NDK version in Unity,

    you can check out the version in unity by Edit>Preferences>External Tools>Android NDK install...>Copy Path Open this folder, open [source.properties](http://source.properties) ,check out the Pkg.Revision property. If you can not find the exactly same version, just copy the previous unity ndk install folder into your android studio sdk lication {Android Studio Android SDK Location}/ndk/{version}

  • NDK version is not matching, version xyz is required

    Open the flutter project in Android Studio and go to Tools/SDKManager/SDK Tools. There enable Show Package Details and pick the required version of SDK and install it. You can remove the other ones.

  • clang++: error: invalid linker name in argument '-fuse-ld=gold.exe'

    I got this error once. For me the issue was that my NDK version was too high. My Unity version was 2020.1 and was installed with Unity Hub so I went to see what NDK version got installed with that version of Unity. You can see this here C:\Program Files\Unity\Hub\Editor\2020.1.17f1\Editor\Data\PlaybackEngines\AndroidPlayer\NDK\source.properties. If you then open the file in something like VS code or notepad you'll see something like this.

    Pkg.Desc = Android NDK
    Pkg.Revision = 19.0.5232133
    

    The currently installed NDK version in android studio was version 23, so I removed that one and installed version 19 instead.

  • Deprecated API

    I got this warning/note during build but it did not prevent me from building.

    Note: C:\Users\alexa\Downloads\flutter-unity-view-widget-4\example\android\unityLibrary\src\main\java\com\unity3d\player\UnityPlayerActivity.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\flutter_plugin_android_lifecycle-1.0.8\android\src\main\java\io\flutter\plugins\flutter_plugin_android_lifecycle\FlutterAndroidLifecyclePlugin.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: C:\Users\alexa\Downloads\flutter_demo\example\android\unityLibrary\src\main\java\com\unity3d\player\UnityPlayerActivity.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: Some input files use unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    
  • Execution failed for task ':app:lintVitalRelease'

    ✅ Apply the following fix https://github.com/juicycleff/flutter-unity-view-widget/issues/321

    Add this line of code in unityLibrary/build.gradle

    android {
     // existing code
     ...
     buildTypes {
         release {
             signingConfig signingConfigs.debug
         }
         debug {
             signingConfig signingConfigs.debug
         }
         profile{
             signingConfig signingConfigs.debug
         }
     }
    

}



🔗 ***resources***

[https://github.com/juicycleff/flutter-unity-view-widget/issues/310#issuecomment-782585847](https://github.com/juicycleff/flutter-unity-view-widget/issues/310#issuecomment-782585847)

---

**Various other links**

[https://github.com/juicycleff/flutter-unity-view-widget](https://github.com/juicycleff/flutter-unity-view-widget)

[https://www.youtube.com/watch?v=sf54tOAkmzU&ab_channel=PushpendraSaini](https://www.youtube.com/watch?v=sf54tOAkmzU&ab_channel=PushpendraSaini)

[https://github.com/LuisReyes98/Flutter-Unity](https://github.com/LuisReyes98/Flutter-Unity) (is maybe old, based on this? [https://medium.com/@davidbeloosesky/embedded-unity-within-android-app-7061f4f473a](https://medium.com/@davidbeloosesky/embedded-unity-within-android-app-7061f4f473a) so not UAAL?)

[https://github.com/Glartek/flutter-unity](https://github.com/Glartek/flutter-unity)

[https://forum.unity.com/threads/using-unity-as-a-library-in-native-ios-android-apps.685195/](https://forum.unity.com/threads/using-unity-as-a-library-in-native-ios-android-apps.685195/)

**UAAL**

[https://unity.com/features/unity-as-a-library](https://unity.com/features/unity-as-a-library)

[https://forum.unity.com/threads/integration-unity-as-a-library-in-native-android-app-version-2.751712/](https://forum.unity.com/threads/integration-unity-as-a-library-in-native-android-app-version-2.751712/)

[https://answers.unity.com/questions/1591254/developing-for-android-gradle-error-androidmanifes.html](https://answers.unity.com/questions/1591254/developing-for-android-gradle-error-androidmanifes.html)