【Android Studio】入门系列6.1.打包 - 详解build.gradle

2024-06-19 12:08

本文主要是介绍【Android Studio】入门系列6.1.打包 - 详解build.gradle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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



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





 

配置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 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 



这篇关于【Android Studio】入门系列6.1.打包 - 详解build.gradle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1074981

相关文章

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

十四、观察者模式与访问者模式详解

21.观察者模式 21.1.课程目标 1、 掌握观察者模式和访问者模式的应用场景。 2、 掌握观察者模式在具体业务场景中的应用。 3、 了解访问者模式的双分派。 4、 观察者模式和访问者模式的优、缺点。 21.2.内容定位 1、 有 Swing开发经验的人群更容易理解观察者模式。 2、 访问者模式被称为最复杂的设计模式。 21.3.观察者模式 观 察 者 模 式 ( Obser

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使

Jitter Injection详解

一、定义与作用 Jitter Injection,即抖动注入,是一种在通信系统中人为地添加抖动的技术。该技术通过在发送端对数据包进行延迟和抖动调整,以实现对整个通信系统的时延和抖动的控制。其主要作用包括: 改善传输质量:通过调整数据包的时延和抖动,可以有效地降低误码率,提高数据传输的可靠性。均衡网络负载:通过对不同的数据流进行不同程度的抖动注入,可以实现网络资源的合理分配,提高整体传输效率。增

Eclipse+ADT与Android Studio开发的区别

下文的EA指Eclipse+ADT,AS就是指Android Studio。 就编写界面布局来说AS可以边开发边预览(所见即所得,以及多个屏幕预览),这个优势比较大。AS运行时占的内存比EA的要小。AS创建项目时要创建gradle项目框架,so,创建项目时AS比较慢。android studio基于gradle构建项目,你无法同时集中管理和维护多个项目的源码,而eclipse ADT可以同时打开

android 免费短信验证功能

没有太复杂的使用的话,功能实现比较简单粗暴。 在www.mob.com网站中可以申请使用免费短信验证功能。 步骤: 1.注册登录。 2.选择“短信验证码SDK” 3.下载对应的sdk包,我这是选studio的。 4.从头像那进入后台并创建短信验证应用,获取到key跟secret 5.根据技术文档操作(initSDK方法写在setContentView上面) 6.关键:在有用到的Mo

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现