Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】
本文内容
- 为什么需要一个 Android 模拟器
- 针对 Visual Studio Android 模拟器的调试
- Visual Studio Android 模拟器的传感器模拟和其他功能
- A peek under the covers
- 当前的限制
北京时间今天(2014年11月12日)凌晨——.NET 开源……集成 Clang 和 LLVM,并且自带 Android 模拟器,这意味着 Visual Studio 这个当下最好没有之一的 IDE 正式支持编写 Android 和 iOS 程序。
本周微软发布了 Visual Studio 2015 预览版,有了它,你就可以选择用 C++,Cordova 或 Xamarin C# 进行 Android 开发。当选择他们其一时,Visual Studio 将安装全新的 Visual Studio Android 模拟器调试你的应用程序。参看 see it in action in this video。
在我介绍这个新的模拟器前,先来谈谈为什么要建立一个 Android 模拟器——你也可以直接去下一章看有趣的部分 :-)
为什么需要一个 Android 模拟器
我们知道,模拟器可以在“编辑 - 编译 - 调试周期”中扮演重要作用。
用一个不错的模拟器调试并不意味着你不需要的设备,而用一个设备调试也并不意味着你不会从一个好的模拟器中受益。它们是互补的。
你必须用一个设备测试下面情形,而这对任何的模拟器并不合适:
- 1,Measuring the performance characteristics of your code. While an emulator can help you with correctness issues, it will never perfectly emulate the performance characteristics of your code running on the actual devices that you want to test against. You want to measure the performance as your users see it.
- 2,Testing hardware-specific issues. If what you are trying to test is the touch-responsiveness of your game, or the speaker quality for your media app, you will want to do that type of testing on the target devices. Ditto if you are trying to work around an OEM-specific bug.
- 3,Purely evaluating the actual user experience in real-world situations, e.g. do your designed interactions work for a user walking around using your app one handed with just their thumb alone?
对于所有的其他测试,这是你“编辑 - 编译 - 调试周期”的一部分一般会占用你至少 80% 的时间,你就要使用仿真器(不包括其他阻塞问题,或是你选择的模拟器的限制)。使用仿真器的原因如下:
1,The majority of your testing is for correctness issues (not performance) and the majority of your code is probably not dealing with hardware specific issues. So use an emulator!
2,You don’t want to spend a bunch of money buying a bunch of devices (and keep doing so every time a new device appears on the market), just to test things like screen resolution, DPI settings for different screen sizes, different API levels / platform versions, when you can configure that in software (in an emulator).
3,You don’t want to have to take physical action with your device to test some sensor, e.g. respond to movement or location changes or simulating network/battery changes. Instead you want to simulate the sensor values easily and quickly in an emulator, e.g. simulate a trip to another town while your app responds to the change of location.
4,There is also the convenience element. Connecting to a device (typically dealing with cables), managing that connection and its lifetime, using one of your USB ports, is not as simple as launching the emulator and treating it like every other desktop application running on your dev machine.
So emulators are great and can be a key part in the edit-compile-debug cycle and we want to make sure that our emulator is best-in-class. You have told us about several pain points with existing emulators that we are starting to address with our release:
- 慢. This is the number one complaint we’ve heard from Android developers. “The emulator is painfully slow, it hurts my productivity, and I’ll use a device.” Slow is not acceptable. If anything, using the emulator should be faster than using a device so you can test your scenarios faster (remember, you are not using emulators to test the performance of our code, you just need them to be as fast as possible for your own use).
- 与 Windows 的 Hyper-V 冲突. Many emulators require you to disable Hyper-V or don’t work as well with Hyper-V as they do without. Using Hyper-V is part of the development setup for many developer activities, so asking you to restart your machine (multiple times a day) to toggle Hyper-V is not acceptable.
- o One specialized variant of this is using the Windows Phone emulator (which itself is based on Hyper-V). It is a real pain having to make changes and reboot every time you want to switch from an Android emulator to a Windows Phone emulator to test your cross-platform code.
- 其他需要和安装步骤. If your main development environment is Visual Studio, you don’t want to have to acquire the emulator separately and follow a separate installation process.
- Separate cost. Having a great emulator that can cost you as much as your main development environment is not an option for most. The Visual Studio Emulator for Android comes with VS without additional charge.
总之,我们将用 Visual Studio Android 模拟器解决所有这些痛点。现在,让回顾一下 Visual Studio 调试 Android 的事情,以及如何选择 VS Android 模拟器。
针对 Visual Studio Android 模拟器的调试
用 Visual Studio2015 预览版可以针对 Android “编辑 - 编译 - 调试”,而不管你选择的编程模型是什么:Cordova JavaScript (or TypeScript),C++,或 Xamarin C#。
对于这三个选择,当你开始调试时,必须先选择一个目标。该目标可以是设备,也可以是你运行在你的机器上的多个模拟器之一。下面让我们看看如何在 Visual Studio2015 预览版中为 Cordova 和 C++ 以及在 Visual Studio2015 中为 Xamarin 选择调试目标。
对于 C++ 项目,调试目标菜单如下所示:
对于 Cordova 项目,你可以选择菜单下面最后两项,如下所示:
(避免选择“Android Emulator”选项,因为这附带 SDK 很慢)
对于 Xamarin 项目,选项如下所示:
For best results with Xamarin projects, disable/uncheck "Use Fast Deployment" under the Android Options under the Xamarin Project Properties.
注:如果您想从一个不同的 IDE 使用 VS Android 模拟器,临时的解决方法是,通过以上的选项,你总是随时从 Visual Studio2015 启动模拟器,然后关闭该项目,并保持模拟器运行,并让的其他 IDE 对其他目标可用(over ADB)。
一旦你选择了你的调试目标,并按下 F5 键,你的应用程序将会被部署到模拟器,跟常规 VS 调试流程一样,你可以下断点,查看调用堆栈,检查变量等等。现在你知道如何使用模拟器进行调试,下面让我们继续探索一些很酷的功能!
Visual Studio Android 模拟器的传感器模拟和其他功能
除了使用模拟器作为部署目标,你还可以利用传感器仿真和其他功能——如下介绍,介绍不分顺序。
放大
You can change the size of the emulator as you see it on your development machine (the host). The dots per inch (DPI) for the emulator is based on the host monitor DPI, regardless of the zoom value. This allows you to scale the emulator in case it is taking too much space on your desktop.
To change the size, use the “Zoom” button on the emulator’s vertical toolbar.
You can also use the “Fit to Screen” button above the “Zoom” button to fit the emulator on your screen.
If you are going to take screenshots of your app running in the emulator (e.g. with the Snipping tool) for best results remember to set the zoom level to the maximum of 100% - or even better, use our built-in Screenshot tool support that I describe below.
方向/旋转
Unless your app only supports a fixed orientation, you should test how your app responds to orientation changes, and what it looks like in portrait, left-landscape, and right-landscape orientations. Simply rotate the emulator left or right with the two corresponding buttons on the vertical toolbar: “Rotate Left” and “Rotate Right”. The size of the emulator remains the same when you rotate.
网络信息
The emulator reuses the network connection of the host machine, so there is nothing for you to configure.
You can also review the emulator’s current network settings. On the vertical toolbar click on the “Tools” button to show the “Additional Tools” fly out panel, and then click on the “Network” tab.
定位(GPS)
If your app does anything with navigation, geofencing, walking/biking/driving, then you will love the location and driving simulation in the emulator under the “Location” tab when you open the “Additional Tools”.
You can navigate the map by dragging it around, by zooming/in and out, or even by searching for a location. You can place and remove pins on the map, thus creating map points. Those appear as latitude longitude coordinates in the list in the bottom left. From the toolbar at the top you can even save those map points to an XML file and later load them from the file.
Instead of having each map point immediately change the GPS location of the emulator (“Live” mode), you have other options too! You may want to place a few map points and then simulate transitioning between those points. To do that, at the toolbar at the top switch from “Live” mode to “Pin” mode. Then you can press the small play button at the end of the toolbar to transition between the map points. You can even enter a transition interval (in seconds).
Finally, you can choose a third mode that is similar to “Pin”, which is called “Route” mode. In this mode you can also simulate transitions between the points but with some additional twists. The simulator will calculate an actual path between the points and generate invisible points at 1 second intervals between the points you choose. The overall speed at which it will play those points is determined by a second setting and your options are: “Walking” (5 kilometers per hour), “Biking” (25 km/h), “Speed Limit” (variable dependent on map point), and “Fast”.
加速度计
If your app tracks and responds to movement of the phone, you can test them using the “Accelerometer” tab when you open the “Additional Tools”.
Simply click and hold the red dot in the middle and drag it towards the directions you want to simulate, within the 3D plane. As you do that your app will receive movement events if it has registered for them.
You can also see the X, Y, Z values in the bottom left. Under those values you can “Reset” to the starting position, and also pick the starting Orientation from these values: Portrait Standing, Landscape Standing, Portrait Flat, and Landscape Flat.
Lastly you can simulate the phone shaking by clicking the “Play” button in the bottom right. The only visual indication that a shake is taking place are the values of the X,Y,Z and when they stop rapidly changing you’ll know the shake is over.
电源/电池仿真(和电源按钮)
If you write your app to respond to battery charge changes, then you will like the emulator’s ability to simulate that by switching to the “Battery” tab when you open the “Additional Tools”.
There is a slider that allows you to set the exact charge value of the battery. Notice as you slide down/up how the battery icon in the top right changes to reflect the change. Your app can also respond accordingly.
If you change the Battery Charging State to not be “Charging”, then the emulator’s screen will go blank after a timeout period. You can configure the timeout though the built-in regular "Settings" app (look for the “Sleep” option under “Display”). If the emulator sleeps due to this, then you can wake it up through the “Power” button on the vertical toolbar.
截屏
To take a screenshot of your app, open the “Additional Tools” and switch to the “Screenshot” tab. Then click on the “Capture” button, which will take a screenshot and show you an instant preview. If you want to keep the screenshot click on the “Save…” button. If you don’t like the screenshot you took, ignore it or click “Capture” again.
The screenshot tool always takes screenshots at 100% (indicated by the resolution in the bottom left corner), regardless of Zoom setting. They are also always portrait, regardless of rotation chosen.
通过拖拽安装 APK
You install apps on Android through an application package file which is known as an APK. If you have an APK that you want to install on the Visual Studio Emulator for Android, just drag it onto the emulator from Windows Explorer. You will see a message in the emulator indicating progress “File transfer in progress…” followed by a message box “File foo installed successfully in Android”. Remember to make sure your APKs have code built for x86!
You can also drag and drop other (non-APK) files to the emulator and they will be placed onto the SD Card, which brings us to the next topic.
SD 卡
If your app has a need to read or write to the SD card of the target, the emulator simulates that by making available a folder representing an SD card.
Note that the Android image uses a separate VHD for SD card support. So if you want to transfer files to/from the SD card on your development machine, you can mount the VHD to Windows: Close the emulator (to shut down the VM), then navigate to the VHD location in Windows explorer, and double click the VHD to mount it. By default the VHD is located under the path:
C:\Users\%username%\AppData \Local\Microsoft\XDE\Android\vsemu.sdcard.vhd
At this point, the VHD is mounted as an additional drive to Windows and you can use it pretty much like any other drive. Before restarting the emulator you must un-mount the VHD, which you can do by right clicking on the drive and selecting Eject.
Having SD card support in the image also allows other built-in Android apps to function, such as the browser downloads and the camera app – which brings me to the next capability.
摄像头
Typically you’d be using the camera from your app (using an appropriate API), and we support that. You can also use the built-in camera app directly. When you launch the camera in the emulator you will see a fixed animated image that you can take a snapshot of, simulating taking a picture.
音频回放,键盘文本输入……
There are other capabilities that the emulator provides that are taken for granted, even though they require “work” from the product team :-). I won’t list them all here but two of them are that:
you can use your computer’s keyboard to enter text in the emulator
any audio coming from the emulator can be heard through your computer’s speakers
配置
With this Preview release you can pick between two out of the box configurations:
Typical Android Phone: 5” Screen, 295 PPI, 720x1280, 1024 MB
Typical Android Tablet: 7” Screen, 315 PPI, 1080x1920, 2048 MB
With the Preview bits if you want to change the amount of memory, you can change the Startup RAM in the Settings dialog from the Hyper-V Manager. Notice that there you can also change the number of cores allocated to each configuration (the default for Preview is 2 cores). Caveat: we have not tested all possible configurations you could choose!
We are just getting started, there is a lot more to come in subsequent releases and you can help us prioritize new sensor simulation and other capabilities by taking our survey.
A peek under the covers
If you are interested in how we built the Visual Studio Emulator for Android, the short answer is that we reused the work of others. Conceptually, an emulator consists of 4 pieces:
1,A virtual machine (represented as a vhd) of the target you are emulating, in this case Android. We started with the source at the Android Open Source Project (AOSP), evolved it, and configured an x86 virtual image for fast Visual Studio debugging.
2,A small shell/chrome that as a user you see and interact with, which loads the virtual image and projects it through a rendering control. Think of this as remote desktop: you are essentially RDPing to the image. We started with the desktop application that is the shell/chrome of the Windows Phone Emulator (internally known as XDE), which is already rich in functionality. Then we made modifications for our Android-specific needs.
3,A virtualization technology that XDE needs to load the image before it can RDP to it. Windows has a great virtualization technology called Hyper-V and that is what we used.
4,The connection pipeline between VS and XDE and also between the debug engine and the virtual image. Here we reused parts of what existed between XDE and Visual Studio, and also the Android Debug Bridge (ADB) channel.
Now let’s look at some of the limitations we have today, and hopefully you can give us input on which ones we need to address first.
当前的限制
Today we are sharing with you an early preview release, with issues/bugs that we look forward to you reporting to us. We also have known limitations – please tell us which ones are most important to you so we can prioritize these on our backlog:
If your app makes direct or indirect use of OpenGL 2 or higher, that will not render on our emulator yet. This support is coming soon, and looking at an early internal-only build that I have it does make the image feel even snappier!
There are many different versions of Android on the market. The one you have with this release of the Visual Studio Emulator for Android is KitKat API 19 (version android-4.4.4_r1). More versions coming later…
If your app takes advantage of the Google Play Services layer then it will not work out of the box in our emulator. That is because when building our Android images we do not include the GMS packages (which require additional licensing that we do not have yet).
You need to recompile your code for x86. If you have parts of your code that can only be compiled for ARM, or you depend on 3rd-party libraries for which you do not have an x86 version, your code will not run on our emulator at this point.
You can only install the Visual Studio Emulator for Android on an operating system where Hyper-V is supported. Examples of where Hyper-V is not supported include Windows 7, non-Windows machines, and inside another VM.
If any of these limitations are an issue for an app you are developing, then the workaround is to use a device (or find another emulator that may not have the limitation). We will be making this current list of limitations shorter with every release that we put out, so please take the survey to help us prioritize.
Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】的更多相关文章
- Visual Studio 2012中编写C程序
换了win7系统后,突然发现VC++6.0不兼容了,我听说有的同学的行,反正我是不行. 那就用VS2012呗.... 我们来看看怎么用: 打开文件->新建->项目,新建一个项目 选择win ...
- .NET 开源了,Visual Studio 开始支持 Android 和 iOS 程序编写并自带 Android 模拟器
.NET 开源了,Visual Studio 开始支持 Android 和 iOS 程序编写并自带 Android 模拟器 北京时间今天凌晨的 Connect(); 大会上,多少程序员的假想成为现实. ...
- Visual Studio 2015支持为Linux构建应用
点这里 微软著名的集成开发环境有可能是首次在其产品页提及了竞争对手Linux.Visual Studio 2015的页面声称,“Build for iOS, Android, Windows devi ...
- Visual Studio Code如何编写运行C、C++
Visual Studio Code如何编写运行C.C++ 作者:知乎用户链接:https://www.zhihu.com/question/30315894/answer/154979413来源:知 ...
- 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
[题外话] 上次介绍了VS上开发Node.js的插件Node.js Tools for Visual Studio(NTVS),其提供了非常方便的开发和调试功能,当然很多情况下由于平台限制等原因需要在 ...
- 在Visual Studio上开发Node.js程序
[题外话] 最近准备用Node.js做些东西,于是找找看能否有Visual Studio上的插件以方便开发.结果还真找到了一个,来自微软的Node.js Tools for Visual Studio ...
- 使用 Visual Studio 分析器找出应用程序瓶颈(转)
使用 Visual Studio 分析器找出应用程序瓶颈 Hari Pulapaka and Boris Vidolov 本文讨论: 以性能瓶颈为目标 应用程序代码分析 比较分析数据 性能报告 本文使 ...
- 使用 Visual Studio 分析器找出应用程序瓶颈
VS的性能分析工具 性能分析工具的选择 打开一个“性能分析”的会话:Debug->Start Diagnotic Tools Without Debugging(或按Alt+F2),VS2013 ...
- 简易扩展Visual Studio UnitTesting支持TestMethodCase
NUnit的TestCaseAttribute可以简化大量的测试参数输入用例的编写,如果基于Visual Studio Unit Test Project开发则默认没有类似的功能,看一段对比代码: p ...
随机推荐
- JS本地存储信息的实现方式(localStorage 与 userData)
详细介绍请看: http://www.cnblogs.com/beiyuu/archive/2011/07/20/js-localstorage-userdata.html 里面涉及到的 demo 代 ...
- BTrace housemd TProfiler
http://blog.csdn.net/y461517142/article/details/26269529 http://calvin1978.blogcn.com/articles/btrac ...
- Revit API封装一个通用函数“过名称找元素”
感觉这个函数不错.通过这种方式寻找元素经常需要用到. ) { ]; } // cannot find it. return null; ...
- java之 ------ 文件拷贝
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- Quartz.NET简介
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...
- android Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
http://www.apkbus.com/android-114019-1-1.html 提高 AndroidManifest.xml中的manifest的android:versionCode ...
- 用TexturePacker打图集用于UGUI中
UGUI的原理则是,让开发者彻底模糊图集的概念,让开发者不要去关心自己的图集.做界面的时候只用小图,而在最终打包的时候unity才会把你的小图和并在一张大的图集里面.Editor->Projec ...
- UITextField的简易封装
UITextField的简易封装 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 UITextFieldView // ...
- Java多线程中join方法的理解
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...
- 以绑定的方式来启动service
先说下原理,之前我们的启动service就是用startService来启动的,这是显式启动.启动后我们无法得到service中的数据,也无法知道它执行的状态,如果我们要启动它的activity和它建 ...