MSbuild Xamarin.Forms.Android project not building when using buildscript

I'm trying to build an Xamarin.Forms Android project from within another visualstudio project.
My idea was to do that by executing a cmd command, or a .bat file.

I tested the msbuild command that builds the Xamarin.Forms.Android project:

MSBuild /t:SignAndroidPackage /p:Configuration=Release C:\Users\...\...\...\MyApp.Android.csproj

this command is working in the "Developer command prompt for VS 2017"
If I execute this in cmd.exe I get errors:

what I want is to get the command working in cmd.exe, or open the dev cmd vs2017 from a script.
I learned that the dev cmd vs 2017 is just a batfile that gets called before cmd.exe, so I did the same in my build.bat:

CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
MSBuild /t:SignAndroidPackage /p:Configuration=Release C:\Users\...\...\...\MyApp.Android.csproj
pause

But this still results in an error, I tried different bat files: vsvars32.bat, VsDevCmd.bat, as admin, added my MSBuild line directly in these files, It all ends with multiple errors:

The type 'IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstand
ard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

this error repeats a few times with different types. But I don't think the error is the problem cause my Xamarin.Forms.Android project build fine if I open the Dev cmd vs2017 manual, and enter the same MSBuild command.

Anyone who knows more about this problem, or can help me solve it?

Best Answer

  • washichiwashichi USMember
    Accepted Answer

    I found a way to get it working:

    var processInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\Tools\\VsDevCmd.bat");
    processInfo.WindowStyle = ProcessWindowStyle.Hidden;
    var process = Process.Start(processInfo);
    process.WaitForExit();
    process.Close();
    

    in my VsDevCmd.bat (as last command to execute) I added the MSbuild command:
    MSBuild /t:SignAndroidPackage /p:Configuration=Release C:\Users\....\App\App.Android\App.Android.csproj

    This only works If I execute one command from my program (I can't run the .bat file and then the MSbuild command)
    and It only works if I run VsDevCmd.bat from its original location,
    so it's a dirty fix, but it works.

Answers

  • washichiwashichi USMember
    edited October 17

    I found a dirty fix to solve the problem;

    created a function that calls the developer command prompt for VS2017:
    private bool BuildApk() { try { var processInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\Tools\\VsDevCmd.bat"); processInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = Process.Start(processInfo); process.WaitForExit(); process.Close(); return true; } catch (Exception e) { Console.WriteLine(e); return false; } }

    In VsDevCmd, as last action before goto:end I added this command :
    MSBuild /t:SignAndroidPackage /p:Configuration=Release C:\..........\Program\Program.Android\Program.Android.csproj

    This is only working if I add the MSBuild command inside the file, and execute it from the original location, so it's a dirty solution, but it works.

  • washichiwashichi USMember
    Accepted Answer

    I found a way to get it working:

    var processInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\Tools\\VsDevCmd.bat");
    processInfo.WindowStyle = ProcessWindowStyle.Hidden;
    var process = Process.Start(processInfo);
    process.WaitForExit();
    process.Close();
    

    in my VsDevCmd.bat (as last command to execute) I added the MSbuild command:
    MSBuild /t:SignAndroidPackage /p:Configuration=Release C:\Users\....\App\App.Android\App.Android.csproj

    This only works If I execute one command from my program (I can't run the .bat file and then the MSbuild command)
    and It only works if I run VsDevCmd.bat from its original location,
    so it's a dirty fix, but it works.

Sign In or Register to comment.