markdown [Gradle basics] #android #android_tutorial #tutorial

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown [Gradle basics] #android #android_tutorial #tutorial相关的知识,希望对你有一定的参考价值。

## Importand Gradle commands  
**how to list down all tasks of a project?**  
*`gradle tasks --all`*

**Following command list down all task to be executed for a task but it does not execute the command.**  
`gradle (some task) --dry-run`  
`gradle assembleDebug --dry-run`




## Questionaire

### What is the difference between the two?
```
implementation 'com.android.support:appcompat-v7:25.0.0'
testImplementation 'junit:junit:4.12'
```
instead of
```
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
```
[Stackoverflow answer](https://stackoverflow.com/a/48388463/1749223)


### What is the difference between `implementation`, `testImplementation` and `androidTestImplementation`?
- implementation : adds dependency for main source set
- testImplementation : adds dependency for test source set
- androidTestImplementation : adds dependency for androidTest source set








# Boring version

**What is artifact?**  
Artifacts are files such as an executable or a compressed archive that are produced by a build process. It is roughly a dependency modules/library that you declare for your project. 


Its a build system, used to automate and manage build process, while allowing you to define flexible custom build configurations.

Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little to no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.

Gradle and android plugin help you configure the following:

#### Build Types
The android studio defines 2 build types, debug and release. you can define your own as well. You can configure each of them, For e.g. the debug build type enables debug options and signs the APK with the debug key, while the release build type may shrink, obfuscate, and sign your APK with a release key for distribution.

#### Product Flavors
The product flavours represent different versions of your app that you may release to user such as paid or free. You can customise to use different code or resources for each of your flavors or reuse the code that are common. Its optional and you have to create them manually.

#### Build Variants
A build variant is a cross product of a build type and product flavor, and is the configuration Gradle uses to build your app. Din’t really quite understand it. Look online :)

#### Manifest Entries
You can specify values for some properties of the manifest file in the build variant configuration. These build values override the existing values in the manifest file. This is useful if you want to generate multiple APKs for your modules where each of the apk files has a different application name, minimum SDK version, or target SDK version. When multiple manifests are present, Gradle merges manifest settings.

#### Dependencies
The build system manages project dependencies from your local file system and from remote repositories. You don’t have to worry about manually searching and copying binaries to your project directory.

#### Signing
The build system enables you to specify signing settings in the build configuration. The build system signs the debug version with a default key and certificate using known credentials to avoid a password prompt at build time. The build system does not sign the release version unless you explicitly define a signing configuration for this build.

#### ProGuard
You can specify proGuard rules for each build variant.

You don’t have to know Groovy, to start configuring your build file, because there is already a template created with all default values, you just need to replace some or add within the block.

#### Gradle files
When starting a new project, Android studio automatically creates some of these files for you with pre-filled default values.

#### The Gradle Settings Files
The settings.gradle file, located in the root project directory, specifies which module it should include when building your app.

include ‘:app'

For multi-module projects, you have to specify each module that should go into the final build.

#### The Top-level Build File
The build.gradle file, located in the root project directory, it applies to all modules in your project.

```Groovy
/**
 * The buildscript {} block is where you configure the repositories and
 * dependencies for Gradle itself--meaning, you should not include dependencies
 * for your modules here. This block includes the Android plugin for
 * Gradle as a dependency.
 */
buildscript {
    /**
     * The repositories {} block configures the repositories Gradle uses to
     * search or download the dependencies. Gradle pre-configures support for remote
     * repositories such as JCenter, Maven Central, and Ivy. You can also use local
     * repositories or define your own remote repositories. The code below defines
     * JCenter as the repository Gradle should use to look for its dependencies.
     */
    repositories {
        jcenter()
    }
    /**
     * The dependencies {} block configures the dependencies Gradle needs to use
     * to build your project. The following line adds Android Plugin for Gradle
     * version 2.2.0 as a classpath dependency.
     */
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
    }
}
/**
 * The allprojects {} block is where you configure the repositories and
 * dependencies used by all modules in your project, such as third-party plugins
 * or libraries. Dependencies that are not required by all the modules in the
 * project should be configured in module-level build.gradle files. For new
 * projects, Android Studio configures JCenter as the default repository, but it
 * does not configure any dependencies.
 */
allprojects {
   repositories {
       jcenter()
   }
}

/* 
 * Module Level Build File
 * The build.gradle file, located in each <project>/<module> directory, overrides settings in the main/ app manifest or top-level build.gradle file.
 */

/**
 * The first line in the build configuration applies the Android plugin for
 * Gradle to this build and makes the android {} block available to specify
 * Android-specific build options.
 */
apply plugin: 'com.android.application'
/**
 * The android {} block is where you configure all your Android-specific
 * build options.
 */
android {
  /**
   * compileSdkVersion specifies the Android API level Gradle should use to
   * compile your app. This means your app can use the API features included in
   * this API level and lower.
   *
   * buildToolsVersion specifies the version of the SDK build tools, command-line
   * utilities, and compiler that Gradle should use to build your app. You need to
   * download the build tools using the SDK Manager.
   */
  compileSdkVersion 25
  buildToolsVersion "25.0.0"
  /**
   * The defaultConfig {} block encapsulates default settings and entries for all
   * build variants, and can override some attributes in main/AndroidManifest.xml
   * dynamically from the build system. You can configure product flavors to override
   * these values for different versions of your app.
   */
  defaultConfig {
    /**
     * applicationId uniquely identifies the package for publishing.
     * However, your source code should still reference the package name
     * defined by the package attribute in the main/AndroidManifest.xml file.
     */
    applicationId 'com.example.myapp'
    // Defines the minimum API level required to run the app.
    minSdkVersion 15
    // Specifies the API level used to test the app.
    targetSdkVersion 25
    // Defines the version number of your app.
    versionCode 1
    // Defines a user-friendly version name for your app.
    versionName "1.0"
  }
  /**
   * The buildTypes {} block is where you can configure multiple build types.
   * By default, the build system defines two build types: debug and release. The
   * debug build type is not explicitly shown in the default build configuration,
   * but it includes debugging tools and is signed with the debug key. The release
   * build type applies Proguard settings and is not signed by default.
   */
  buildTypes {
    /**
     * By default, Android Studio configures the release build type to enable code
     * shrinking, using minifyEnabled, and specifies the Proguard settings file.
     */
    release {
        minifyEnabled true // Enables code shrinking for the release build type.
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
  /**
   * The productFlavors {} block is where you can configure multiple product
   * flavors. This allows you to create different versions of your app that can
   * override defaultConfig {} with their own settings. Product flavors are
   * optional, and the build system does not create them by default. This example
   * creates a free and paid product flavor. Each product flavor then specifies
   * its own application ID, so that they can exist on the Google Play Store, or
   * an Android device, simultaneously.
   */
  productFlavors {
    free {
      applicationId 'com.example.myapp.free'
    }
    paid {
      applicationId 'com.example.myapp.paid'
    }
  }
  /**
   * The splits {} block is where you can configure different APK builds that
   * each contain only code and resources for a supported screen density or
   * ABI. You'll also need to configure your build so that each APK has a
   * different versionCode.
   */
  splits {
    // Screen density split settings
    density {
      // Enable or disable the density split mechanism
      enable false
      // Exclude these densities from splits
      exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
    }
  }
}
/**
 * The dependencies {} block in the module-level build configuration file
 * only specifies dependencies required to build the module itself.
 */
dependencies {
    compile project(":lib")
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
```

#### Gradle properties file
Gradle also includes 2 properties file in your root directory, to specify settings for gradle build toolkit.
- gradle.properties: to configure project-wide gradle settings.
- local.properties: configure local environment properties, such as path to SDK installation, its auto-generated by android studio and is specific to the local developer environment.

Ref: 
- https://developer.android.com/studio/build/index.html
- http://stackoverflow.com/questions/16754643/what-is-gradle-in-android-studio
- https://www.sitepoint.com/quick-tip-what-is-gradle-and-how-does-it-work-with-android-studio/

以上是关于markdown [Gradle basics] #android #android_tutorial #tutorial的主要内容,如果未能解决你的问题,请参考以下文章

Markdown: Basics (快速入门)

markdown basic_install.md

markdown basic_install.md

markdown Angular_Firebase_basic

markdown basic_install.md

markdown Git Basics