构建基础配置

Android Studio包含一个顶级的构建文件和每个模块的构建文件。构建文件被称为 build.gradle,它是一个纯文本文件,它使用Groovy语法来配置由Android Gradle插件提供的元素。在大多数情况下,你只需要编辑模块级别的构建文件。例如,BuildSystemExample项目的app模块的构建文件是像这样的:

apply plugin:'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }
}

dependencies {
    compile project(":lib")
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile fileTree(dir:'libs', include:['*.jar'])
}

apply plugin:'com.android.application' 是应用Android Gradle插件来构建。这样添加Android特定的构建任务到顶级构建任务中,并且使用 android{…}中的元素来指定Android特定的构建项。

android{….} 配置所有的Android特定构建项:

compileSdkVersion 项指定编译的目标。

buildToolsVersion 项指定使用什么版本的构建工具。使用SDK管理器来安装多个版本的构建工具。

注意:请始终使用其主要版本号高于或等于您的编译目标SDK的版本。

defaultConfig 元素动态的配置在AndroidManifest.xml中的设置。在defaultConfig的值将覆盖manifest文件中的值。配置在defaultConfig的值将应用于所有的构建变种(build variants),除非构建变种的配置覆盖了这些值。

buildType元素控制如何构建和打包你的应用。默认的构建系统定义了两个构建类型:debug和release。debug构建类型包含debugging符号,并且使用了debug key签名。release构建类型默认没有被签名。在这个例子中构建文件配置了release版本,使用了ProGuard。

dependencies元素是在android元素外面的,并且在android元素后面。这个元素声明了这个模块的依赖。在一下的章节中都有依赖的内容。

注:当你在你的项目中改变了构建文件,Android Studio为了导入改变了构建配置而要求项目同步。点击黄色通知栏中的”Sync Now”按钮来同步改变的内容。

<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="图片_x0020_1" o:spid="_x0000_i1027" type="#_x0000_t75" alt="http://developer.android.com/images/tools/as-gradlesync.png" style='width:415.2pt; height:70.2pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\SUNNYA~1\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png" o:title="as-gradlesync"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

图一. 同步 Android Studio中的项目。

声明依赖

这个例子的应用模块声明了3个依赖:

...
dependencies {
    // Module dependency
    compile project(":lib")     // Remote binary dependency
    compile 'com.android.support:appcompat-v7:19.0.1'     // Local binary dependency
    compile fileTree(dir:'libs', include:['*.jar'])
}

每个依赖在下面给出描述。构建系统添加所有类型为”compile”的依赖到编译路径中,并且最终将他们打到最终的包中。

Module dependencies

app模块依赖于lib模块。因为像在”Open an Activity from a Library Module”中描述的,MainActivity登录LibActivity1.

compile project(":lib") 声明了依赖lib模块。当你构建app模块的时候,构建系统将会集合lib模块。

运程二进制依赖

app和lib模块都使用了来自Android支持库的ActionBarActivity类,所有这些模块都依赖它。

compile 'com.android.support:appcompat-v7:19.0.1' 通过指定Maven坐标来声明了对Android支持库19.0.1版本的依赖。在Android SDK的仓库包中Android的支持库是可用的。如果你安装的SDK没有这个包,通过使用SDK管理工具下载安装它。

Android Studio默认使用了Maven的中央仓库来配置项目。(这个配置在项目的顶级构建文件中)。

本地二进制依赖

一些模块不使用任何的本地系统二进制依赖。如果你有依赖本地二进制依赖的模块,拷贝JAR文件到<moduleName>/libs目录下。

compile fileTree(dir: 'libs', include: ['*.jar']) 告诉构建系统将 app/libs目录下面的JAR文件依赖包含到编译路径,并且最终在最终的包中。

有关在Gradle更多的依赖信息,请查看Gradle的用户指南(Dependency Management Basics )。

运行ProGuard

构建系统运行ProGuard,以达到在构建过程中混淆你的代码的目的。在BuildSystemExample中,为了release构建运行ProGuard修改app模块的构建文件:

...
android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }
}
...

getDefaultProguardFile('proguard-android.txt') 从安装的Android SDK中获取默认的ProGuard设置。你可自定义ProGuard规则,Android Studio 将会加模块根部的proguard-rules.pro文件添加到模块特定的规则中。

包的标识:Application ID

在Android构建系统中,applicationId属性是唯一标示发行应用包。Application ID在build.gradle文件的android节点中设置。

apply plugin:'com.android.application'

      android {
        compileSdkVersion 19
        buildToolsVersion "19.1"     defaultConfig {
        applicationId "com.example.my.app"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

注: applicationID只能在build.gradle文件中被指定,不能再AndroidManifest.xml文件中。

当使用build variants,构建系统可以让你为每个product flavors和build types指定包的唯一标示。在build type中的Application ID被添加到product flavors作为后缀。

 productFlavors {
        pro {
            applicationId ="com.example.my.pkg.pro"
        }
        free {
            applicationId ="com.example.my.pkg.free"
        }
    }     buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
    ....

包名依然需要在manifest文件中指定。它在你的源码中用来涉及你的R class和解决相关activity/service注册问题。

package="com.example.app">

注:如果你有多个manifests(比如,一个product flavor指定的manifest和一个build type的manifest),包名在这些manifests中是可选的。如果它再这些manifests中被指定,那报名必须和src/main目录下的manifest的包名一致。

有关更多的关于构建文件和构建过程信息,请看 Build System Overview。

配置签名设置

debug和release版本的app在是否能在安全设备调试和如何进行签名是有区别的。构建系统使用一个默认的key来签名debug版本,并且为了在构建过程中不出现密码提示,使用了已知的证书。构建系统不会签名release版本,除非你明确定义了签名配置。如果你没有一个release的key,你可以安装”Signing your Applications”中描述的进行生成。

使用build variants工作

这个章节描述构建系统如何帮助你在一个项目中的同一个应用创建不同的版本。当时有一个demo和paid版本的时候,这是有用的,或者是你想在Google Play上为不同配置的设备发布多个APK。

构建系统使用 product flavors为你的应用创建不同的版本。每个版本有可能有不同的特性和设备要求。构建系统也应用 build types到不同的构建中,并且打包配置到每个版本中。 每个product flavor和build type的组合形成了一个build variant。构建系统为每个build variant生成了不同的APK。

Build variants

这个项目例子包含了两个默认的build types(debug 和release),还有两个product flavors(demo和full)。更多关于使用build variants的高级信息,查看”Build System Overview”。

Product flavors

为你的应用创建不同的版本:

<!--[if !supportLists]-->1、<!--[endif]-->在构建文件中定义product flavors

<!--[if !supportLists]-->2、<!--[endif]-->为每个flavor创建附加的源码路径

<!--[if !supportLists]-->3、<!--[endif]-->添加flavor特定的源码到你的项目中

接下来的章节带你了解 BuildSystemExample项目中的每个细节。在BuildSystemExample应用中创建两个Flavor。一个demo flavor和一个full flavor。两个flavors共享 MainActivity,MainActivity中有一个按钮跳转到一个新的activity—SecondActivity.对于两个flavor来说SecondActivity是不同的,因此你应该模拟这样的情况:full flavor中的Activity的特性要比demo flavor中的Activity多。在练习的最后,你将得到不同flavor的不同APK。

在构建文件中定义product flavors

为app模块中的构建文件定义两个product flavors:

...
android {
    ...
    defaultConfig {...}
    signingConfigs {...}
    buildTypes {...}
    productFlavors {
        demo {
            applicationId "com.buildsystemexample.app.demo"
            versionName "1.0-demo"
        }
        full {
            applicationId "com.buildsystemexample.app.full"
            versionName "1.0-full"
        }
    }
}
...

这个项目的flavor定义支持使用defualtConfig相同的配置。所有flavors相同都配置都定义在defaultConfig中,每个flavor可以覆盖任何默认的值。上面的构建文件使用了 applicationId属性来分配给每个flavor:自从每个flavor创建了不同的app,他们应该需要不同的包名。

注:在Google Play中,为使你的应用可以拥有多APK支持。给你的所用variants分配相同的包名,并且给每个viant不同的versionCode. 为了再Google Play中区分不同的variants,你应该分配不同的包名给每个variant。

为每个flavor添加额外的源码目录

现在你应该创建源码目录,并且将SecondActivity添加到不同的flavor中。为demo flavor创建源码目录结构:

<!--[if !supportLists]-->1、<!--[endif]-->在Project模板中,展开BuildSystemExample,并且展开app目录

<!--[if !supportLists]-->2、<!--[endif]-->右键src目录,选择New>Directory

<!--[if !supportLists]-->3、<!--[endif]-->使用”demo”作为目录的名字

<!--[if !supportLists]-->4、<!--[endif]-->同样的创建如下目录:

app/src/demo/java

app/src/demo/res

app/src/demo/res/layout

app/src/demo/res/values

目录的结构看起来像图1:

<!--[if gte vml 1]><v:shape id="图片_x0020_2" o:spid="_x0000_i1026" type="#_x0000_t75" alt="http://developer.android.com/images/tools/as-demoflavordirs.png" style='width:150pt;height:162.6pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\SUNNYA~1\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png" o:title="as-demoflavordirs"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

图一:demo flavor的目录

添加不同的activity到不同的flavor中:

添加SecondActivity到demo flavor中:

<!--[if !supportLists]-->1、<!--[endif]-->在Project模板中,右键app模块,选择New>Activity。

<!--[if !supportLists]-->2、<!--[endif]-->选择 Blank Activity,点击Next

<!--[if !supportLists]-->3、<!--[endif]-->输入activity名字: SecondActivity

<!--[if !supportLists]-->4、<!--[endif]-->输入包名”com.buildsystemexample.app”

<!--[if !supportLists]-->5、<!--[endif]-->在app/src/demo目录中右键java目录选择New>Package。

<!--[if !supportLists]-->6、<!--[endif]-->输入com.buildsystemexample.xapp

<!--[if !supportLists]-->7、<!--[endif]-->将SecondActivity拖拽到app/src/demo/java中

<!--[if !supportLists]-->8、<!--[endif]-->接受默认的Refactor

为demo flavor添加SecondActivity的布局文件和资源文件

<!--[if !supportLists]-->1、<!--[endif]-->从ap/src/main/res/layout中将activity_second.xml文件拖拽到app/src/demo/res/layout中

<!--[if !supportLists]-->2、<!--[endif]-->接受默认的提示

<!--[if !supportLists]-->3、<!--[endif]-->将strings.xml从app/src/main/res中拷贝到app/src/demo/res中

<!--[if !supportLists]-->4、<!--[endif]-->替换string.xml文件中的内容,如下:

<!--[if !supportLists]-->5、<!--[endif]--><?xml version="1.0" encoding="utf-8"?>
<resources>
    <stringname="hello_world">This is the full version!</string>
</resources>

注:从现在开始,你可以为每个flavor单独开发SecondActivity. 比如,你可以为full flavor的activity添加更多的属性。

为了让指定的flavor文件工作,点击IDE窗口邮编的Build Variants,并且选择你想使用的flavor,就像图2. Android Studio可能会展示其他flavor的错误,但是这并不影响输出内容的构建。

<!--[if gte vml 1]><v:shape id="图片_x0020_3" o:spid="_x0000_i1025" type="#_x0000_t75" alt="http://developer.android.com/images/tools/as-buildvariants.png" style='width:210pt;height:113.4pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\SUNNYA~1\AppData\Local\Temp\msohtmlclip1\01\clip_image005.png" o:title="as-buildvariants"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

图2

从MainActivity登入到指定flavor的activity

SecondActivity在所有的flavors中都有相同的包名,你可以同main activity中登入。编辑mainActivity:

<!--[if !supportLists]-->1、<!--[endif]-->编辑 activity_main.xml,添加一个按钮:

<LinearLayout ...>
    ...
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:onClick="onButton2Clicked"/>
</LinearLayout>

<!--[if !supportLists]-->2、<!--[endif]-->为按钮添加text标题,和按钮事件onButton2Clicked

<!--[if !supportLists]-->3、<!--[endif]-->在MainActivity中添加如下代码:

publicvoid onButton2Clicked(View view){
    Intent intent =newIntent(this,SecondActivity.class);
    startActivity(intent);
}

<!--[if !supportLists]-->4、<!--[endif]-->编辑manifest文件

<manifest ...>
    <application ...>
        ...
        <activity
            android:name="com.buildsystemexample.app.SecondActivity"
            android:label="@string/title_activity_second">
        </activity>
    </application>
</manifest>

Build types

Build types表现为为每个app包构建包版本。默认的debug和release被提供:

...
android {
    ...
    defaultConfig {...}
    signingConfigs {...}
    buildTypes {...}
    productFlavors {...}
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
         debug {
            debuggable true
        }
    }
}
...

注:尽管在build.gradle文件中默认只有release构建类型,当时release和debug构建类型都被应用的每个构建中。

在这个例子中,product flavors和build types创建了一下的build variants:

demoDebug

demoRelease

fullDebug

fullRelease

为这个例子构建,可以点击Android Studio的Build菜单,或者在命令行中执行 assemble命令。

注:Build>Make Project会编译项目中所有的源码。Build>Rebuild Project选项重新编译所有的源码。

会为不同的build variant创建不同的输出目录。

QQ技术交流群290551701  http://cxy.liuzhihengseo.com/558.html

配置Gradle构建的更多相关文章

  1. Gradle构建多模块项目

    通常我在使用Maven构建项目的时候是将应用项目划分为多个更小的模块. Gradle 项目也拥有多于一个组件,我们也将其称之为多项目构建(multi-project build). 我们首先创建一个多 ...

  2. gradle的安装,配置,构建,研究,初体验......(入职一周研究的第一个大知识点)

    (1)Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置.更 ...

  3. Android NDK开发 Android Studio使用新的Gradle构建工具配置NDK环境(一)

    本文主要讲述了如何如何在Android Studio使用新的Gradle构建工具配置NDK环境,现在把相关的步骤整理出来分享给Android程序员兄弟们,希望给他们在配置NDK环境时带来帮助. 从An ...

  4. Gradle构建SpringBoot并打包可运行的jar配置

    使用Gradle构建项目,继承了Ant的灵活和Maven的生命周期管理,不再使用XML作为配置文件格式,采用了DSL格式,使得脚本更加简洁. 构建环境: jdk1.6以上,此处使用1.8 Gradle ...

  5. Eclipse中使用Gradle构建Java Web项目

    Gradle是一种自动化建构工具,使用DSL来声明项目设置.通过Gradle,可以对项目的依赖进行配置,并且自动下载所依赖的文件,使得构建项目的效率大大提高. 1. 安装Gradle 下载Gradle ...

  6. 模块化之Spring3.0 web fragment和gradle构建项目

      1.背景 模块化开发很久以前就开始普及的概念.但是到了企业实际情况中,真正把模块化作为系统架构的核心的不多.或者说对模块化有这个意识,但是具体到底该如何实现,有些模糊,同时也许因为项目紧.任务中. ...

  7. [转]加速Android Studio/Gradle构建

    加速Android Studio/Gradle构建 android android studio gradle   已经使用Android Studio进行开发超过一年,随着项目的增大,依赖库的增多, ...

  8. [转]-用Gradle 构建你的android程序

    出处:http://www.cnblogs.com/youxilua  前言 android gradle 的插件终于把混淆代码的task集成进去了,加上最近,android studio 用的是gr ...

  9. Android Studio Gradle构建脚本

    Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. 构建工具就是对你的项目进行 ...

随机推荐

  1. 动态inventory脚本的python实现

    #!/usr/bin/env python # -*- coding: utf-8 -*- ''' 基于python的动态inventory脚本实例 ''' import os import sys ...

  2. python 解析配置文件

    settings.cfg [english] greeting = Hello [french] greeting = Bonjour [files] home = /usr/local bin = ...

  3. Android Studio 生成APK出现的「前言不允许有内容」错误

    Build-Generate Signed APK的时候发现提示「前言不允许有内容」.发现提示的是Android.mk.xxxjni.c存在问题. 解决方法是,把/main/res中的,包括/jni目 ...

  4. hdu 3507 Print Article —— 斜率优化DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m ...

  5. Gulp-webpack简单应用

    1.配置环境:  在  webstorm  的控制台中  (1) cnpm install --save-dev gulp    (2)  cnpm install --save-dev gulp-w ...

  6. iOS项目上线的流程

    基本知识 首先要了解一下Xcode打包签名机制中 Certificates & Identificates &Provisioning Profiles 三者之间的关系: Certif ...

  7. Flutter实战视频-移动电商-08.Dio基础_伪造请求头获取数据

    08.Dio基础_伪造请求头获取数据 上节课代码清楚 重新编写HomePage这个动态组件 开始写请求的方法 请求数据 .但是由于我们没加请求的头 所以没有返回数据 451就是表示请求错错误 创建请求 ...

  8. 5-1 变量与常量 & 6-1课程总结

    变量与常量 常量就是变量定义的的前面加上final final关键字定义常量 新建类FinalDemo 更新常量n的值会报错.常量不可以被修改 常量有个命名规则 一般以大写字母去表示 final in ...

  9. Several ports (8005, 8080, 8009) required

    Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The ...

  10. 201621123016 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:接口,多态,Comparable,Comparator 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不 ...