Xamarin.Google.MLKit.Common.MlKitException Failed to Initialize Detector

If you came to this post, this mean you encounter the same problem as I am. Xamarin.Google.MLKit.Common.MlKitException Failed to initialize detector is throw when ObjectDetectorOptions.Builder() is call.

If we try to catch the error and inspect the inner error. The inner exception complain about: This file can not be opened as a file descriptor; it is probably compressed.

Ask any AI, all will suggest adding AndroidAapt2NoCompress tag under property group in csproj.

<PropertyGroup>
 <AndroidAapt2NoCompress>tflite</AndroidAapt2NoCompress>
</PropertyGroup>

or with dot

<PropertyGroup>
 <AndroidAapt2NoCompress>.tflite</AndroidAapt2NoCompress>
</PropertyGroup> 

Unfortunately .NET compiler doesn’t seem to read this property. After more try and error, the solution is adding this instead.

<PropertyGroup>
 <AndroidAapt2LinkExtraArgs>$(AndroidAapt2LinkExtraArgs) -0 .tflite</AndroidAapt2LinkExtraArgs>
</PropertyGroup>

Leave a comment