gradle

 摘自:https://segmentfault.com/a/1190000004229002

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.1.0"
}

 buildscript { ... } 配置了用于驱动构建的代码。上述代码声明了项目使用 jCenter 仓库,并且声明了一个 jCenter 文件的 classpath。该文件声明了项目的 Android Gradle 插件版本为 1.3.1。

接着,使用了 com.android.application 插件。该插件用于编译 Android 应用

android { ... } 配置了所有 android 构建所需的参数,这也是 Android DSL 的入口点。默认情况下,只有compileSdkVersion 和 buildtoolsVersion 这两个属性是必须的。

android {
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }

        jnidebug {
            initWith(buildTypes.debug)
            packageNameSuffix ".jnidebug"
            jnidebugBuild true
        }
    }
}
  • 配置默认的 debug Build Type:
    • 设置包名为 <app appliationId>.debug,以便能够在同一个设备上安装 debug 和 release 版的 apk
  • 创建了名为 jnidebug 的新 BuildType,并且以 debug 的配置作为默认配置。
  • 继续配置 jnidebug,开启了 JNI 组件的 debug 功能,并添加了包名后缀。
android {
    signingConfigs {
        debug {
            storeFile file("debug.keystore")
        }

        myConfig {
            storeFile file("other.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }

    buildTypes {
        foo {
            signingConfig signingConfigs.myConfig
        }
    }
}

以上代码片段指定了 debug keystore 的路径在项目根目录下。其他使用了相同配置的 Build Types 亦会受影响,在该例子中为 debug Build Type。该代码片段同时还创建了新的 Signing Config 及使用该签名配置的Build Type

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

本地包依赖

  • compile 编译主 moudle
  • androidTestCompile 编译主 moudle 的测试
  • debugCompile debug Build Type 的编译
  • releaseCompile release Build Type 的编译
repositories {
    jcenter()
}


dependencies {
    compile 'com.google.guava:guava:18.0'
}

远程包依赖

Gradle 支持从 Maven 或 Ivy 仓库中拉取依赖文件。首先必须将仓库添加到列表中,然后必须在 dependencies 中添加 Maven 或 Ivy 声明的包。

include ':app', ':libraries:lib1', ':libraries:lib2'

其中 setting.gradle 的内容非常简单。该文件定义了各 Gradle 项目的位置

productFlavors {
        flavor1 {
            ...
        }

        flavor2 {
            ...
        }
    }

每一个 Build Type 都会生成新的 APK。Product Flavors 同样也会做这些事情:项目的输出将会组合所有的 Build Types 和 Product Flavors(如果有定义 Flavor)。每一种组合(包含 Build Type和 Product Flavor)就是一个 Build Variant(构建变种版本)。

android {
    ...

    defaultConfig {
        minSdkVersion 8
        versionCode 10
    }

    productFlavors {
        flavor1 {
            packageName "com.example.flavor1"
            versionCode 20
        }

        flavor2 {
            packageName "com.example.flavor2"
            minSdkVersion 14
        }
    }
}

ProductFlavor 类型的 android.productFlavors.* 对象与 android.defaultConfig 对象的类型是相同的。就是说着它们之间属性共享。

defaultConfig 为所有的 flavor 提供基本的配置,每个 flavor 都可以重写这些配置的值。

BuildConfig 包含的值如下:

  • boolean DEBUG —— 当前构建是否开启了 debuggable
  • int VERSION_CODE
  • String VERSION_NAME
  • String APPLICATION_ID
  • String BUILD_TYPE —— build type 的名字,例:"release"
  • String FLAVOR —— flavor 的名字,例:"paidapp"
原文地址:https://www.cnblogs.com/anni-qianqian/p/7154705.html