Affdex Unity Android IL2CPP build and runtime errors

unity
android

#1
  1. Using Unity 2017.2, Android, IL2CPP, importing Affdex SDK causes the build to fail with linker errors [1]. The reason is that the pinvoke entry points are compiled differently, so must be omitted using #if preprocessors rather than Application.platform runtime checks.

  2. Once the above is fixed, and you try to run on an Android device (again, using IL2CPP) runtime errors [2] occur due to how the listener callbacks are implemented. The solution is to use the same method for callbacks as are used on iOS, by applying the MonoPInvokeCallback attribute.

I’ve fixed the Affdex scripts to resolve the above two issues, and they can be found here:

https://drive.google.com/open?id=170QHg15WhgopCIa8XEqiJwlX1oVxpw5i

[1]

Failed running /Applications/Unity/Unity.app/Contents/il2cpp/build/il2cppcore/il2cppcore.dll --convert-to-cpp --emit-null-checks --enable-array-bounds-check --dotnetprofile="net45" --compile-cpp --libil2cpp-static --platform="Android" --architecture="ARMv7" --configuration="Release" --outputpath="/Users/username/Desktop/Affdex/Temp/StagingArea/assets/bin/Data/Native/armeabi-v7a/libil2cpp.so" --cachedirectory="/Users/username/Desktop/Affdex/Assets/../Library/il2cpp_android_armeabi-v7a/il2cpp_cache" --additional-include-directories="/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/bdwgc/include" --additional-include-directories="/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/libil2cpp/include" --tool-chain-path="/Applications/android-sdk/android-ndk-r13b" --map-file-parser="/Applications/Unity/Unity.app/Contents/Tools/MapFileParser/MapFileParser" --assembly="/Users/username/Desktop/Affdex/Temp/StagingArea/assets/bin/Data/Managed/Assembly-CSharp.dll" --assembly="/Users/username/Desktop/Affdex/Temp/StagingArea/assets/bin/Data/Managed/UnityEngine.UI.dll" --assembly="/Users/username/Desktop/Affdex/Temp/StagingArea/assets/bin/Data/Managed/UnityEngine.dll" --generatedcppdir="/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput" 

stdout:
Building libil2cpp.so with AndroidToolChain.
	Output directory: /Users/username/Desktop/Affdex/Temp/StagingArea/assets/bin/Data/Native/armeabi-v7a
	Cache directory: /Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache
ObjectFiles: 571 of which compiled: 134
	Time Compile: 48552 milliseconds GenericMethods1.cpp
	Time Compile: 35342 milliseconds Bulk_Generics_2.cpp
	Time Compile: 34868 milliseconds Bulk_Generics_13.cpp
	Time Compile: 34211 milliseconds Bulk_mscorlib_3.cpp
	Time Compile: 33006 milliseconds Bulk_System.Xml_4.cpp
	Time Compile: 31165 milliseconds Bulk_mscorlib_2.cpp
	Time Compile: 30753 milliseconds Bulk_System.Xml_2.cpp
	Time Compile: 29617 milliseconds Bulk_System_5.cpp
	Time Compile: 28169 milliseconds GenericMethods0.cpp
	Time Compile: 27767 milliseconds Bulk_mscorlib_10.cpp
Total compilation time: 141236 milliseconds.
il2cpp.exe didn't catch exception: Unity.IL2CPP.Building.BuilderFailedException: /Applications/android-sdk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ @"/var/folders/45/pq_748rx1zbcv2ll3t755_1r0000gn/T/tmpXo7X0H.tmp" -o "/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/linkresult_04AE9CC1B2F4622AAAFF7CAB4B75E17B/libil2cpp.so" -shared -Wl,-soname,libil2cpp.so -Wl,--no-undefined -Wl,-z,noexecstack -Wl,--gc-sections -Wl,--build-id --sysroot "/Applications/android-sdk/android-ndk-r13b/platforms/android-16/arch-arm" -gcc-toolchain "/Applications/android-sdk/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64" -target armv7-none-linux-androideabi -Wl,--wrap,sigaction -L "/Applications/android-sdk/android-ndk-r13b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a" -lgnustl_static -Xlinker -Map="/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/linkresult_04AE9CC1B2F4622AAAFF7CAB4B75E17B/libil2cpp.map" -rdynamic -fPIE -pie -fuse-ld=bfd

clang++: warning: argument unused during compilation: '-pie'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexRegisterListeners_m3681386252':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12199: undefined reference to `affdexRegisterListeners'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexProcessFrame_m1856467240':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12210: undefined reference to `affdexProcessFrame'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStart_m518483318':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12221: undefined reference to `affdexStart'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexRelease_m1225033291':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12232: undefined reference to `affdexRelease'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStop_m3423609253':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12242: undefined reference to `affdexStop'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetExpressionState_m1489503166':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12253: undefined reference to `affdexSetExpressionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetEmotionState_m688554097':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12263: undefined reference to `affdexSetEmotionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexInitialize_m142600964':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12277: undefined reference to `affdexInitialize'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexProcessFrame_m1856467240':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12210: undefined reference to `affdexProcessFrame'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetExpressionState_m1489503166':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12253: undefined reference to `affdexSetExpressionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetEmotionState_m688554097':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12263: undefined reference to `affdexSetEmotionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStart_m518483318':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12221: undefined reference to `affdexStart'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStop_m3423609253':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12242: undefined reference to `affdexStop'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexRelease_m1225033291':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12232: undefined reference to `affdexRelease'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexInitialize_m142600964':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12277: undefined reference to `affdexInitialize'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)


   at Unity.IL2CPP.Building.CppProgramBuilder.PostprocessObjectFiles(IEnumerable`1 objectFiles, CppToolChainContext toolChainContext) in /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/Unity.IL2CPP.Building/CppProgramBuilder.cs:line 306
   at Unity.IL2CPP.Building.CppProgramBuilder.Build(IBuildStatistics& statistics) in /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/Unity.IL2CPP.Building/CppProgramBuilder.cs:line 99
   at il2cpp.Program.DoRun(String[] args) in /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/il2cpp/Program.cs:line 241
   at il2cpp.Program.Run(String[] args) in /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/il2cpp/Program.cs:line 103
   at il2cpp.Program.Main(String[] args) in /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/il2cpp/Program.cs:line 80
stderr:

Unhandled Exception: Unity.IL2CPP.Building.BuilderFailedException: /Applications/android-sdk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ @"/var/folders/45/pq_748rx1zbcv2ll3t755_1r0000gn/T/tmpXo7X0H.tmp" -o "/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/linkresult_04AE9CC1B2F4622AAAFF7CAB4B75E17B/libil2cpp.so" -shared -Wl,-soname,libil2cpp.so -Wl,--no-undefined -Wl,-z,noexecstack -Wl,--gc-sections -Wl,--build-id --sysroot "/Applications/android-sdk/android-ndk-r13b/platforms/android-16/arch-arm" -gcc-toolchain "/Applications/android-sdk/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64" -target armv7-none-linux-androideabi -Wl,--wrap,sigaction -L "/Applications/android-sdk/android-ndk-r13b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a" -lgnustl_static -Xlinker -Map="/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/linkresult_04AE9CC1B2F4622AAAFF7CAB4B75E17B/libil2cpp.map" -rdynamic -fPIE -pie -fuse-ld=bfd

clang++: warning: argument unused during compilation: '-pie'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexRegisterListeners_m3681386252':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12199: undefined reference to `affdexRegisterListeners'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexProcessFrame_m1856467240':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12210: undefined reference to `affdexProcessFrame'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStart_m518483318':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12221: undefined reference to `affdexStart'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexRelease_m1225033291':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12232: undefined reference to `affdexRelease'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStop_m3423609253':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12242: undefined reference to `affdexStop'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetExpressionState_m1489503166':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12253: undefined reference to `affdexSetExpressionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetEmotionState_m688554097':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12263: undefined reference to `affdexSetEmotionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexInitialize_m142600964':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12277: undefined reference to `affdexInitialize'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexProcessFrame_m1856467240':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12210: undefined reference to `affdexProcessFrame'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetExpressionState_m1489503166':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12253: undefined reference to `affdexSetExpressionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexSetEmotionState_m688554097':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12263: undefined reference to `affdexSetEmotionState'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStart_m518483318':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12221: undefined reference to `affdexStart'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexStop_m3423609253':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12242: undefined reference to `affdexStop'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexRelease_m1225033291':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12232: undefined reference to `affdexRelease'
/Users/username/Desktop/Affdex/Library/il2cpp_android_armeabi-v7a/il2cpp_cache/CE9BA2EEA2BC20996BF1D2A1979BBC37.o: In function `iOSNativePlatform_affdexInitialize_m142600964':
/Users/username/Desktop/Affdex/Temp/StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:12277: undefined reference to `affdexInitialize'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)


   at Unity.IL2CPP.Building.CppProgramBuilder.PostprocessObjectFiles(IEnumerable`1 objectFiles, CppToolChainContext toolChainContext)
   at Unity.IL2CPP.Building.CppProgramBuilder.Build(IBuildStatistics& statistics)
   at il2cpp.Program.DoRun(String[] args)
   at il2cpp.Program.Run(String[] args)
   at il2cpp.Program.Main(String[] args)
   at Program.Main(String[] args)

UnityEngine.Debug:LogError(Object)
UnityEditorInternal.Runner:RunProgram(Program, String, String, String, CompilerOutputParserBase) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildUtils.cs:128)
UnityEditorInternal.Runner:RunNetCoreProgram(String, String, String, CompilerOutputParserBase, Action`1) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildUtils.cs:79)
UnityEditorInternal.IL2CPPBuilder:RunIl2CppWithArguments(List`1, Action`1, String) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:322)
UnityEditorInternal.IL2CPPBuilder:ConvertPlayerDlltoCpp(ICollection`1, String, String) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:305)
UnityEditorInternal.IL2CPPBuilder:Run() (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:151)

[2]

ArgumentException: Object contains non-primitive or non-blittable data.
12-18 13:51:46.524 10877 10889 E Unity   :   at Affdex.AndroidNativePlatform+<Initialize>c__Iterator0.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
12-18 13:51:46.524 10877 10889 E Unity   :   at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0

#2

Hi Nicolas, thanks very much for reporting this issue and including the fixes that worked for you. We’ll log this in our internal bug tracker.

P.S. sorry for the inconvenience in posting here, we’re still working through some kinks associated with our adoption of this forum system.


#3

Hey, thanks for taking care of this.

I’ve posted another fix we’ve had to use in the Emotion SDK forum. The modifications we’ve had to make to get the Unity SDK working are really starting to stack up, and it will make upgrading difficult in the future. It would be great to be able to use the SDK right out of the box.

Thanks!