Adding existing C# library project

Hi,

I have created an C# Android Application and seen it working fine in the emulator.
And I have added my existing C# library which was made in VS2010 with .Net Framework 4.0
But when I tried to add it to the reference, it says (Imcompatible target framework: .NetFramework, Version=v4.0).
How can I make .Net Framework 4.0 compatible to the Android application?

Posts

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    How are you adding your existing C# library? As a project reference or as a file reference? As far as I know, you shouldn't be able to reference a .NET library project from and Android Application project...

    Assuming you're referencing a .dll, you need to rebuild the .dll from source within an Android Library project. You can do this by creating a new Android Library project and link your source code into it.

  • 9tontruck9tontruck CAMember

    So basically I can't directly import my .Net class library which was built in VS2010?
    I should re-write all of the c# code in Android Library project again right?

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    So basically I can't directly import my .Net class library which was built in VS2010?

    Not directly.

    I should re-write all of the c# code in Android Library project again right?

    You shouldn't need to rewrite your existing C# code. You just need to link your existing C# code into an Android Library project and fix any build errors due to missing types/assemblies/etc.

    Please see the Setting Up A Xamarin Cross Platform Solution documentation for more information.

  • 9tontruck9tontruck CAMember
    edited April 2013

    Thanks for the replies, but do you have any examples about linking? Sorry I am pretty new to linking projects.

    Let's assume I created a "Visual C# Class Library" in VS2010 like:
    BaseClass.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace BaseClass
    {
        public class BaseClass
        {
            public string getStringTest()
            {
                return "hi Android from C#";
            }
        }
    }
    

    and VS2010 gives me BaseClass.dll once it's built.

    And I want to display the message from getStringTest() on the Android/IOS emulator using Xamarin Studio without rewriting anything. What is the next step I should do?

    Would you please write me some steps simply?

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    Within your Android Application solution:

    1. Within Solution Explorer, right-click the Solution node > Add > New Project...
    2. Within the Add New Project dialog, in the left-hand tree select Visual C# > Android. Within the middle panel select Android Class Library. Enter a Name such as BaseClass.Android, then click OK.
    3. Within Solution Explorer, right-click the BaseClass.Android project > Add > Existing Item... Navigate to your C# source code from your BaseClass.dll project and select the file. To the right of the Add button is a downward-facing arrow; click it, then click Add As Link.

      Add As Link

    4. Within Solution Explorer, right-click your Android Application project's References node > Add Reference.... In the left-hand tree, select Solution > Projects. In the middle panel select your BaseClass.Android project, then click OK.

    5. Build.

  • 9tontruck9tontruck CAMember

    Awesome.. Thanks for the reply.
    I have one last question :D
    After adding BaseClass.Android project into reference, I should make an object of BaseClass ** and call **getStringTest() but the Android Application project does not know if there is such a class and a method in the linked dll.

    I believe I should make an interface of the class. Would you please give me the last advice to complete to call getStringTest()?

    Thanks,

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    After adding BaseClass.Android project into reference, I should make an object of BaseClass and call getStringTest() but the Android Application project does not know if there is such a class and a method in the linked dll.

    Odd; it should work. Does it change if you close and re-open the Solution?

    I believe I should make an interface of the class.

    You should only need to migrate to interfaces and (optionally) Inversion of Control (IoC) containers if you're dealing with platform-specific code and you don't want #ifs in your codebase. You are not yet at that point, not for BaseClass.getStringTest(). Something else is going awry.

  • 9tontruck9tontruck CAMember

    Hi Jonathan,

    I tried a couple of times more and my Android application project can't still find BaseClass from the linked dll. Can I send you my sample code files through email and let you take a look please? If you are okay, can you let me know your email addr?

  • 9tontruck9tontruck CAMember
    edited April 2013

    Okay.

    When I add BaseClass.cs as a link, it works.
    But adding BaseClass.dll as a link does not work.
    I do need to know how to import .dll file because of the complexity of my library.
    Can you please tell me how?

  • FZelleFZelle DEMember ✭✭✭

    Is it realy that hard to understand?

    1. You can't just compile a DLL for Windows and then ad this as a reference to an Android Project.
    2. So you have to compile a Android Class Library and then add the reference (like in every .NET project ) to your project.
    3. So you don't have to rewrite everything you have done in Windows you link the CS files into this class library project.
  • 9tontruck9tontruck CAMember

    Dude. I know how to import and add my cs files into the Android Application project.
    But Jonathan mentioned that he can add the ".dll" file into Android Library project as a link and it should all work fine. Did I understand his reply incorrectly?

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    But Jonathan mentioned that he can add the ".dll" file into Android Library project as a link and it should all work fine. Did I understand his reply incorrectly?

    Yes, you misread my reply. I wrote:

    ... Navigate to your C# source code [ed: emphasis added] from your BaseClass.dll project and select the file.

    Emphasis on source code. Assemblies are not source. I did not mean to imply in any way that you could add binary assemblies as links to a project.

  • SreerajSASreerajSA USMember

    I also have the same problem which Devon mentioned. How can I create an object of a Class inside the .Net assembly which is linked with .Android Assembly?

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    You might consider using Portable Class Libraries. Create a new library of type PCL (make sure that checkbox Xamarin.Android is checked) and reference it from Android application.
    These type of libraries can be shared among different platforms.

Sign In or Register to comment.