Xamarin – Setting Up Your Environment

移动开发 DZone

Xamarin is a solution to start developing cross-platform apps. It gives you a smooth user interface with native output for iOS, Android, and even UWP.

With Xamarin.Forms, the user interface is dedicated to all three platforms using one framework: XAML. It’s very interesting if we are able to release an app with the code sharing for all three platforms.

This course aims to introduce the Xamarin multi-platform development framework in my way of learning.

Xamarin is well documented with free and paid resources. The best to start with is Xamarin University . There’s no limit to the knowledge you can obtain.

If we start learning Xamarin, we will find two approaches to development. The first one is to use Xamarin.Forms, where you will build everything in a portable project using XAML and C# or using Xamarin.Android or Xamarin.iOS. In this case, we will use specific native environments like StoryBoard for iOS with C#.

This is an overview about Xamarin:

“Xamarin’s not one thing, but many.”

This is a small description related to Xamarin. You can find more documents describing the advantages and disadvantages.

Xamarin lets you create native apps using C#. I will start by defining the hardware and software requirements, you will get what you need to know to start building an application even with no mobile experience.

Background

  • You should have a basic knowledge of C# and some mobile basis.

Setting Up the Environment (Windows and iOS)

It is important to note that the tools evolve very fast and it may be that, depending on when you read this post, it is not as useful.

If you want to build an Android application, you need Visual Studio 2017 in Windows or Visual Studio 2017 Mac. If you build the iOS application, you can use Visual Studio 2017 in Windows, but you have to compile using a Mac or use Visual Studio 2017 Mac; this is imposed by Apple.

System Requirements

Windows:

  • Windows 10.
  • Core i7, 16 GB RAM (to work more comfortable and the minimum requirement can be Core i5, 4GB RAM).
  • Visual Studio 2017 (In this case it is not important whether the version is Enterprise or Community).

Mac:

  • Mac OS 11 (until writing this document, but we use always the last version).
  • Core i7, 16 GB RAM, and 256 SSD.
  • Visual Studio for Mac.
  • XCode 9 (or the last version).

UWP

UWP only works on Windows and its installation is very simple. Select the option “Development with the Universal Platform of Windows” as an option to install the environment for UWP development, and the choice of SDKs: IntelliTrace and SDK for Windows 10.

Setting Up the Environment

You can find all needed requirements for Windows or iOS in this documentation . For both Windows and iOS, we have to install the latest version of Visual Studio. At the time of writing this article, we have Visual Studio 2017 (for Windows and for MAC).

For Windows

We can install what we want: community, professional, or enterprise. We choose mobile development with .NET.

For iOS

The installation of Visual Studio for Mac is very similar to that of Visual Studio for Windows. You can choose the components, and at this point, you may prefer not to install the Android SDK, since you will already have it installed in Windows. Now, if you do not use Windows, you will have to install the Android SDK, and the process I will describe below can be applied to Mac without any problem.

If you are on a Mac, the website will detect that and display the download for the community version or others:

The iOS simulator is not an emulator. This is a big difference. There are things that only work on the physical device.

Simulators are installed with XCode. By default, when installing the IDE, some are already installed, but if you want to add some more, click on the code, then go to Preferences, Downloads, Components, and select the simulators you want. Whenever you install/update XCode, run it. Generally, a window for accepting the terms of use will appear. If you do not accept the terms, Visual Studio for Mac may not identify the simulators.

Check here for more details.

Prerequisites for Android

Android basically needs the following components:

  • Java 64-bit SDK (JDK)

  • Android SDK

  • Android NDK is optional, but I install it anyway

When you install Xamarin via Visual Studio, it already installs all these components. However, until the latest version, the installer downloads the 32-bit JDK, which is incompatible.

Another point is that it installs them in C:/Program Files/RepositoryName; this is not safe because you can get an error related to the character size of the path that may exceed 256 characters, and in this path, you can have a space, which can cause many problems.

So, to prevent all these problems, you install each one manually in C:/ and then add its paths in Windows environment variables:

C:/java

C:/android-sdk

C:/android-ndk

Placing these components in environment variables ensures that if I need to use any other development platform that also needs these components, they will already be there, ready for use.

Installing JDK

Download it from this link . Make sure this is the most current version.

Once installed, add the following system variables:

JAVA_HOME with the value C: / java / jdk {installed version}

CLASSPATH with the value.;% JAVA_HOME%  lib;% JAVA_HOME%  jre  lib

Edit the path variable by adding the following value: % JAVA_HOME% bin

Now open the command line and type java -version . If all goes well and nothing goes wrong, the Java version will be displayed.

Installing Android NDK

Download the zip here and unzip it in C:/android-ndk. Add the variable ANDROID_NDK_HOME with the value C:/android-ndk . Unzip the content, making sure it does not have a folder inside the other.

Installing Android SDK

On the website, this is will be installed with Android Studio, but in Visual Studio 2017, this will be integrated it so there is no need to add it, but you can also add Xamarin Android SDK Manager using this link .

Create a Cross-Platform Xamarin Project

You can get started with a new Xamarin.Forms app by going to File -> New -> Visual C# -> Cross-Platform -> Cross-Platform App. This template allows you to select the starter project, the platforms you plan on targeting, and your code sharing strategy.

Before, we had this display:

“Portable Class Libraries allow you to write code and produce libraries that can be shared across multiple platforms including Xamarin.iOS, Xamarin.Android and Windows Phone.” ( Source )

But now, the Portable Class Library is replaced by .NET Standard:

.NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET runtimes (such as .NET Framework, Mono, and .NET Core), according to the documentation .

Source .

You can also include Azure Mobile Apps-backed projects.

The next step is to choose between compiling using Emulators or your device, but recently they announced a new, faster tool for Xamarin and Visual Studio for Mac feature to compile and render your application: Xamarin Live Player app, installed on your iOS or Android device.

Emulators

When I started Workshop, I was facing many problems to work with Emulators; Windows 10 Mobile emulators use Hyper-v. We tried to install GenyMotion as an Android emulator, and it uses Virtual Box. Hyper-v and Virtual Box do not work on the same machine. There may even be some way to make them work together; GenyMotion allows you to get more devices (for personal use), but as I said before it requires specific configuration ( learn more ), and the machine used in the training room doesn’t have this characteristic.

After this, to minimize the use of the machine memory, we used our mobile (Samsung, Huawei…), installed a USB driver, and started to compile and see the result, but it’s always a problem for some mobiles.

Xamarin Live Player

Visual Studio has a Forms Previewer tool that’s supposed to show the view on the fly, but it never works. Everybody is complaining about it, so Visual Studio released another tool: Xamarin Live Player. It provides real-time feedback to developers while fitting into their existing mobile development experience. By pairing your device to the IDE, we are able to continually deploy, test, and debug the app directly on the device.

So, how we can use it?

It’s simple. We select a new target from our device.

Visual Studio App Center (Before Xamarin Test Cloud)

After creating a new application and development, developers are facing a lot of challenges in the testing process. To proceed with a continuous integrated delivery, you need an environment that lets you test on many devices.

In the mobile cycle, an application starts with a design, then development, then we go through the test and monitoring, but if the tests aren’t as we expected, we come back to the design and development, so we try to iterate, develop, and maintain.

Microsoft offers a lot of services, like HockeyApp , Xamarin Test Cloud , Xamarin Insights (which joined HockeyApp after), Azure Mobile Engagement, Azure App Service, and CodePush. After Microsoft created the Visual Studio App Center that collects all in one place. They have taken all of these different solutions and they brought them together in one single central application.

To start your build functionality, you connect to an existing code repository in the cloud from VSTS or GitHub or BitBucket. Along with this build process, it has also a device lab with over 300 configurations of Android and iOS devices that you can run your automation scripts on. It supports a variety of frameworks, like Appium, Espresso, XCUITest, and Xamarin.UITest.

Once you have been through the build task process, you go to the distribution to users. You can create groups, both for internal test groups in your company or for beta testers, and send your app directly to them as well as give them live in-app updates. Once you’re ready to publish your app to Google Play or iTunes, connect to the app center .

Once the application is published and used, developers want to know what is going on and to be able to maintain it in case of a crash. You can get real-time stack traces in crash reports from the fields of your app.

Along with a crash, we need to know some analytics to better understand your customers, so from the app center, we can log custom events. Finally, it’s a single source for push notifications.

DZone稿源:DZone (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Xamarin – Setting Up Your Environment

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录