{"id":6781,"date":"2015-02-23T12:40:00","date_gmt":"2015-02-23T12:40:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/02\/23\/developing-xamarin-android-native-applications\/"},"modified":"2021-10-07T13:42:20","modified_gmt":"2021-10-07T13:42:20","slug":"developing-xamarin-android-native-applications","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/developing-xamarin-android-native-applications\/","title":{"rendered":"Developing Xamarin Android Native Applications"},"content":{"rendered":"<p style=\"text-align: left\"><a href=\"http:\/\/xamarin.com\/\">Xamarin<\/a> has quickly become a popular way for creating cross-platform mobile applications promoting true code-reuse while allowing developers to create native* (native here means natural to the platform) user interfaces, providing them access to full spectrum of functionality exposed by the underlying platform\/device and guaranteeing native* performance by allowing applications to leverage platform specific hardware acceleration. To add on, Xamarin integrates fully into Visual Studio and allows development of iOS, Android and Windows applications alike.<\/p>\n<p style=\"text-align: left\">So what does this mean for our true native (C\/C++) developers? Well if you take a look at Xamarin&rsquo;s current approach for the android platform, it allows for <a href=\"http:\/\/developer.xamarin.com\/guides\/android\/advanced_topics\/using_native_libraries\/\">leveraging android native (C\/C++) libraries<\/a> when building end-end Xamarin Android applications.<\/p>\n<p style=\"text-align: left\">This approach currently however involves mucking around with the Xamarin Android project files in Visual Studio and adding the Item-Group pointing to the prebuilt dynamic shared library mentioned here in the Xamarin Android guides. Typically this native dynamic shared library is developed in another IDE (commonly, Eclipse), which adds further complexity given developers now have to worry about using two IDE&rsquo;s and absorbing the fun that comes from the added complexity to edit, build and debug their application specially when working in the native (C\/C++) library layer.<\/p>\n<p style=\"text-align: left\">With Visual Studio 2015 CTP 6, this is no longer the case and developers can now create even this native (C\/C++) library in Visual Studio, we (the C++ cross-platform mobile team) have been working with Xamarin in order to provide a smooth experience when developing Xamarin Android Native applications. This blog highlights a few of the enablement made available with Visual Studio 2015 CTP 6 and while all the work has not been finished yet, we are excited for you try it out and provide us feedback.<\/p>\n<h2 style=\"text-align: left\">Getting Started<\/h2>\n<p style=\"text-align: left\">As a pre-requisite for being able to follow this blog, developers need to install<a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=521794\"> Visual Studio 2015 CTP6<\/a> with Visual C++ cross-platform mobile bits and Xamarin optional packages. Once these bits have been installed you have everything you need to start developing Xamarin Android Native applications entirely in Visual Studio. Let us start with creating a Xamarin Android Project. For purposes of this blog let&rsquo;s choose the simplest of Xamarin Android templates &lsquo;Blank App (Android)&rsquo; from the File-&gt;New project menu and create a new project called <span style=\"color: #800000\">&lsquo;XamarinNativeExample&rsquo;<\/span>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew.png\" alt=\"Image 5164 FileNew\" width=\"1910\" height=\"1120\" class=\"aligncenter size-full wp-image-29413\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew.png 1910w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew-300x176.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew-1024x600.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew-768x450.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/5164.FileNew-1536x901.png 1536w\" sizes=\"(max-width: 1910px) 100vw, 1910px\" \/><\/a><\/p>\n<p style=\"text-align: left\">Next to the existing solution let&rsquo;s add a dynamic shared library project from the &lsquo;<span style=\"color: #800000\">Visual C++ &#8211; Cross-Platform node<\/span>&rsquo; and call it &lsquo;<span style=\"color: #800000\">NativeLibrary<\/span>&rsquo;.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture.png\" alt=\"Image 8105 New Picture\" width=\"1910\" height=\"1094\" class=\"aligncenter size-full wp-image-29418\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture.png 1910w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture-300x172.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture-1024x587.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture-768x440.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/8105.New-Picture-1536x880.png 1536w\" sizes=\"(max-width: 1910px) 100vw, 1910px\" \/><\/a><\/p>\n<p style=\"text-align: left\">Once we are done, our solution should have two projects (a Xamarin Android project and a C++ Dynamic Shared Library project) in solution explorer.<\/p>\n<h2 style=\"text-align: left\">\n<a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6864.XamPropertyPages.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6864.XamPropertyPages.png\" alt=\"Image 6864 XamPropertyPages\" width=\"615\" height=\"518\" class=\"aligncenter size-full wp-image-29416\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6864.XamPropertyPages.png 615w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6864.XamPropertyPages-300x253.png 300w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><\/a><\/p>\n<p>Referencing Android Native libraries from a Xamarin Android project<\/h2>\n<p style=\"text-align: left\">As mentioned in the abstract of this blog traditionally developers leveraging native android binaries would have to go build them in other IDE&rsquo;s and then to leverage these pre-built android native binaries within a Xamarin Android project they would have to muck around with the Xamarin Android project. With the work we have done referencing the native binary is as simple as adding a project reference between the &lsquo;<span style=\"color: #993300\">XamarinNativeExample<\/span>&rsquo; and the &lsquo;<span style=\"color: #800000\">NativeLibrary<\/span>&rsquo; projects. The process is illustrated in the images below:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/3632.references.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/3632.references.png\" alt=\"Image 3632 references\" width=\"1185\" height=\"807\" class=\"aligncenter size-full wp-image-29412\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/3632.references.png 1185w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/3632.references-300x204.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/3632.references-1024x697.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/3632.references-768x523.png 768w\" sizes=\"(max-width: 1185px) 100vw, 1185px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6204.reference.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6204.reference.png\" alt=\"Image 6204 reference\" width=\"1035\" height=\"262\" class=\"aligncenter size-full wp-image-29414\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6204.reference.png 1035w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6204.reference-300x76.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6204.reference-1024x259.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/6204.reference-768x194.png 768w\" sizes=\"(max-width: 1035px) 100vw, 1035px\" \/><\/a>\n<\/p>\n<p style=\"text-align: left\">Once the reference has been added, under the references section for the &lsquo;<span style=\"color: #800000\">XamarinNativeExample<\/span>&rsquo; project the &lsquo;<span style=\"color: #800000\">NativeLibrary<\/span>&rsquo; entry should be found. One would notice that this reference is currently not resolved, this is a known issue and even though the reference doesn&rsquo;t resolve at design time the application package file (.apk) that is built would contain the native library.<\/p>\n<p style=\"text-align: left\">Next let us go ahead and add some code in our native library and invoke it from our Xamarin Android project. In the &lsquo;<span style=\"color: #993300\">NativeLibrary.cpp<\/span>&rsquo; source file within the &lsquo;<span style=\"color: #993300\">NativeLibrary<\/span>&rsquo; project, add the following function. This function will be invoked every time the button&nbsp;defined in our Xamarin Android project is clicked.<\/p>\n<p style=\"text-align: left\"><strong><span style=\"color: #3366ff\">int<\/span> clickCount = 1;<\/strong><br \/><strong><span style=\"color: #3366ff\">int<\/span> getClickCount() {<\/strong><\/p>\n<p style=\"text-align: left\"><strong><span style=\"color: #3366ff\">&nbsp;&nbsp; return<\/span> clickCount++;<\/strong><\/p>\n<p style=\"text-align: left\"><strong>}<\/strong><\/p>\n<p style=\"text-align: left\">To call this function from the Xamarin Android project &lsquo;<span style=\"color: #800000\">XamarinNativeExample<\/span>&rsquo;, add the following code in &lsquo;<span style=\"color: #800000\">MainActivity.cs<\/span>&rsquo; file.<\/p>\n<p style=\"text-align: left\"><strong>[<span style=\"color: #33cccc\">DllImport<\/span>(&#8220;<span style=\"color: #800000\">libNativeLibrary.so<\/span>&#8220;)]<\/strong><br \/><strong><span style=\"color: #0000ff\">public extern static int<\/span> getClickCount();&nbsp;<\/strong><\/p>\n<p style=\"text-align: left\">This Dllimport attribute will load the android native library at application runtime. For using this attribute the &lsquo;using System.Runtime.InteropServices;&rsquo; namespace will also be required in your source. Next let us modify the existing source line in the &lsquo;OnCreate&rsquo; function. OnCreate method is the first method to be called when the activity is created. This modification in the OnCreate function will allow us to call the &lsquo;getClickCount&rsquo; function from the native library whenever the user clicks the button.&nbsp;<\/p>\n<p style=\"text-align: left\"><strong>button.Click +=<span style=\"color: #3366ff\"> delegate<\/span> { button.Text = <span style=\"color: #0000ff\">string<\/span>.Format(&#8220;<span style=\"color: #993300\">{0} clicks!<\/span>&#8220;,&nbsp;getClickCount()); };<\/strong><\/p>\n<p style=\"text-align: left\">At this point we are almost done but before we go ahead and build our solution and test out our new Xamarin Android Native Application, the active solution platform needs to be setup appropriately (ARM or x86 depending upon the choice of your debug target) as shown in the figure below.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/0743.debugtarget.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/0743.debugtarget.png\" alt=\"Image 0743 debugtarget\" width=\"361\" height=\"208\" class=\"aligncenter size-full wp-image-29409\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/0743.debugtarget.png 361w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/0743.debugtarget-300x173.png 300w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/><\/a><\/p>\n<p style=\"text-align: left\">Lastly let us make sure that we have named our &lsquo;NativeLibrary&rsquo; correctly by viewing the &lsquo;Target Name&rsquo; property in the NativeLibrary project property pages. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages.png\" alt=\"Image 2671 proppages\" width=\"1663\" height=\"1132\" class=\"aligncenter size-full wp-image-29411\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages.png 1663w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages-300x204.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages-1024x697.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages-768x523.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2671.proppages-1536x1046.png 1536w\" sizes=\"(max-width: 1663px) 100vw, 1663px\" \/><\/a><\/p>\n<p>At this point we are all set to go ahead and build\/deploy our application to an Android device or emulator.&nbsp;In case you had any issues following this blog, the solution being presented in this blog can be found <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/XamarinNativeExample.zip\">here<\/a>.&nbsp;<\/p>\n<h1 style=\"text-align: left\"><span>Debugging an Android Native Library in a Xamarin Android Application<\/span><\/h2>\n<p style=\"text-align: left\">In addition to being able to consume and build a native android library easily in a <span style=\"color: #993300\">Xamarin<\/span> Android application. The Visual studio experience will also allow developers to easily debug their native android library code being consumed within the Xamarin&nbsp;Android application. To demonstrate this, continuing with our example bring up the property pages for the &lsquo;<span style=\"color: #993300\">XamarinNativeExample&rsquo;<\/span> project and choose the &lsquo;Native&rsquo; debugger option in the &lsquo;Android Options&rsquo; menu as shown in the figure below.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/7651.debugoption.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/7651.debugoption.png\" alt=\"Image 7651 debugoption\" width=\"1141\" height=\"959\" class=\"aligncenter size-full wp-image-29417\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/7651.debugoption.png 1141w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/7651.debugoption-300x252.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/7651.debugoption-1024x861.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/7651.debugoption-768x645.png 768w\" sizes=\"(max-width: 1141px) 100vw, 1141px\" \/><\/a><\/p>\n<p style=\"text-align: left\">&nbsp;Next let us bring up the &lsquo;<span style=\"color: #800000\">NativeLibrary.cpp<\/span>&rsquo; source and set a breakpoint in our &lsquo;<span style=\"color: #993300\">getClickCount()<\/span>&rsquo; function and start debugging (F5). Once the application starts up, on every button click the break-point we set should be hit.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2335.breakpointresolution.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2335.breakpointresolution.png\" alt=\"Image 2335 breakpointresolution\" width=\"553\" height=\"246\" class=\"aligncenter size-full wp-image-29410\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2335.breakpointresolution.png 553w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/02\/2335.breakpointresolution-300x133.png 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/a><\/p>\n<p style=\"text-align: left\">While this is a trivial example, all other debug features such as expression evaluation, watch window, auto window work with this experience as well.<\/p>\n<h1 style=\"text-align: left\"><span><br \/>Wrap Up<\/span>&nbsp;<\/h2>\n<p style=\"text-align: left\">This blog should provide you insight into the work that the Visual Studio team has done in partnership with Xamarin to enable a smooth experience when building Xamarin Android Native applications. We would love you folks to try this experience out and provide your feedback. Additionally If you would like us to talk about a specific topic on our blogs please let us know.&nbsp; As a reminder the goal with this CTP release is for us to collect feedback and learn from you. We are considering future support for other platforms too and your feedback is critical in shaping the direction of our product.&nbsp;<\/p>\n<p style=\"text-align: left\">Sharing feedback is easy! Make feature suggestions on&nbsp;<a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=517102\">UserVoice<\/a>, log bugs you find on our&nbsp;<a href=\"http:\/\/connect.microsoft.com\/visualstudio\">Connect<\/a>&nbsp;site and&nbsp;<a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=517102\">send us a&nbsp;smile or frown<\/a>&nbsp;from inside the IDE. You can also leave comments below. In addition to this if you are \u202flooking&nbsp;to&nbsp;have&nbsp;a&nbsp;directed conversation with our product team&nbsp;on this topic, please reach out to us through email (<a href=\"mailto:aasthan@microsoft.com\">aasthan@microsoft.com<\/a>).&nbsp;We would love to know more and connect with you.\u202f&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/XamarinNativeExample.zip\">XamarinNativeExample.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xamarin has quickly become a popular way for creating cross-platform mobile applications promoting true code-reuse while allowing developers to create native* (native here means natural to the platform) user interfaces, providing them access to full spectrum of functionality exposed by the underlying platform\/device and guaranteeing native* performance by allowing applications to leverage platform specific hardware [&hellip;]<\/p>\n","protected":false},"author":265,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[137,237],"class_list":["post-6781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-announcements","tag-xamarin"],"acf":[],"blog_post_summary":"<p>Xamarin has quickly become a popular way for creating cross-platform mobile applications promoting true code-reuse while allowing developers to create native* (native here means natural to the platform) user interfaces, providing them access to full spectrum of functionality exposed by the underlying platform\/device and guaranteeing native* performance by allowing applications to leverage platform specific hardware [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/6781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/265"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=6781"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/6781\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=6781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=6781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=6781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}