C# .dll library in Android app

2019-02-09 05:12发布

问题:

I'm currently using Monodroid to develop an android app. I'm rather new to phone apps so am confused over a couple things:

  1. What by definition is an android library? I use the Mono template to create android library: Create a Mono for Android Class Library => write the code and build into .dll => run with emulator from Visual Studio and it works fine in the emulator. But is the .dll file a android library? Or it is converted by Mono before working on the emulator and the 'REAL' android library should be JAR?

  2. Can I directly use my defined .dll written in C# to make this app? I tried it in Mono by directly referencing it in my android app and it is building, no errors. But when the emulator run this app, it get stucked (still no error) - what is the right way to use c# .dll in Android app project?

I realize there are quite a couple topics discussing the link between c# and android and about mono but after reading I still cannot get around with this library referencing problem so if anyone could offer any hint/thoughts would be most appreciated

回答1:

  1. The DLL generated is compiled against the Mono for Android profile, which is based on Moonlight (Mono's implementation of Silverlight). At runtime, .NET code is run directly against the Mono runtime which runs as part of your application. When .NET code subclasses something from the Java side (e.g. Java.Lang.Object), Mono for Android generates callable wrappers to handle communication between runtimes.
  2. Technically you can use a normal .NET DLL in your Mono for Android application, but I wouldn't recommend it. The profile exposed by Mono for Android is much smaller than that of the full .NET framework (and isn't even completely equivalent to that of Silverlight), so if your DLL references a method that isn't found in Mono for Android, your application will crash. Instead, I would recommend creating a separate class library for your application, and share source files across projects using file linking. My blog post here contains an example of how to do that.