{"id":31379,"date":"2016-01-13T18:21:56","date_gmt":"2016-01-13T12:51:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=31379"},"modified":"2016-03-03T17:01:55","modified_gmt":"2016-03-03T11:31:55","slug":"gradle-android-studio","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/gradle-android-studio\/","title":{"rendered":"Gradle Android Studio"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Gradle Android Studio\u00a0<\/strong><\/p>\n<p><strong>Introduction:<\/strong><\/p>\n<ol>\n<li>Gradle is an <strong>automated build toolkit<\/strong> that can integrate into lots of different environments, via plugins.<\/li>\n<li><strong>Minimize Configuration Required for New Projects:<\/strong> Gradle has a set of default configuration settings that are automatically applied to every project you create in Android Studio. If you&#8217;re developing a project that doesn&#8217;t adhere to these default configuration rules, Gradle is easy to customize.<\/li>\n<li><strong>Declare Project Dependencies:<\/strong> Dependencies can be modules, JAR files or libraries, and they can be located either on the local file system or a remote server.<\/li>\n<li>T<strong>est Your Project:<\/strong> Gradle automatically generates a test directory and a test APK from your project&#8217;s test sources and can run your tests during the build process.<\/li>\n<li><strong>Generate Signed APKs:<\/strong> If you add all the necessary information, such as keyPassword and keyAlias, to your Gradle build file, you can use Gradle to generate signed APKs.<\/li>\n<li><strong>Generate Multiple APKs from a Single Module:<\/strong> Gradle can generate multiple APKs with different package and build configurations from a single module. This feature is particularly handy for Android developers, for several reasons:<br \/>\n<strong>Support a Wide Range of Devices<\/strong>,<br \/>\n<strong>Offer Different Versions of an App like Demo or Pro<\/strong><\/li>\n<\/ol>\n<p><strong>Gradle Build Files:<\/strong><\/p>\n<ol>\n<li>Gradle build files use a Domain Specific Language or DSL to define custom build logic and to interact with the Android-specific elements of the Android plugin for Gradle.<\/li>\n<li>Android Studio projects consist of one or more modules, which are components that you can build, test, and debug independently. Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files:<\/li>\n<\/ol>\n<ul>\n<li><strong>Top-Level Build File:<\/strong> This is where you&#8217;ll find the configuration options that are common to all the modules that make up your project.<\/li>\n<li><strong>Module-Level Build File:<\/strong> Each module has its own Gradle build file that contains module-specific build settings. You&#8217;ll spend most of your time editing module-level build file(s) rather than your project&#8217;s top-level build file.<\/li>\n<\/ul>\n<p>To take a look at these build.gradle files, open Android Studio&#8217;s Project panel (by selecting the Project tab) and expand the Gradle Scripts folder. In our sample project, the first two items in the list are your project&#8217;s top-level and module-level build files.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-31454 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/01\/gradle.png\" alt=\"gradle\" width=\"1366\" height=\"768\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The screenshot below gives you an idea of how the Gradle Scripts folder might look for a project with multiple modules.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-31456\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/01\/gradle1.png\" alt=\"gradle1\" width=\"1366\" height=\"768\" \/><\/p>\n<p><strong>Top-Level Gradle Build File<\/strong>:<\/p>\n<ol>\n<li>Every Android Studio project contains a single, top-level Gradle build file. This build.gradle file is the first item that appears in the Gradle Scripts folder and is clearly marked Project.<\/li>\n<li>Most of the time, you won&#8217;t need to make any changes to this file, but it&#8217;s still useful to understand its contents and the role it plays within your project. Below is an annotated version of a typical top-level build file.<\/li>\n<\/ol>\n<p>[code]\/\/Project-level Gradle build files use buildscript to define dependencies.\/\/<br \/>\nbuildscript {<br \/>\nrepositories {<br \/>\n\/\/ jcenter() is a superset of mavenCentral(), it uses HTTPs to serve content, it is faster than mavenCentral() \/\/<br \/>\njcenter()<br \/>\n}<br \/>\n\/\/This file relies on the jcenter repository.\/\/<br \/>\ndependencies {<br \/>\nclasspath &#8216;com.android.tools.build:gradle:1.5.0&#8217;<br \/>\n\/\/Project is dependent on version 1.5.0 of the Android plugin for Gradle.\/\/<\/p>\n<p>\/\/ NOTE: Do not place your application dependencies here; they belong<br \/>\n\/\/ in the individual module build.gradle files<br \/>\n}<br \/>\n}<\/p>\n<p>allprojects {<br \/>\n\/\/Defines the dependencies required by your application.\/\/<br \/>\nrepositories {<br \/>\njcenter()<br \/>\n}<br \/>\n}<br \/>\n\/\/Application depends on the jCenter repository.\/\/<br \/>\ntask clean(type: Delete) {<br \/>\ndelete rootProject.buildDir<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>There are 3 main areas to this Android build file:<br \/>\n<strong>buildscript { &#8230; }<\/strong><br \/>\nYou will notice a new closure buildscript. This is nothing to do with App Engine and is generally used to specify dependencies that the build script will need to it. In our case, this script requires the App Engine Gradle library to be present and hence we are specifying it via the normal dependency and specifying from which repository to take it.<\/p>\n<ul>\n<li>Configures the code driving the build.<\/li>\n<li>In this case, this declares that it uses the jCenter repository,<\/li>\n<li>There is a classpath dependency on a Maven artifact. This artifact is the library that contains the Android plugin for Gradle in version 1.3.1.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> This only affects the code running the build, not the project. The project itself needs to declare its own repositories and dependencies.<\/p>\n<p><strong>allprojects { &#8230; }<\/strong><br \/>\nWe have the dependencies that this project source code needs to compile.<\/p>\n<p><strong>task clean<\/strong><\/p>\n<ul>\n<li>Basically it is used to clean build system, It delete the build directory when run.<\/li>\n<li>Task type is Delete (Can also be copy)<\/li>\n<li>When modify the file settings.gradle click sync, will delete the file rootProject.buildDir<\/li>\n<\/ul>\n<p><strong>Module-Level Gradle Build Files<\/strong><br \/>\nIn addition to the project-level Gradle build file, each module has a Gradle build file of its own. Below is an annotated version of a basic, module-level Gradle build file.<\/p>\n<p>[code]apply plugin: &#8216;com.android.application&#8217;<br \/>\n\/\/Since this project is an Android app, the build.gradle file utilises the Android plugin. if there is any library project then we&#8217;ll use apply plugin: &#8216;com.android.library&#8217; as plugin \/\/<br \/>\nandroid {<br \/>\n\/\/ The following section configures all your project\u2019s Android-specific parameters, and tells Gradle which version of Android it should build your project with. If you\u2019ve developed Android applications before, the following should all be familiar. \/\/<br \/>\n\/\/ Signing configuration to generate signed app if you already have .jks file \/\/<br \/>\nsigningConfigs {<br \/>\nconfig {<br \/>\nkeyAlias &#8216;test&#8217;<br \/>\nkeyPassword &#8216;test123&#8217;<br \/>\nstoreFile file(&#8216;\/home\/shivang\/Desktop\/test.jks&#8217;)<br \/>\nstorePassword &#8216;test123&#8217;<br \/>\n}<br \/>\n}<br \/>\ncompileSdkVersion 21<br \/>\n\/\/The API your project is targeting.\/\/<br \/>\nbuildToolsVersion &quot;21.1.1&quot;<br \/>\n\/\/\/\/The version of the build tools you want to use.\/\/<br \/>\ndefaultConfig {<br \/>\napplicationId &quot;com.example.jessica.myapplication&quot;<br \/>\n\/\/Defines your application\u2019s ID. Note, earlier versions of the Android plugin used \u2018packageName\u2019 instead of \u2018applicationID.\u2019\/\/<br \/>\n\/\/ Package name has deprecated now, application provides the facility to create multiple instance(Varient) of your app \/\/<br \/>\nminSdkVersion 16<br \/>\n\/\/The minimum API required by your project.\/\/<br \/>\ntargetSdkVersion 21<br \/>\n\/\/The version of Android you\u2019re developing your application for.\/\/<br \/>\nversionCode 1<br \/>\nversionName &quot;1.0&quot;<br \/>\n}<\/p>\n<p>buildTypes {<br \/>\nrelease {<br \/>\n\/\/ add signing configuration for build \/\/<br \/>\nsigningConfig signingConfigs.config<br \/>\n\/\/ \u2018BuildTypes\u2019 controls how your app is built and packaged. If you want to create your own build variants, you\u2019ll need to add them to this section. \/\/<br \/>\nminifyEnabled true<br \/>\nshrinkResources true<br \/>\n\/\/ Resource shrinking is the automatic removal of resources that are unused, at build time, in the packaged app, you have to enable minifyEnabled in order to turn on code shrinking.<\/p>\n<p>\/\/ Stops the reverse engineering of your application\/\/<br \/>\nproguardFiles getDefaultProguardFile(&#8216;proguard-android.txt&#8217;), &#8216;proguard-rules.pro&#8217;<br \/>\n\/\/Applies the default ProGuard settings from the Android SDK.\/\/<\/p>\n<p>}<br \/>\n\/\/ Generates Different variants of app \/\/<br \/>\nproductFlavors {<br \/>\ndemo {<br \/>\napplicationId = &quot;com.example.myapplication.demo&quot;<br \/>\nresValue &quot;string&quot;, &quot;app_name&quot;, &quot;Demo App&quot;<br \/>\n\/\/ change the name of application according to variants of app \/\/<br \/>\n}<br \/>\nfull {<br \/>\napplicationId = &quot;com.example.myapplication.full&quot;<br \/>\nresValue &quot;string&quot;, &quot;app_name&quot;, &quot;Full App&quot;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>dependencies {<br \/>\n\/\/ A folder entirely dependent, All the jar file exist in libs directory will automatically included \/\/<br \/>\ncompile fileTree(dir: &#8216;libs&#8217;, include: [&#8216;*.jar&#8217;])<br \/>\n\/\/ single file depends<br \/>\ncompile Files ( &#8216;libs \/ Android-Support-v4.jar&#8217; )<br \/>\n\/\/ Gradle dependency that points to the wearable app module, because users cannot browse and install apps directly on the wearable. If packaged properly, when users download the handheld app, the system automatically pushes the wearable app to the paired wearable.<br \/>\nwearApp project(&#8216;:wear&#8217;)<br \/>\n\/\/ Testing dependencies<br \/>\ntestCompile &#8216;junit:junit:4.12&#8217;<br \/>\n\/\/ Application format: packageName: artifactId: Version<br \/>\ncompile &#8216;com.android.support:appcompat-v7:23.1.1&#8217;<br \/>\ncompile &#8216;com.google.android.gms:play-services:8.3.0&#8217;<br \/>\ncompile &#8216;com.android.support:design:23.1.1&#8217;<br \/>\n\/\/ Use &lt;a href=&quot;http:\/\/www.tothenew.com\/blog\/aar-android-archive-library-format-android\/&quot;&gt;Android Archive&lt;\/a&gt; \/\/<br \/>\ncompile &#8216;com.kevinpelgrims.library:library:1.0.0@aar&#8217;<br \/>\n\/\/ Using the @aar notation if you want to download the dependencies, you should add, otherwise it may skip some dependencies \/\/<br \/>\ntransitive=true<br \/>\ncompile(name:&#8217;yourlibraryname&#8217;, ext:&#8217;aar&#8217;)<br \/>\n}<br \/>\n[\/code]<\/p>\n<p><strong>apply plugin:<\/strong><\/p>\n<ul>\n<li>The com.android.application plugin is applied. This is the plugin used for building Android applications.<\/li>\n<li>if there is any library project then we&#8217;ll use apply plugin: &#8216;com.android.library&#8217; as plugin<\/li>\n<\/ul>\n<p><strong>android { &#8230; }<\/strong><\/p>\n<ul>\n<li>configures all the parameters for the android build.<\/li>\n<li>This is the entry point for the Android DSL.<\/li>\n<li>By default, only the compilation target, and the version of the build-tools are needed. This is done with the compileSdkVersion and buildtoolsVersion properties.<\/li>\n<\/ul>\n<p><strong>Other Gradle Files:<\/strong><br \/>\nIn addition to the build.gradle files, your Gradle Scripts folder contains some other Gradle files. Most of the time you won&#8217;t have to manually edit these files as they&#8217;ll update automatically when you make any relevant changes to your project. However, it&#8217;s a good idea to understand the role these files play within your project.<\/p>\n<p><strong>gradle-wrapper.properties (Gradle Version)<\/strong><br \/>\nThis file allows other people to build your code, even if they don&#8217;t have Gradle installed on their machine. This file checks whether the correct version of Gradle is installed and downloads the necessary version if necessary. In our sample app,gradle-wrapper.properties contains the following:<\/p>\n<p>[code]distributionBase=GRADLE_USER_HOME<br \/>\n\/\/Determines whether the unpacked wrapper distribution should be stored in the project, or in the Gradle user home directory.\/\/<br \/>\ndistributionPath=wrapper\/dists<br \/>\n\/\/The path where the Gradle distributions required by the wrapper are unzipped.\/\/<\/p>\n<p>zipStoreBase=GRADLE_USER_HOME<\/p>\n<p>zipStorePath=wrapper\/dists<\/p>\n<p>distributionUrl=https\\:\/\/services.gradle.org\/distributions\/gradle-2.2.1-all.zip<\/p>\n<p>\/\/The URL where the correct version of Gradle should be downloaded from.\/\/<br \/>\n[\/code]<\/p>\n<p><strong>settings.gradle<\/strong><br \/>\nThis file references all the modules that make up your project. Since our sample project has a single module, this file is very straightforward as you can see below.<\/p>\n<p>[code]<br \/>\ninclude &#8216;:app&#8217;<br \/>\nor<br \/>\ninclude &#8216;:mobile&#8217;, &#8216;:wear&#8217;, &#8216;:tv&#8217;, &#8216;:glass&#8217;, &#8216;:mylibrary&#8217;<br \/>\n[\/code]<\/p>\n<p><strong>gradle.properties (Project Properties)<\/strong><br \/>\nThis file contains configuration information for your entire project. It&#8217;s empty by default, but you can apply a wide range of properties to your project by adding them to this file.<\/p>\n<p>[code]<br \/>\n\/\/ Used to speed up gradle build process \/\/<br \/>\n\/\/ When configured, Gradle will run in incubating parallel mode.<br \/>\norg.gradle.parallel=true<br \/>\n\/\/ Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries<br \/>\nthat require a non-trivial initialization time. As a result, it can sometimes seem a<br \/>\nlittle slow to start. The solution to this problem is the Gradle Daemon: a long-lived<br \/>\nbackground process that executes your builds much more quickly than would otherwise be<br \/>\nthe case. \/\/<br \/>\norg.gradle.daemon=true<br \/>\n\/\/ you can declare various configuration variables, just like Global variables you<br \/>\nalways declares while developing any app. These variables further use by<br \/>\nproject.variable_name, Like project.COMPILE_SDK_VER. Its more useful if your<br \/>\napplication have multiple module \/\/<br \/>\nCOMPILE_SDK_VER=23<br \/>\nBUILD_TOOL_VER=21.1.2<br \/>\n[\/code]<\/p>\n<p><strong>local.properties (SDK Location)<\/strong><br \/>\nThis file tells the Android Gradle plugin where it can find your Android SDK installation. For example:<\/p>\n<p>[code]<br \/>\nndk.dir=\/home\/shivang\/programFiles\/android-sdk-linux\/ndk-bundle<br \/>\nsdk.dir=\/home\/shivang\/programFiles\/android-sdk-linux<br \/>\n[\/code]<\/p>\n<p><strong>Note<\/strong> that local.properties contains information that&#8217;s specific to the local installation of the Android SDK. This means that you shouldn&#8217;t keep this file under source control.<\/p>\n<p><strong>Some Important Points:<\/strong><\/p>\n<ul>\n<li><strong>Use your own repository URL<\/strong><\/li>\n<\/ul>\n<p>[code]<br \/>\nrepositories {<br \/>\nmavenCentral() or Jcenter()<br \/>\n}<br \/>\nOr<br \/>\nrepositories {<br \/>\nmavenCentral name: &#8216;single_jar_repo&#8217;,<br \/>\nurls: &quot;http:\/\/repo.mycompany.com\/jars&quot;<br \/>\nmavenCentral name: &#8216;multi_jar_repos&#8217;,<br \/>\nurls: [&quot;http:\/\/repo.mycompany.com\/jars1&quot;, &quot;http:\/\/repo.mycompany.com\/jars1&quot;]<br \/>\n}<br \/>\nOr<br \/>\nrepositories {<br \/>\nmavenRepo urls: &quot;http:\/\/repo.mycompany.com\/maven2&quot;<br \/>\n}<\/p>\n<p>&lt;strong&gt;Flat File Repo:&lt;\/strong&gt;<br \/>\nrepositories {<br \/>\nflatDir name: &#8216;localRepository&#8217;,<br \/>\ndirs: &#8216;lib&#8217; flatDir dirs: [&#8216;lib1&#8217;, &#8216;lib2&#8217;]<br \/>\n}[\/code]<\/p>\n<ul>\n<li><strong>There are some dependencies those are used only in compilation, No need to include these dependencies in apk, like lombok. Gradle code below is working fine for me but I don&#8217;t want to include lombok in apk.<\/strong><\/li>\n<\/ul>\n<p>[code]apply plugin: &#8216;android&#8217;<br \/>\nandroid {<br \/>\ncompileSdkVersion 22<br \/>\nbuildToolsVersion &quot;22.0.1&quot;<\/p>\n<p>defaultConfig {<br \/>\nminSdkVersion 19<br \/>\ntargetSdkVersion 19<br \/>\nversionCode 1<br \/>\nversionName &quot;1.0&quot;<br \/>\n}<br \/>\nbuildTypes {<br \/>\nrelease {<br \/>\nminifyEnabled true<br \/>\nproguardFiles getDefaultProguardFile(&#8216;proguard-android.txt&#8217;), &#8216;proguard-rules.txt&#8217;<br \/>\n}<br \/>\n}<br \/>\n }<br \/>\ndependencies {<br \/>\ncompile &#8216;com.android.support:appcompat-v7:+&#8217;<br \/>\ncompile &#8216;org.projectlombok:lombok:1.12.2&#8217; }[\/code]<\/p>\n<p>To solve this issue you can use &#8216;provided&#8217; instead of &#8216;compile&#8217;. That tells the scope of dependency. It will work in compile time but will not include in apk. You can use like below code:<\/p>\n<p>[code]dependencies {<br \/>\nprovided &#8216;org.projectlombok:lombok:1.12.2&#8217;<br \/>\n}[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Gradle Android Studio\u00a0 Introduction: Gradle is an automated build toolkit that can integrate into lots of different environments, via plugins. Minimize Configuration Required for New Projects: Gradle has a set of default configuration settings that are automatically applied to every project you create in Android Studio. If you&#8217;re developing a project that doesn&#8217;t adhere [&hellip;]<\/p>\n","protected":false},"author":892,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[518,1],"tags":[4845,1511],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31379"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/892"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=31379"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=31379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=31379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=31379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}