入门系列6.1打包 - 详解的build.gradle

来源:互联网 发布:东华软件股份公司地址 编辑:程序博客网 时间:2024/04/30 00:45


http://www.jianshu.com/p/7e3a69dbd20e


【Android的工作室】入门系列6.1打包 - 详解的build.gradle

字数1983年 阅读534 评论0 

配置的build.gradle

AS中APP所有的配置尽在一个build.gradle文件中,打包的时候也是解析build.gralde文件来打包的,所以搞懂build.gradle文件是至关重要的,结构如下所示


应用插件用来指定用的是哪个插件,取值有:

  • com.android.application:Android应用插件(打包得到的是.apk文件文件)
  • com.android.library:Android的库插件(打包得到的是.aar文件)

机器人用来指定的Andr​​oid打包插件的相关属性,其包含如下节点

  • compileSdkVersion(apiLevel):设置编译时用的的Andr​​oid版本
  • buildToolsVersion(buildToolsVersionName):设置编译时使用的构建工具的版本
  • defaultConfig:设置一些默认属性,其可用属性是buildTypes和ProductFlavors之和
  • sourceSets:配置相关源文件的位置,当你的项目的目录结构跟默认的有区别但又不想改的时候sourceSets就派上用场了

    • AIDL设置爱迪的目录
    • 资产设置资产资源目录
    • compileConfigurationName为此源集编译配置的名称。
    • Java的Java的源代码目录
    • JNI JNI代码目录
    • jniLibs已编译好的JNI库目录
    • 清单指定清单文件
    • 这个名字源集的名称。
    • packageConfigurationName为此源集运行时配置的名称。
    • providedConfigurationName为此源集编译,只有配置的名称。
    • renderscript Renderscript源代码目录
    • 水库资源目录
    • setRoot(路径)根目录
  • signingConfigs:配置签名信息

    • keyAlias​​签名的别名
    • keyPassword密码
    • storeFile签名文件的路径
    • storePassword签名密码
    • 的storetype类型
  • buildTypes:配置构建类型,可打出不同类型的包,默认有debug和release两种,你还可以在增加N种

    • applicationIdSuffix修改applicationId,在默认applicationId的基础上加后缀。在buildType中修改 applicationId时只能加后缀,不能完全修改
    • 调试的设置是否生成调试版的APK
    • jniDebuggable设置生成的APK是否支持调试本地代码
    • minifyEnabled设置是否执行混淆
    • multiDexEnabled无论多地塞米松为这种变异启用。
    • renderscriptDebuggable设置生成的APK是否支持调试RenderScript代码
    • renderscriptOptimLevel设置RenderScript优化级别
    • signingConfig设置签名信息
    • versionNameSuffix修改版本名称,在默认版本名称的基础上加后缀。在buildType中修改版本名称时只能加后缀,不能完全修改
    • zipAlignEnabled设置是否对APK包执行ZIP对齐优化
    • proguardFile(proguardFile)添加一个混淆文件
    • proguardFiles(proguardFileArray)添加多个混淆文件
    • setProguardFiles(proguardFileIterable)设置多个混淆文件
  • productFlavors:配置不同风格的APP,在buildTypes的基础上还可以让每一个类型的APP拥有不同的风格,所以最终打出的APK的数量就是buildTypes乘以productFlavors

    • 的applicationID设置应用ID
    • multiDexEnabled无论多地塞米松是由该产品的味道用这个variant.signingConfig签名配置启用。
    • testApplicationId设置测试时的应用ID
    • testFunctionalTest见仪器。
    • testHandleProfiling见仪器。
    • testInstrumentationRunner测试仪表亚军类名​​。
    • 的versionCode设置版本号
    • 的versionName设置版本名称
    • 的minSdkVersion(INT的minSdkVersion)设置兼容的最小SDK版本
    • 的minSdkVersion(字符串的minSdkVersion)设置兼容的最小版本
    • proguardFile(proguardFile)添加一个混淆文件
    • proguardFiles(proguardFileArray)添加多个混淆文件
    • setProguardFiles(proguardFileIterable)设置多个混淆文件
    • targetSdkVersion(INT targetSdkVersion)设置目标SDK版本
    • targetSdkVersion(字符串targetSdkVersion)设置目标SDK版本
  • testOptions:设置测试相关属性

    • reportDir设置测试报告的目录
    • resultsDir设置测试结果的目录
  • aaptOptions:设置AAPT的属性

    • failOnMissingConfigEntry部队AAPT如果它不能找到一个配置的条目返回一个错误。
    • ignoreAssets模式描述的资产是不容忽视。
    • 不会被存储在APK的压缩文件的NOCOMPRESS扩展。
    • useNewCruncher是否使用新排排坐。
  • lintOptions:设置林特的属性

    • abortOnError设置是否在皮棉发生错误时终止构建
    • absolutePaths皮棉是否应显示在错误输出全路径。默认的路径是相对路径棉绒被调用。
    • 检查确切的一系列问题进行检查,或空运行通过LintOptions.getEnable()和无()通过LintOptions.getDisable禁用问题启用了默认启用的问题,加上任何问题。如果非空,主叫方被允许修改此集合。
    • checkAllWarnings返回皮棉是否应该检查所有警告,包括默认的关闭。
    • checkReleaseBuilds返回皮棉是否应检查致命错误时发布版本。默认值为true。如果发现严重性“致命”的问题,发布版本被中止。
    • 禁止设置的问题ID的压制。来电者被允许修改此集合。
    • 使这组问题编号的启用。来电者被允许修改此集合。要启用一个给定的问题,该问题ID添加到返回的集中。
    • explainIssues返回皮棉是否应包括问题的错误解释。(请注意,HTML和XML报告故意这样做无条件的,忽略此设置。)
    • htmlOutput到HTML报告应写入的可选路径。
    • htmlReport我们是否应该写一个HTML报告。缺省为true。该位置可以通过LintOptions.getHtmlOutput控制()。
    • ignoreWarnings返回皮棉是否只检查错误(忽略警告)。
    • lintConfig默认的配置文件,以作为后备使用。
    • noLines皮棉是否应包括在发生错误的输出源线(默认为true)。
    • 安静返回是否掉毛应该是安静的(比如,不写信息消息如路径报告写入的文件)。
    • severityOverrides严重性覆盖的一个可选的映射。从发行ID的地图映射到相应的程度来使用,它必须是“致命”,“错误”,“警告”或“忽略”。
    • SHOWALL返回皮棉是否应包括所有的输出(如包括所有的备用位置,不会截断长的消息等)
    • textOutput到一个文本报告应写入的可选路径。特殊值“标准输出”,可以用来指向标准输出。
    • textReport我们是否应写一个文字报告。默认为false。该位置可以通过LintOptions.getTextOutput控制()。
    • warningsAsErrors返回皮棉是否应该将所有警告视为错误。
    • xmlOutput可选路径到一个XML报告应被写入。
    • xmlReport我们是否应编写XML报告。缺省为true。该位置可以通过LintOptions.getXmlOutput控制()。
    • 检查(ID)将ID的一系列问题进行检查。
    • 检查(IDS)将IDS的一系列问题进行检查。
    • 禁用(ID)将ID的一系列问题使。
    • 禁用(IDS)将IDS的一系列问题使。
    • 使能(ID)将ID的一系列问题使。
    • 使能(IDS)将IDS的一系列问题使。
    • 错误(ID)添加一个覆盖程度对于给定的问题。
    • 错误(IDS)添加一个覆盖程度对于给定的问题。
    • 致命(ID)添加一个覆盖程度对于给定的问题。
    • 致命(IDS)添加一个覆盖程度对于给定的问题。
    • 忽略(ID),增加了对特定问题的严重性覆盖。
    • 忽略(IDS)增加了对特定问题的严重性覆盖。
    • 警告(ID),增加了对特定问题的严重性覆盖。
    • 警告(IDS)增加了对特定问题的严重性覆盖。
  • dexOptions

    • 增量是否启用DX增量模式。这有很大的局限性,可能无法正常工作。请小心使用。
    • javaMaxHeapSize调用DX时,设置-JXmx *值。格式应遵循1024M模式。
    • jumboMode启用DX(--force巨无霸)巨型模式。
    • preDexLibraries无论是预DEX库。这可以提高增量编译,但干净版本可能会比较慢。
  • compileOptions:设置编译的相关属性

    • sourceCompatibility语言水平的源代码。
    • 生成的Java字节码的targetCompatibility版本。
  • packagingOptions:设置APK包的相关属性

    • 排除排除路径的列表。
    • pickFirsts的其中第一发生被打包在APK路径的列表。
    • 排除(路径)添加一个排除的路径。
    • PICKFIRST(路径)添加firstPick路径。先挑路径确实会打包在APK,但只有第一次出现被打包。
  • jacoco:设置JaCoCo的相关属性

    • 版本设置JaCoCo的版本
  • 拆分:设置如何拆分APK(比如你想拆分成手臂版和86版)

    • ABI ABI设置。
    • abiFilters用于多APK ABI过滤器的列表。
    • 密度密度设置。
    • densityFilters用于多APK密度滤镜的列表。

依赖关系:配置依赖

参考文档:
最权威的官方打包指南(需要翻墙)http://tools.android.com/tech-docs/new-build-system
Android打包插件API(在线版)http://apdr.qiniudn.com/index.html

如果你对于手动配置build.gradle文件还不太熟练,那么可以使用AS提供的图形界面来配置,按下CMD+;即可打开配置页面


新特性:
Google在用Gradle最为Android打包工具的时候引入了applicationId的概念,这是为了打多个不同ID的APK包准备的。
applicationId可以和清单文件中的packageName不一样,我们在代码中通过getPackageName()方法拿到的是applicationId,而清单文件中配置的packageName则仅作为R.java和BuildConfig.java的存放目录。

这样一来通过Class.forName(getPackageName()+”.R”)来获取R类的方式就行不通了,一定要注意。

打包

的build.gradle文件配置完成后,打开终端,进入项目目录下,执行gradle这个build即可打包,打包结束后在相应module的build/outputs/apk/目录下可以看到.apk文件

如果你是在项目目录下执行的打包命令,那么会对项目中所有的module都打包,进入某个module目录下执行打包命令就只对当前module打包,每个module打包生成的APK都才存放在mudule的build/outputs/apk目录下







翻译翻译翻译翻译翻译翻译翻译翻译翻译翻译翻译


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


配置build.gradle

AS中APP所有的配置尽在一个build.gradle文件中,打包的时候也是解析build.gralde文件来打包的,所以搞懂build.gradle文件是至关重要的,结构如下所示


apply plugin用来指定用的是哪个插件,取值有:

  • com.android.application:Android APP插件(打包得到的是.apk文件)
  • com.android.library:Android库插件(打包得到的是.aar文件)

android用来指定Android打包插件的相关属性,其包含如下节点

  • compileSdkVersion(apiLevel):设置编译时用的Android版本
  • buildToolsVersion(buildToolsVersionName):设置编译时使用的构建工具的版本
  • defaultConfig:设置一些默认属性,其可用属性是buildTypes和ProductFlavors之和
  • sourceSets:配置相关源文件的位置,当你的项目的目录结构跟默认的有区别但又不想改的时候sourceSets就派上用场了

    • aidl 设置aidi的目录
    • assets 设置assets资源目录
    • compileConfigurationName The name of the compile configuration for this source set.
    • java Java源代码目录
    • jni JNI代码目录
    • jniLibs 已编译好的JNI库目录
    • manifest 指定清单文件
    • name The name of this source set.
    • packageConfigurationName The name of the runtime configuration for this source set.
    • providedConfigurationName The name of the compiled-only configuration for this source set.
    • renderscript Renderscript源代码目录
    • res 资源目录
    • setRoot(path) 根目录
  • signingConfigs:配置签名信息

    • keyAlias 签名的别名
    • keyPassword 密码
    • storeFile 签名文件的路径
    • storePassword 签名密码
    • storeType 类型
  • buildTypes:配置构建类型,可打出不同类型的包,默认有debug和release两种,你还可以在增加N种

    • applicationIdSuffix 修改applicationId,在默认applicationId的基础上加后缀。在buildType中修改 applicationId时只能加后缀,不能完全修改
    • debuggable 设置是否生成debug版的APK
    • jniDebuggable 设置生成的APK是否支持调试本地代码
    • minifyEnabled 设置是否执行混淆
    • multiDexEnabled Whether Multi-Dex is enabled for this variant.
    • renderscriptDebuggable 设置生成的APK是否支持调试RenderScript代码
    • renderscriptOptimLevel 设置RenderScript优化级别
    • signingConfig 设置签名信息
    • versionNameSuffix 修改版本名称,在默认版本名称的基础上加后缀。在buildType中修改版本名称时只能加后缀,不能完全修改
    • zipAlignEnabled 设置是否对APK包执行ZIP对齐优化
    • proguardFile(proguardFile) 添加一个混淆文件
    • proguardFiles(proguardFileArray) 添加多个混淆文件
    • setProguardFiles(proguardFileIterable) 设置多个混淆文件
  • productFlavors:配置不同风格的APP,在buildTypes的基础上还可以让每一个类型的APP拥有不同的风格,所以最终打出的APK的数量就是buildTypes乘以productFlavors

    • applicationId 设置应用ID
    • multiDexEnabled Whether Multi-Dex is enabled for this variant.signingConfig Signing config used by this product flavor.
    • testApplicationId 设置测试时的应用ID
    • testFunctionalTest See instrumentation.
    • testHandleProfiling See instrumentation.
    • testInstrumentationRunner Test instrumentation runner class name.
    • versionCode 设置版本号
    • versionName 设置版本名称
    • minSdkVersion(int minSdkVersion) 设置兼容的最小SDK版本
    • minSdkVersion(String minSdkVersion) 设置兼容的最小版本
    • proguardFile(proguardFile) 添加一个混淆文件
    • proguardFiles(proguardFileArray) 添加多个混淆文件
    • setProguardFiles(proguardFileIterable) 设置多个混淆文件
    • targetSdkVersion(int targetSdkVersion) 设置目标SDK版本
    • targetSdkVersion(String targetSdkVersion) 设置目标SDK版本
  • testOptions:设置测试相关属性

    • reportDir 设置测试报告的目录
    • resultsDir 设置测试结果的目录
  • aaptOptions:设置AAPT的属性

    • failOnMissingConfigEntry Forces aapt to return an error if it fails to find an entry for a configuration.
    • ignoreAssets Pattern describing assets to be ignore.
    • noCompress Extensions of files that will not be stored compressed in the APK.
    • useNewCruncher Whether to use the new cruncher.
  • lintOptions:设置Lint的属性

    • abortOnError 设置是否在lint发生错误时终止构建
    • absolutePaths Whether lint should display full paths in the error output. By default the paths are relative to the path lint was invoked from.
    • check The exact set of issues to check, or null to run the issues that are enabled by default plus any issues enabled via LintOptions.getEnable() and without issues disabled via LintOptions.getDisable(). If non-null, callers are allowed to modify this collection.
    • checkAllWarnings Returns whether lint should check all warnings, including those off by default.
    • checkReleaseBuilds Returns whether lint should check for fatal errors during release builds. Default is true. If issues with severity "fatal" are found, the release build is aborted.
    • disable The set of issue id's to suppress. Callers are allowed to modify this collection.
    • enable The set of issue id's to enable. Callers are allowed to modify this collection. To enable a given issue, add the issue ID to the returned set.
    • explainIssues Returns whether lint should include explanations for issue errors. (Note that HTML and XML reports intentionally do this unconditionally, ignoring this setting.)
    • htmlOutput The optional path to where an HTML report should be written.
    • htmlReport Whether we should write an HTML report. Default true. The location can be controlled by LintOptions.getHtmlOutput().
    • ignoreWarnings Returns whether lint will only check for errors (ignoring warnings).
    • lintConfig The default configuration file to use as a fallback.
    • noLines Whether lint should include the source lines in the output where errors occurred (true by default).
    • quiet Returns whether lint should be quiet (for example, not write informational messages such as paths to report files written).
    • severityOverrides An optional map of severity overrides. The map maps from issue id's to the corresponding severity to use, which must be "fatal", "error", "warning", or "ignore".
    • showAll Returns whether lint should include all output (e.g. include all alternate locations, not truncating long messages, etc.)
    • textOutput The optional path to where a text report should be written. The special value "stdout" can be used to point to standard output.
    • textReport Whether we should write an text report. Default false. The location can be controlled by LintOptions.getTextOutput().
    • warningsAsErrors Returns whether lint should treat all warnings as errors.
    • xmlOutput The optional path to where an XML report should be written.
    • xmlReport Whether we should write an XML report. Default true. The location can be controlled by LintOptions.getXmlOutput().
    • check(id) Adds the id to the set of issues to check.
    • check(ids) Adds the ids to the set of issues to check.
    • disable(id) Adds the id to the set of issues to enable.
    • disable(ids) Adds the ids to the set of issues to enable.
    • enable(id) Adds the id to the set of issues to enable.
    • enable(ids) Adds the ids to the set of issues to enable.
    • error(id) Adds a severity override for the given issues.
    • error(ids) Adds a severity override for the given issues.
    • fatal(id) Adds a severity override for the given issues.
    • fatal(ids) Adds a severity override for the given issues.
    • ignore(id) Adds a severity override for the given issues.
    • ignore(ids) Adds a severity override for the given issues.
    • warning(id) Adds a severity override for the given issues.
    • warning(ids) Adds a severity override for the given issues.
  • dexOptions

    • incremental Whether to enable the incremental mode for dx. This has many limitations and may not work. Use carefully.
    • javaMaxHeapSize Sets the -JXmx* value when calling dx. Format should follow the 1024M pattern.
    • jumboMode Enable jumbo mode in dx (--force-jumbo).
    • preDexLibraries Whether to pre-dex libraries. This can improve incremental builds, but clean builds may be slower.
  • compileOptions:设置编译的相关属性

    • sourceCompatibility Language level of the source code.
    • targetCompatibility Version of the generated Java bytecode.
  • packagingOptions:设置APK包的相关属性

    • excludes The list of excluded paths.
    • pickFirsts The list of paths where the first occurrence is packaged in the APK.
    • exclude(path) Adds an excluded paths.
    • pickFirst(path) Adds a firstPick path. First pick paths do get packaged in the APK, but only the first occurrence gets packaged.
  • jacoco:设置JaCoCo的相关属性

    • version 设置JaCoCo的版本
  • splits:设置如何拆分APK(比如你想拆分成arm版和x86版)

    • abi ABI settings.
    • abiFilters The list of ABI filters used for multi-apk.
    • density Density settings.
    • densityFilters The list of Density filters used for multi-apk.

dependencies:配置依赖

参考文档:
最权威的官方打包指南(需要翻墙)http://tools.android.com/tech-docs/new-build-system
Android打包插件API(在线版)http://apdr.qiniudn.com/index.html

如果你对于手动配置build.gradle文件还不太熟练,那么可以使用AS提供的图形界面来配置,按下CMD+;即可打开配置页面


新特性:
Google在用Gradle最为Android打包工具的时候引入了applicationId的概念,这是为了打多个不同ID的APK包准备的。
applicationId可以和清单文件中的packageName不一样,我们在代码中通过getPackageName()方法拿到的是applicationId,而清单文件中配置的packageName则仅作为R.java和BuildConfig.java的存放目录。

这样一来通过Class.forName(getPackageName()+”.R”)来获取R类的方式就行不通了,一定要注意。

打包

build.gradle文件配置完成后,打开终端,进入项目目录下,执行gradle build即可打包,打包结束后在相应module的build/outputs/apk/目录下可以看到.apk文件

如果你是在项目目录下执行的打包命令,那么会对项目中所有的module都打包,进入某个module目录下执行打包命令就只对当前module打包,每个module打包生成的APK都才存放在mudule的build/outputs/apk目录下



文/浮游大虾(简书作者)
原文链接:http://www.jianshu.com/p/7e3a69dbd20e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。






0 0