Is there some way to remove native code in Visual Studio UWP-apps BEFORE reaching native compiler?

Thorsten Schöning 81 Reputation points
2020-01-08T17:29:56.323+00:00

I have some Java code that needs to be made available in an UWP-app and am using IKVM to compile a DLL of that code. That works pretty well in DEBUG-mode, but fails with different errors in RELEASE-mode and the then used .NET native toolchain:

    Schweregrad Code    Beschreibung    Projekt Datei   Zeile   Unterdrückungszustand
    Fehler      Die Methode "WinFormsMessageLoop.Invoke(Delegate)" löst immer eine Ausnahme aufgrund der fehlenden Methode "Control.Invoke(Delegate)" aus in der Assembly "System.Windows.Forms" aus. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version.  Ams.Oms.Poc         
    Fehler      Der Typ "System.Windows.Forms.MenuItem" von der Assembly "System.Windows.Forms" war nicht in der Kompilierung enthalten, es wurde jedoch in "Typ "ikvm.awt.NetMenuPeer"" darauf verwiesen. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version. Ams.Oms.Poc         
    Fehler      Der Typ "System.Windows.Forms.KeyEventHandler" von der Assembly "System.Windows.Forms" war nicht in der Kompilierung enthalten, es wurde jedoch in "Methode "NetComponentPeer.hookEvents()"" darauf verwiesen. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version. Ams.Oms.Poc         
    Fehler      Der Typ "System.Drawing.Drawing2D.ColorBlend" von der Assembly "System.Drawing.Common" war nicht in der Kompilierung enthalten, es wurde jedoch in "Methode "NetGraphics.setPaint(Paint)"" darauf verwiesen. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version.   Ams.Oms.Poc         
    [...]
    Fehler      Die Methode "NetWindowPeer.getGraphics()" löst immer eine Ausnahme aufgrund der fehlenden Methode "Control.get_ClientRectangle()" aus in der Assembly "System.Windows.Forms" aus. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version.  Ams.Oms.Poc         
    Fehler      Die Methode "NetToolkit.createImage(byte[], int, int)" löst immer eine Ausnahme aufgrund der fehlenden Methode "Bitmap..ctor(Stream)" aus in der Assembly "System.Drawing" aus. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version.    Ams.Oms.Poc         
    Fehler      Die Methode "NetMouseInfoPeer.fillPointWithCoords(Point)" löst immer eine Ausnahme aufgrund der fehlenden Methode "Cursor.get_Position()" aus in der Assembly "System.Windows.Forms" aus. Möglicherweise fehlt eine Assembly oder eine Abhängigkeit von einer neueren Windows SDK-Version.  Ams.Oms.Poc

Simply disabling the usage of the native toolchain in the build settings of the RELEASE-mode resolves those problems, but the resulting app still can not be published in the app store:

Package acceptance validation error: This package wasn't built for submission to the Store. Make sure you're uploading a Release build with the .NET Native tool chain enabled.

The point is that I don't need any of the problematic native code for my own Java code, but instead all the problematic functions and references come from some GUI-related DLL of IKVM itself. I don't need that DLL as well, but for some reason it needs to be available in the project to fulfill dependencies or it can't be compiled in DEBUG-mode already. But nothing of that is needed during runtime in the end.

So I'm wondering if there's some step or setting or "trick" to remove native code BEFORE the native toolchain sees it and runs into errors somehow? Something like an additional filter or stuff like that.

Everything else seems to force me to change and fix those problems in IKVM somehow, which is non-trivial most likely as well. In the best case I'm able to simply comment the code not needed or make other DLLs not depend on the failing IKVM.OpenJDK.SwingAWT-DLL anymore or stuff like that.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,882 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. James Dailey - MSFT 336 Reputation points Microsoft Employee
    2020-01-13T22:39:45.387+00:00

    The errors you are seeing are due to the fact that the binary that is being built by IKVM has a dependency on the legacy Win Forms UI technology. To work around this problem you will need to make sure that IKVM is building a COM object or PCL that doesn't include any additional dependencies. From looking at the IKVM docs is looks like there is an additional dependency on IKVM.OpenJDK.Core.dll. This binary likely has the dependency on the Win Forms UI components.

    I understand that it is frustrating that you can't disable .net native complication and upload to the store. In this case however, the errors are due to the external dependencies. Remember when building debug the UWP "sandbox" is much more permissive than when you build as release and install the app from the store (vs. side loading).

    Unfortunately IKVM is not something that Microsoft supports but I hope this will help you fine tune the parameters for IKVM to allow you to generate a usable PCL.

    That said, depending on your particular scenario you may be able to build your app using Win Forms and use the Desktop Bridge tools to bring your app to the Microsoft Store. The Desktop Bridge allows you to build and package a standard Win32 app as a UWP and gives you hybrid access to the UWP UI components. While there are some restrictions I would recommend taking a look at this solution if you can't remove the Win Forms dependency.

    Package desktop application
    https://learn.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-root
    link text
    Good luck!

    James Dailey
    Microsoft Developer Services

    0 comments No comments