{"id":9365,"date":"2016-06-12T23:13:52","date_gmt":"2016-06-13T06:13:52","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=9365"},"modified":"2019-02-18T18:04:46","modified_gmt":"2019-02-18T18:04:46","slug":"build-your-android-applications-in-visual-studio-using-gradle","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/build-your-android-applications-in-visual-studio-using-gradle\/","title":{"rendered":"Build your Android applications in Visual Studio using Gradle"},"content":{"rendered":"<p>This blog post is going to talk about how you can use Gradle to build Android libraries (.AAR) and APK\u2019s and leverage Maven dependencies all in Visual Studio so let\u2019s get started! If you are new to Gradle, then take a look at the <a href=\"http:\/\/gradle.org\/getting-started-gradle\">gradle overview<\/a> page and the <a href=\"https:\/\/docs.gradle.org\/current\/userguide\/userguide.html\">User Guide<\/a>.<\/p>\n<p>Gradle has really been the default build system for Android for quite some time and Android developers have slowly but surely been moving to Gradle. Gradle provides flexibility as well as the ability to define common standards for Android builds. Gradle allows Android developers to use a simple, declarative DSL to configure Gradle builds supporting a wide variety of Android devices and App stores.<\/p>\n<p>With a simple, declarative Domain specific language (DSL), Gradle developers can easily describe and manipulate the build logic (e.g. manifest entries \u2013 minSDKVersion, targetSDKVersion etc.), use the built-in dependency management via Maven or Ivy and essentially have access to a single, authoritative build that powers both the Android Studio IDE and builds from the command-line but this ability to use Gradle in Visual Studio has been missing until recently.<\/p>\n<p><span class=\"TextRun SCX145043190\" lang=\"EN-US\" style=\"color: #2e74b5;line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">\nGetting Started<\/span><\/span><span class=\"LineBreakBlob BlobObject SCX145043190\" style=\"line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\"><span class=\"SCX145043190\">\u00a0<\/span><br class=\"SCX145043190\" \/><\/span><span class=\"TextRun SCX145043190\" lang=\"EN-US\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">With the latest Visual Studio release, if you take a look at the cross-platform C++ section in the File-&gt;New project template section, you will see a variety of templates as shown in the figure below. Notice two new <\/span><\/span><span class=\"TextRun SCX145043190\" lang=\"EN-US\" xml:lang=\"EN-US\"><span class=\"SpellingError SCX145043190\">Gradle<\/span><\/span><span class=\"TextRun SCX145043190\" lang=\"EN-US\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\"> templates that have been introduced.\u00a0<\/span><\/span><span class=\"LineBreakBlob BlobObject SCX145043190\"><span class=\"SCX145043190\">\u00a0<\/span><br class=\"SCX145043190\" \/><\/span>\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9375\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template.png\" alt=\"template\" width=\"624\" height=\"372\" \/><\/a><\/p>\n<p>\u2018Basic Android Application (Android, Gradle)\u2019, template as the name suggests will build an E2E android application (.APK) and the \u2018Basic Library (Android, Gradle)\u2019 template will build an AAR file also known as an Android artifact. For traditional Java users, AAR\u2019s are similar to Jar files with the main difference being AAR files include resources such as layouts, drawables etc. This makes it a lot easier to create self-contained visual components.\u00a0 For e.g. if you have multiple apps that use the same login screen, with jar\u2019s you can share classes but not the layout itself. With AAR\u2019s everything is bundled into one package. With that said, let us now take a look at structure of a basic Android application.<\/p>\n<p><span class=\"TextRun SCX145043190\" lang=\"EN-US\" style=\"color: #2e74b5;line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">\nUnderstanding the Gradle artifacts\u00a0<\/span><\/span><span class=\"LineBreakBlob BlobObject SCX145043190\" style=\"line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\"><span class=\"SCX145043190\">\u00a0<\/span><\/span>\nAs you create a new project from template, you will notice the top-level build.gradle file, located in the root project directory, defines build configurations that apply to all modules in your project. By default, the top-level build file uses the buildscript {} block to define the Gradle repositories and dependencies that are common to all modules in the project. The following code sample describes the default settings and DSL elements you can find in the top-level build.gradle.template after creating a new project.\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template2.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9385\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template2.png\" alt=\"template2\" width=\"750\" height=\"296\" \/><\/a><span style=\"color: #000000;font-family: Calibri\">\nFor this project, the repositories section declares the jCenter and mavenCentral repositories, and we have also introduced a classpath dependency on a Maven artifact. This artifact is the library that contains the Android plugin for Gradle version specified in the property pages (figure below).<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/gradle2.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9395\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/gradle2.png\" alt=\"gradle2\" width=\"661\" height=\"170\" \/><\/a><\/p>\n<p>The module-level build.gradle.template file, located in the \u2018app\u2019 directory will allow you to configure build settings for the specific module it is located in. One thing special about this particular specific build.gradle.template file is the fact that it is based upon the experimental plugin (0.4.0) which allows for significant time reduction in configuration time. In addition to this, this experimental plugin also allows for integration with NDK and CMake build system which is important if your application contains C++ code.<\/p>\n<p>A sample Android app module build.gradle.template file when using the experimental plugin outlines some of the basic DSL elements.<\/p>\n<p>The apply plugin: &#8216;com.android.model.$(ConfigurationType)&#8217; command specifies the kind of artifact being built. You can either set this to an application for building an .APK or a library for building an .AAR file. This configuration type can be controlled via the property pages \u2018Configuration Type\u2019 property\u00a0.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template3.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9405\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template3.png\" alt=\"template3\" width=\"923\" height=\"137\" \/><\/a><\/p>\n<p>The plugin name used by the experimental plugin is different than the standard plugin name \u2018com.android.application\u2019 for building an Android APK or \u2018com.android.library\u2019 in case you are building an Android aar library.<\/p>\n<p>The next section in the module-specific \u2018build.gradle.template\u2019 file is the model section. The model section wraps the configuration. The Android closure is the standard way used for wrapping the SDK version used for compiling, minimum SDK version supported and more.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template6.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9425\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template6.png\" alt=\"template6\" width=\"603\" height=\"254\" \/><\/a><\/p>\n<p><span style=\"color: #000000;font-family: Calibri\">The next section \u2018compileOptions\u2019 allows for choosing the language level used by the compiler.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template7.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9435\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template7.png\" alt=\"template7\" width=\"545\" height=\"102\" \/><\/a><\/p>\n<p>The <a href=\"https:\/\/developer.android.com\/studio\/build\/build-variants.html\">android.buildTypes<\/a> section creates a debug and release build type\u00a0 for this application. Although the debug build type doesn\u2019t appear in the build configuration file; it gets configured when the debuggable property is set to true. This allows for debugging the application on secure Android devices and configures APK signing with a default generic keystore.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template8.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9445\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template8.png\" alt=\"template8\" width=\"583\" height=\"188\" \/><\/a><\/p>\n<p>The last section in the model enclosure is <a href=\"https:\/\/developer.android.com\/studio\/build\/build-variants.html\">productFlavors<\/a>. A product flavor defines a customized version of the application build by the project. A single project can have different flavors which change the generated application. Since this particular build.gradle is tailored for an NDK android application the abiFilters.add allows for building one APK per architecture easily.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template9.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9455\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template9.png\" alt=\"template9\" width=\"470\" height=\"344\" \/><\/a><\/p>\n<p>The last section in this build.gradle.template file is the dependencies section. There are three kinds of dependencies, and you can find what each kind does <a href=\"https:\/\/developer.android.com\/studio\/build\/build-variants.html\">here on the Android documentation site<\/a>. This particular build.gradle.template only defines the compile.filetree dependency as shown below. The ${AarDependencies) is an addition by us which allows easy Visual Studio project referencing.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template10.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9465\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template10.png\" alt=\"template10\" width=\"558\" height=\"99\" \/><\/a><\/p>\n<p>The compile fileTree(dir: &#8216;libs&#8217;, include: [&#8216;*.jar&#8217;]) line tells the build system to include any JAR files inside the app\/libs\/ directory in the compilation classpath and in the final package of your application.<\/p>\n<p>The other Gradle files that are included as the part of your project is the settings.gradle.template file. This file references all the modules that make up your project.<\/p>\n<p>include &#8216;:app&#8217; $(AarDependenciesSettings)<\/p>\n<p><span class=\"TextRun SCX145043190\" lang=\"EN-US\" style=\"color: #2e74b5;line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">\nUsing the NDK integration\u00a0\u00a0<\/span><\/span><span class=\"LineBreakBlob BlobObject SCX145043190\" style=\"line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\"><span class=\"SCX145043190\">\u00a0<\/span><\/span>\nUsing the NDK integration with Gradle in Visual Studio is easy. Typically you would need to add the ndk {} section in your model section for your module specific build.gradle.template file and potentially also use the android.sources section to provide JNI source set as specified in the <a href=\"http:\/\/tools.android.com\/tech-docs\/new-build-system\/gradle-experimental\">android <\/a><u>documentation<\/u>\u00a0 but\u00a0 in Visual Studio in order to incorporate a native C\/C++ component all you need to do is create a new native component through the Cross-Platform \u2013 Android section \u00a0and then add it via the typical project reference from the Gradle project as shown below. Visual Studio will automatically take care of packaging your native library in the Android application you are building.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/gifref.gif\"><img decoding=\"async\" class=\"alignnone  wp-image-9475\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/gifref.gif\" alt=\"gifref\" width=\"557\" height=\"500\" \/><\/a><\/p>\n<p>Alternatively you can also use the standard approach for including JNI sources using the experimental Gradle plugin as well if that works out better for you. Google provides a good list of samples for Android NDK and Gradle in <a href=\"https:\/\/github.com\/googlesamples\/android-ndk\">this repository<\/a>. With minor changes, mainly involving copying over contents from the \u2018build.gradle\u2019 -&gt; \u2018build.gradle.template\u2019 files\u00a0, those samples would work for you in Visual Studio. We are currently in the process of making this process happen automatically by providing you folks with an Android Studio -&gt; Visual Studio Android project converter. If this converter really interests you please leave your contact information here or reach out to me.<\/p>\n<p><span class=\"TextRun SCX145043190\" lang=\"EN-US\" style=\"color: #2e74b5;line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">\nReferencing Android libraries in your Android project\u00a0\u00a0\u00a0<\/span><\/span><span class=\"LineBreakBlob BlobObject SCX145043190\" style=\"line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\"><span class=\"SCX145043190\">\u00a0<\/span><\/span>\nGenerally, Android applications built in Visual Studio mainly use C++. Every now and then even for these C++ heavy applications there can be a need to include Android libraries such as AARs and JARs into your Android application. These could be homegrown Android artifacts built for the purposes of sharing common code, assets and utilities.<\/p>\n<p>With this release of Visual Studio, we have made the process really easy for you to do that as well. In order to create and reference an AAR file you can simply do so by creating a new AAR from the File-&gt;New project menu (Basic Library (Android, Gradle)) and then include it in your Android application by adding it through a project reference as shown in the figure below.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/androidlibref.gif\"><img decoding=\"async\" class=\"alignnone  wp-image-9485\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/androidlibref.gif\" alt=\"androidlibref\" width=\"1272\" height=\"676\" \/><\/a><\/p>\n<p><span class=\"TextRun SCX145043190\" lang=\"EN-US\" style=\"color: #2e74b5;line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">\nAn example: Incorporating the Facebook SDK in your Android app\n<\/span><\/span>Now that we have a basic understanding of how the Gradle build system works, lets now go ahead and show off the real power of the build system. As mentioned earlier, Gradle makes it simple to integrate with external repositories. In order to leverage the Facebook SDK (instructions <a href=\"https:\/\/developers.facebook.com\/docs\/android\/getting-started\">here<\/a>) all one has to do is use the compile dependency command in the module specific build.gradle.template file and add the following line (given that we have already specified mavenCentral() and JCenter() repositories in the root build.gradle file).<\/p>\n<p>compile &#8216;com.facebook.android:facebook-android-sdk:4.7.+&#8217;<\/p>\n<p>This command declares a remote binary dependency on the version 4.7 and \u00a0above for the Facebook SDK. At build time Gradle will now automatically pull the dependency from the jCenter repository specified in the top-level build.gradle.template file.<\/p>\n<p>This mechanism not only allows you to specify and incorporate the Facebook SDK as a build time artifact but also allows Visual Studio to power the <a href=\"https:\/\/visualstudiogallery.msdn.microsoft.com\/53e6e65b-fa0f-411a-a98d-7911009537ac\">Java language service experience<\/a> with features like Intellisense extending their functionality for the Facebook SDK set of API(s) as well as shown \u00a0in the figure below!<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template11.png\"><img decoding=\"async\" class=\"alignnone  wp-image-9495\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/template11.png\" alt=\"template11\" width=\"908\" height=\"333\" \/><\/a><\/p>\n<p><span class=\"TextRun SCX145043190\" lang=\"EN-US\" style=\"color: #2e74b5;line-height: 28px;font-family: 'Calibri Light',Sans-Serif;font-size: 20pt\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCX145043190\">\nWrap Up\u00a0\u00a0\u00a0<\/span><\/span>\nThis summarizes the current support for building Android applications with Gradle for <a href=\"https:\/\/www.visualstudio.com\/post-download-vs?sku=community&amp;clcid=0x409\">Visual Studio 2015 Update 3<\/a>. We look forward to you trying out these new features and sharing your feedback either through <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt280277.aspx\">Send-A-Smile\/Frown<\/a> (use #cpp2015), <a href=\"http:\/\/connect.microsoft.com\/VisualStudio\">Connect<\/a>, <a href=\"http:\/\/visualstudio.uservoice.com\/\">User Voice<\/a> or down below in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is going to talk about how you can use Gradle to build Android libraries (.AAR) and APK\u2019s and leverage Maven dependencies all in Visual Studio so let\u2019s get started! If you are new to Gradle, then take a look at the gradle overview page and the User Guide. Gradle has really been [&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":[],"class_list":["post-9365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>This blog post is going to talk about how you can use Gradle to build Android libraries (.AAR) and APK\u2019s and leverage Maven dependencies all in Visual Studio so let\u2019s get started! If you are new to Gradle, then take a look at the gradle overview page and the User Guide. Gradle has really been [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/9365","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=9365"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/9365\/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=9365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=9365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=9365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}