{"id":93,"date":"2014-12-05T08:05:00","date_gmt":"2014-12-05T08:05:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2014\/12\/05\/developing-native-activity-android-applications\/"},"modified":"2021-07-27T13:48:46","modified_gmt":"2021-07-27T13:48:46","slug":"developing-native-activity-android-applications","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/developing-native-activity-android-applications\/","title":{"rendered":"Developing Native-Activity (Android) applications"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>Visual Studio 2015 preview (download <a href=\"http:\/\/www.visualstudio.com\/en-us\/downloads\/visual-studio-2015-downloads-vs.aspx\">here<\/a>) introduces the ability for developers to create Android Native Activity application as a part of the cross-platform mobile development. Android platform with Android Native Activity (API Level 9) introduced the capability to create pure C\/C++ applications. While there is some hidden Java JNI code in the background the idea is to abstract this detail away from the developer. Native Activity applications are popular for gaming (e.g. Unreal 4) and graphic intensive (OpenGL) applications. This post will guide you through the <br \/> experience of creating a Native Activity application and walking through the developer lifecycle of editing, building and debugging Native Activity code. As you walk over this experience in Visual Studio we welcome your thoughts and any feedback that you might have.&nbsp;<\/p>\n<h2>Getting Started<\/h2>\n<p>In order to get started with creating Native Activity Applications please grab the cross-platform mobile tools required which enable this experience. If you haven&rsquo;t done so already please refer to <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2014\/11\/12\/cross-platform-mobile-development-with-visual-c.aspx\">this post <\/a>which walks you over the acquisition experience. Once the tools have been acquired let us start with creating the OOTB Native-Activity Application, for doing so choose the Native Activity Application template available under the &lsquo;Visual C++ &#8211; Cross Platform&rsquo; node.&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1777.newprojectwizard.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1777.newprojectwizard.jpg\" alt=\"Image 1777 newprojectwizard\" width=\"1493\" height=\"837\" class=\"alignnone size-full wp-image-28410\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1777.newprojectwizard.jpg 1493w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1777.newprojectwizard-300x168.jpg 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1777.newprojectwizard-1024x574.jpg 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1777.newprojectwizard-768x431.jpg 768w\" sizes=\"(max-width: 1493px) 100vw, 1493px\" \/><\/a><\/p>\n<p> &nbsp;&nbsp;<\/p>\n<h2>The Native-Activity Solution<\/h2>\n<p>Let&rsquo;s explore the template created starting with the solution architecture. The <strong>&lsquo;HelloNativeActivity&rsquo;<\/strong> solution has two projects the <strong>&lsquo;HelloNativeActivity.NativeActivity&rsquo;<\/strong> and <strong>&lsquo;HelloNativeActivity.Packaging&rsquo;<\/strong> project as shown in the figure below. The NativeActivity project is used to encapsulate all the C\/C++ code specific for this application.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/6431.solutionstructure.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/6431.solutionstructure.png\" alt=\"Image 6431 solutionstructure\" width=\"645\" height=\"524\" class=\"alignnone size-full wp-image-28417\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/6431.solutionstructure.png 645w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/6431.solutionstructure-300x244.png 300w\" sizes=\"(max-width: 645px) 100vw, 645px\" \/><\/a><br \/> Exploring the property pages for the NativeActivity project users can set project properties such as the target Android API Level, Name of the library and the platform toolset. Notice as a part of our cross-platform mobile enablement we have added support for the <strong><em>CLANG toolchain<\/em><\/strong><em> (made available from the Native Development Kit (NDK))<\/em> when targeting the Android platform. This includes adding support in the property pages for easily providing compiler build flags for &lsquo;Code Generation&rsquo;, &lsquo;Optimization&rsquo; and even the use of &lsquo;Precompiled Headers&rsquo;.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages.png\" alt=\"Image 0312 PropertyPages\" width=\"1838\" height=\"920\" class=\"alignnone size-full wp-image-28413\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages.png 1838w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages-300x150.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages-1024x513.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages-768x384.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/0312.PropertyPages-1536x769.png 1536w\" sizes=\"(max-width: 1838px) 100vw, 1838px\" \/><\/a><\/p>\n<p>Moving over the <strong>NativeActivity.Packaging project<\/strong>, the second project in the HelloNativeActivity solution is used to bundle up the Dynamic Shared Library and create a complete Android Application (.apk) by invoking the Ant Build System.&nbsp; Because this is a NativeActivity solution this project doesn&rsquo;t contain any Java code but this project can very well be used to compile Java code as well, as a part of the Ant build invocation. Also notice all other packaging details such as the Android Manifest file (AndroidManifest.xml), the Ant Build (build.xml) are found in this project as well.&nbsp;<\/p>\n<h2>Edit-&gt;Build-&gt;Debug cycle<\/h2>\n<p>The cross-platform mobile experience is designed for a fast edit-build-debug cycle. As a part of this experience<strong> full intellisense support<\/strong> has been introduced for Clang toolchain when t<span style=\"font-size: 1em\">argeting the Android platform. This is in addition to other common code authoring features such as<strong> refactoring, quick navigation and others<\/strong>. The figure below depicts these various features in play.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring.png\" alt=\"Image 3250 codeauthoring\" width=\"1848\" height=\"917\" class=\"alignnone size-full wp-image-28415\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring.png 1848w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring-300x149.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring-1024x508.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring-768x381.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/3250.codeauthoring-1536x762.png 1536w\" sizes=\"(max-width: 1848px) 100vw, 1848px\" \/><\/a><\/p>\n<p>Next let us explore the source for our NativeActivity application. Let&rsquo;s start with main.cpp source file, <strong>&lsquo;android_main&rsquo;<\/strong> function is the entry point function for any native-activity application and is running the event loop for receiving input events and doing other things. Let&rsquo;s edit the source to log the number of sensors available on a particular debug target (emulator\/device) by adding the following code section and build. You can add this code segment before the &lsquo;if (state-&gt;savedState != NULL)&rsquo; conditional block start.&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008000\">ASensorList<\/span> sensorList;<br \/>&nbsp; &nbsp; &nbsp; &nbsp;<br \/><span style=\"color: #00ccff\">&nbsp; &nbsp; &nbsp; &nbsp; int<\/span> counter = 0;<\/p>\n<p><span style=\"color: #00ccff\">&nbsp; &nbsp; &nbsp; &nbsp; int<\/span> numSensorsDiscovered = ASensorManager_getSensorList(engine.sensorManager, &amp;sensorList);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: #0000ff\">&nbsp; <strong>if<\/strong><\/span><strong> ((sensorList != <\/strong><span style=\"color: #800080\"><strong>NULL<\/strong><\/span><strong>) &amp;&amp; (numSensorsDiscovered !=0)))<\/strong> {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style=\"color: #800080\">&nbsp; LOGI<\/span>(&#8220;<span style=\"color: #800000\">%d sensors were discovered on this device<\/span>&#8220;, numSensorsDiscovered);<br \/>&nbsp; &nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n<p>This code section has a bug to demonstrate the work that has been done to provide diagnostic support with Clang&rsquo;s well known features such as Fix-It and others at play (shown below). In this particular case the code section we added had an extra parenthesis on the line highlighted above and once it&rsquo;s edited out the application builds correctly.&nbsp;<\/p>\n<p> <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics.png\" alt=\"Image 4477 clangdiagnostics\" width=\"1780\" height=\"625\" class=\"alignnone size-full wp-image-28416\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics.png 1780w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics-300x105.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics-1024x360.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics-768x270.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/4477.clangdiagnostics-1536x539.png 1536w\" sizes=\"(max-width: 1780px) 100vw, 1780px\" \/><\/a><\/p>\n<p>&nbsp;Lastly, let us now try to debug this application by setting a breakpoint in this newly added code section. Since the <strong>fast x86 based Microsoft emulator<\/strong> is already up and running the debug target is already configured to start debugging. On hitting F5, you can see the debugger break point is hit and debugger features such as the Autos window, Locals window and other features such as the <strong>watch window, expression evaluation, and traversing source under debug mode with step-in, step-out<\/strong> all work correctly as well as illustrated in the figure below.&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging.png\" alt=\"Image 1145 debugging\" width=\"1835\" height=\"776\" class=\"alignnone size-full wp-image-28414\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging.png 1835w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging-300x127.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging-1024x433.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging-768x325.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2014\/12\/1145.debugging-1536x650.png 1536w\" sizes=\"(max-width: 1835px) 100vw, 1835px\" \/><\/a>&nbsp;<\/p>\n<h2><span style=\"background-color: #ffffff\">Wrap Up<\/span><\/h2>\n<p>This blog should give you working knowledge on how to use Visual Studio for creating Android Native-Activity applications. In addition to this the blog should help you understand the various features we have added for a lean-mean edit-build-debug cycle. We&nbsp;will&nbsp;follow up this blog post with&nbsp;additional&nbsp;posts&nbsp;diving deeper into&nbsp;key features.&nbsp;&nbsp;This is a preview release and as with any preview release, the goal for us is 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;&nbsp;<br \/> &nbsp;<br \/> Sharing feedback is easy! Make feature suggestions on&nbsp;<a href=\"http:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio\/category\/30937-languages-c\">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:\/\/msdn.microsoft.com\/en-us\/library\/zzszcehe.aspx\">send us a 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","protected":false},"excerpt":{"rendered":"<p>Overview Visual Studio 2015 preview (download here) introduces the ability for developers to create Android Native Activity application as a part of the cross-platform mobile development. Android platform with Android Native Activity (API Level 9) introduced the capability to create pure C\/C++ applications. While there is some hidden Java JNI code in the background the [&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":[232,233,234,235,185],"class_list":["post-93","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-android","tag-cross-platform","tag-example","tag-native-activity","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>Overview Visual Studio 2015 preview (download here) introduces the ability for developers to create Android Native Activity application as a part of the cross-platform mobile development. Android platform with Android Native Activity (API Level 9) introduced the capability to create pure C\/C++ applications. While there is some hidden Java JNI code in the background the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/93","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=93"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/93\/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=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}