mono / Embeddinator-4000

Tools to turn .NET libraries into native libraries that can be consumed on Android, iOS, Mac, Linux and other platforms.
MIT License
758 stars 95 forks source link

GenerateJavaStubs is invoked before initializing supported frameworks appropriately #567

Open atsushieno opened 6 years ago

atsushieno commented 6 years ago

I think the root cause of the problem is the same as https://github.com/mono/Embeddinator-4000/issues/530 but current code base is implemented to invoke non-public features and targets in Xamarin.Android build tasks (which you are not supposed to do of course) and thus it runs GenerateJavaStubs task before appropriate supported frameworks.

Namely, GenerateJavaStubs.proj build fails at:

    Error executing task GenerateJavaStubs: System.NullReferenceException: Object reference not set to an instance of an object
      at Xamarin.Android.Tasks.ManifestDocument.Merge (System.Collections.Generic.List`1[T] subclasses, System.Collections.Generic.List`1[T] selectedWhitelistAssemblies, System.String applicationClass, System.Boolean embed, System.String bundledWearApplicationName, System.Collections.Generic.IEnumerable`1[T] mergedManifestDocuments) [0x00382] in /sources/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs:270 
      at Xamarin.Android.Tasks.GenerateJavaStubs.Run (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver res) [0x004b0] in /sources/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:227 
      at Xamarin.Android.Tasks.GenerateJavaStubs.Execute () [0x0023a] in /sources/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs:91 
      at Microsoft.Build.BuildEngine.TaskEngine.Execute () [0x00000] in /sources/mono/mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/TaskEngine.cs:134 
      at Microsoft.Build.BuildEngine.BuildTask.Execute () [0x0008d] in /sources/mono/mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/BuildTask.cs:101 
    Done building target "Build" in project "/home/atsushi/Desktop/TextToSpeechPlugin-master/e4k/android/GenerateJavaStubs.proj" ("/home/atsushi/Desktop/TextToSpeechPlugin-master/e4k/android/GenerateJavaStubs.proj").
    Target performance summary:
         3.341 ms  _CheckForInvalidConfigurationAndPlatform                1 calls
       895.380 ms  Build                                                   1 calls
    Tasks performance summary:
       183.372 ms  ResolveAssemblies                                       1 calls
       709.865 ms  GenerateJavaStubs                                       1 calls
    Build FAILED.

The NRE is caused by accessing MonoAndroidHelper.SupportedVersions which is null.

tritao commented 6 years ago

Can you guys add some public task that we can use for this?

cc @jonathanpeppers @jonpryor

jonathanpeppers commented 6 years ago

@atsushieno is this only an issue on Linux? GenerateJavaStubs has been working for me on Windows and Mac.

Using "private" MSBuild tasks at the time was the only path to get things working, but I'm not sure what marks a task private versus non-private. @atsushieno are you saying there needs to be an Embeddinator target of some kind in Xamarin.Android.Common.targets?

It would probably have to be two targets:

Then add tests making sure they don't get broken.

This would also come at a cost, because Embeddinator would have to again rely on an unreleased Xamarin.Android if those were added.

atsushieno commented 6 years ago

@jonathanpeppers I have no idea. I know it is problematic and it doesn't work fine on Linux. I rather assume you are based on bogus assumption and incorrectly premised setup that every Mac (and Linux) must have former versions of Xamarin.Android.

If you prepare clean CI environment that never installs any Xamarin.Android and make sure that it still works, then I trust the outcome (well, it does not have to be you who prepare it).

atsushieno commented 6 years ago

This would also come at a cost, because Embeddinator would have to again rely on an unreleased Xamarin.Android if those were added.

Then @jonpryor needs to rethink about collecting everything in xamarin-android then. I keep saying that idea is wrong.

atsushieno commented 6 years ago

To make it clear, I just ran E4k from NuGet package (i.e. as of 0.3.0) with my Mac which only has installed VSMac and Xamarin.Android from alpha channel, and things still don't work. What I observe from here is that even with the packaged Xamarin.Android e4k still has dependencies on local setup, or it totally does not work anywhere (in case @jonathanpeppers verified behavior with some local changes, which I often get trapped with my code).

Parsing assemblies...
    MonoAndroidToolsPath path is not found and no default location is provided; skipping
    Parsed 'Plugin.TextToSpeech.dll'
Processing assemblies...
Generating binding code...
    Generated: Plugin.TextToSpeech.h
    Generated: Plugin.TextToSpeech.c
    Generated: Mono.Android.h
    Generated: Mono.Android.c
    Generated: Java.Interop.h
    Generated: Java.Interop.c
    Generated: mscorlib.h
    Generated: mscorlib.c
    Generated: Plugin.TextToSpeech.Abstractions.h
    Generated: Plugin.TextToSpeech.Abstractions.c
    Generated: c-support.c
    Generated: c-support.h
    Generated: embeddinator.h
    Generated: glib.c
    Generated: glib.h
    Generated: mono-support.c
    Generated: mono-support.h
    Generated: mono_embeddinator.c
    Generated: mono_embeddinator.h
Compiling binding code...

Unhandled Exception:
System.InvalidOperationException: Sequence contains no elements
  at System.Linq.Enumerable.Max (System.Collections.Generic.IEnumerable`1[T] source) [0x0001d] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/Max.cs:23 
  at Embeddinator.XamarinAndroid+<>c.<.cctor>b__4_1 () [0x00000] in /Users/joao/Dev/Embeddinator-4000/binder/Utils/XamarinAndroid.cs:112 
  at System.Lazy`1[T].CreateValue () [0x00081] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/Lazy.cs:443 
  at System.Lazy`1[T].LazyInitValue () [0x00080] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/Lazy.cs:388 
  at System.Lazy`1[T].get_Value () [0x0003a] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/Lazy.cs:339 
  at Embeddinator.XamarinAndroid.get_ApiLevel () [0x00000] in /Users/joao/Dev/Embeddinator-4000/binder/Utils/XamarinAndroid.cs:119 
  at Embeddinator.Driver.CompileNDK (System.Collections.Generic.IEnumerable`1[T] files) [0x00115] in /Users/joao/Dev/Embeddinator-4000/binder/Compilation.cs:669 
  at Embeddinator.Driver.CompileNativeCode (System.Collections.Generic.IEnumerable`1[T] files) [0x000ba] in /Users/joao/Dev/Embeddinator-4000/binder/Compilation.cs:722 
  at Embeddinator.Driver.CompileCode () [0x000a6] in /Users/joao/Dev/Embeddinator-4000/binder/Compilation.cs:50 
  at Embeddinator.Driver.Run () [0x000a5] in /Users/joao/Dev/Embeddinator-4000/binder/Driver.cs:250 
  at Embeddinator.CLI.Main (System.String[] args) [0x00092] in /Users/joao/Dev/Embeddinator-4000/binder/CLI.cs:215 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Sequence contains no elements
  at System.Linq.Enumerable.Max (System.Collections.Generic.IEnumerable`1[T] source) [0x0001d] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/Max.cs:23 
  at Embeddinator.XamarinAndroid+<>c.<.cctor>b__4_1 () [0x00000] in /Users/joao/Dev/Embeddinator-4000/binder/Utils/XamarinAndroid.cs:112 
  at System.Lazy`1[T].CreateValue () [0x00081] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/Lazy.cs:443 
  at System.Lazy`1[T].LazyInitValue () [0x00080] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/Lazy.cs:388 
  at System.Lazy`1[T].get_Value () [0x0003a] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/Lazy.cs:339 
  at Embeddinator.XamarinAndroid.get_ApiLevel () [0x00000] in /Users/joao/Dev/Embeddinator-4000/binder/Utils/XamarinAndroid.cs:119 
  at Embeddinator.Driver.CompileNDK (System.Collections.Generic.IEnumerable`1[T] files) [0x00115] in /Users/joao/Dev/Embeddinator-4000/binder/Compilation.cs:669 
  at Embeddinator.Driver.CompileNativeCode (System.Collections.Generic.IEnumerable`1[T] files) [0x000ba] in /Users/joao/Dev/Embeddinator-4000/binder/Compilation.cs:722 
  at Embeddinator.Driver.CompileCode () [0x000a6] in /Users/joao/Dev/Embeddinator-4000/binder/Compilation.cs:50 
  at Embeddinator.Driver.Run () [0x000a5] in /Users/joao/Dev/Embeddinator-4000/binder/Driver.cs:250 
  at Embeddinator.CLI.Main (System.String[] args) [0x00092] in /Users/joao/Dev/Embeddinator-4000/binder/CLI.cs:215 

For environment:

Visual Studio Community 2017 for Mac (Preview)
Version 7.4 Preview (7.4 build 839)
Installation UUID: 311d43a2-75f0-41a4-b610-08c0d230325b
Runtime:
    Mono 5.8.0.103 (2017-10/1359c52fc6e) (64-bit)
    GTK+ 2.24.23 (Raleigh theme)

    Package version: 508000103

NuGet
Version: 4.3.1.4445

.NET Core
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    2.0.0
    1.1.0
    1.0.2
    1.0.0
SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks
SDK Versions:
    2.0.0
    1.0.0-preview2-1-003177
    1.0.0-preview2-003148
    1.0.0-preview2-003121
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.8.0/lib/mono/msbuild/15.0/bin/Sdks

Xamarin.Profiler
Version: 1.6.1
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Xamarin.Android
Version: 8.2.0.1 (Visual Studio Community)
Android SDK: /Users/atsushi/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
        2.3    (API level 10)
        4.0.3  (API level 15)
        4.1    (API level 16)
        4.4    (API level 19)
        4.4.87 (API level 20)
        5.0    (API level 21)
        6.0    (API level 23)
        7.0    (API level 24)
        7.1    (API level 25)
        8.0    (API level 26)
        8.1    (API level 27)

SDK Tools Version: 26.0.2
SDK Platform Tools Version: 26.0.0
SDK Build Tools Version: 27.0.1

Java SDK: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Apple Developer Tools
Xcode 9.2 (13772)
Build 9C40b

Xamarin.Mac
Version: 4.2.0.1 (Visual Studio Community)

Xamarin.iOS
Version: 11.8.0.1 (Visual Studio Community)
Hash: 82e38cd1
Branch: d15-6
Build date: 2017-12-22 14:23:52-0500

Xamarin Inspector
Version: 1.4.0-beta1+69
Hash: 4012b4e
Branch: master
Build date: Thu, 07 Dec 2017 16:20:26 GMT
Client compatibility: 1

Build Information
Release ID: 704000839
Git revision: 82eabac6f9ebeaa9c0f3143baa8037c26c6c00fc
Build date: 2018-01-05 22:22:29-05
Xamarin addins: b2cfc17e56386f2c360aaa8b02404b900d6be8c1
Build lane: monodevelop-lion-d15-6

Operating System
Mac OS X 10.13.2
Darwin 17.3.0 Darwin Kernel Version 17.3.0
    Thu Nov  9 18:09:22 PST 2017
    root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64

Enabled user installed addins
AddinMaker 1.4.1
Paket 0.3
Internet of Things (IoT) development (Preview) 7.1
tritao commented 6 years ago

Please test with master (0.4.0), there are some issues updating NuGet due to the migration to MS namespace.

atsushieno commented 6 years ago

Just for some updates; I'm getting closer to get it working. Now with the updated https://github.com/mono/Embeddinator-4000/pull/566 , I could successfully finish the command below i.e. GenerateJavaStubs issue is gone:

TargetFrameworkRootPath=/sources/xamarin-android/bin/Debug/lib/xamarin.android/xbuild-frameworks MONO_ANDROID_PATH=/sources/xamarin-android/bin/Debug/ mono --debug -O=-all /sources/Embeddinator-4000/build/lib/Debug/Embeddinator-4000.exe --gen Java -p Android -out android -d -v /home/atsushi/Desktop/TextToSpeechPlugin-master/src/TextToSpeech.Plugin.Android/bin/Debug/Plugin.TextToSpeech.dll 

This was achieved by adding another non-public target dependency https://github.com/mono/Embeddinator-4000/pull/566/files#diff-aad38e8 . Also, this means that there should still be TargetFrameworkRootPath setter somewhere.

And with -c option, I'm getting this error (maybe this is due to insufficient fixes...)

Compiling binding code...
    Invoking: /home/atsushi/android-ndk-r15b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --sysroot="/home/atsushi/android-ndk-r15b/platforms/android-24/arch-arm" -fdiagnostics-color -DMONO_EMBEDDINATOR_DLL_EXPORT -I"/usr/include/mono-2.0" -L"/sources/xamarin-android/bin/Debug/lib/armeabi" -lmonosgen-2.0 -lmono-android.release android/Plugin.TextToSpeech.c android/Mono.Android.c android/Java.Interop.c android/mscorlib.c android/Plugin.TextToSpeech.Abstractions.c android/c-support.c android/glib.c android/mono-support.c android/mono_embeddinator.c --std=c99 -shared -o android/android/jni/armeabi/libPlugin.TextToSpeech.so
        /home/atsushi/android-ndk-r15b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lmonosgen-2.0
/home/atsushi/android-ndk-r15b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lmono-android.release
collect2: error: ld returned 1 exit status
Zomb commented 6 years ago

@atsushieno were you able to solve the error: cannot find -lmonosgen-2.0 error? I am running into the same issue on Windows and I am not sure how to fix it.

atsushieno commented 6 years ago

I have a pending PR to fix toolchain location lookup at https://github.com/mono/Embeddinator-4000/pull/566 , but it is reportedly not working on Windows...