原文地址
AS中APP所有的配置盡在一個(gè)build.gradle文件中,打包的時(shí)候也是解析build.gralde文件來打包的倡缠,所以搞懂build.gradle文件是至關(guān)重要的攒菠,結(jié)構(gòu)如下所示
1迫皱、apply plugin用來指定用的是哪個(gè)插件,取值有:
- com.android.application:Android APP插件(打包得到的是.apk文件)
- com.android.library:Android庫插件(打包得到的是.aar文件)
2辖众、android用來指定Android打包插件的相關(guān)屬性卓起,其包含如下節(jié)點(diǎn)
compileSdkVersion(apiLevel):設(shè)置編譯時(shí)用的Android版本
buildToolsVersion(buildToolsVersionName):設(shè)置編譯時(shí)使用的構(gòu)建工具的版本
defaultConfig:設(shè)置一些默認(rèn)屬性,其可用屬性是buildTypes和ProductFlavors之和
sourceSets:配置相關(guān)源文件的位置凹炸,當(dāng)你的項(xiàng)目的目錄結(jié)構(gòu)跟默認(rèn)的有區(qū)別但又不想改的時(shí)候sourceSets就派上用場(chǎng)了
- aidl 設(shè)置aidi的目錄
- assets 設(shè)置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:配置構(gòu)建類型戏阅,可打出不同類型的包,默認(rèn)有debug和release兩種啤它,你還可以在增加N種
applicationIdSuffix 修改applicationId左冬,在默認(rèn)applicationId的基礎(chǔ)上加后綴舷嗡。在buildType中修改applicationId時(shí)只能加后綴,不能完全修改
debuggable 設(shè)置是否生成debug版的APK
jniDebuggable 設(shè)置生成的APK是否支持調(diào)試本地代碼
minifyEnabled 設(shè)置是否執(zhí)行代碼混淆
multiDexEnabled Whether Multi-Dex is enabled for this variant.
renderscriptDebuggable 設(shè)置生成的APK是否支持調(diào)試RenderScript代碼
renderscriptOptimLevel 設(shè)置RenderScript優(yōu)化級(jí)別
signingConfig 設(shè)置簽名信息
versionNameSuffix 修改版本名稱,在默認(rèn)版本名稱的基礎(chǔ)上加后綴婶熬。在buildType中修改版本名稱時(shí)只能加后綴,不能完全修改
zipAlignEnabled 設(shè)置是否對(duì)APK包執(zhí)行ZIP對(duì)齊優(yōu)化炉爆,減小zip體積蔬浙,增加運(yùn)行效率(類比于ssd的4k對(duì)齊)
proguardFile(proguardFile) 添加一個(gè)混淆文件
proguardFiles(proguardFileArray) 添加多個(gè)混淆文件
setProguardFiles(proguardFileIterable) 設(shè)置多個(gè)混淆文件
productFlavors:配置不同風(fēng)格的APP,在buildTypes的基礎(chǔ)上還可以讓每一個(gè)類型的APP擁有不同的風(fēng)格誊爹,所以最終打出的APK的數(shù)量就是buildTypes乘以productFlavors
applicationId 設(shè)置應(yīng)用ID
multiDexEnabled Whether Multi-Dex is enabled for this variant.signingConfig Signing config used by this product flavor.
testApplicationId 設(shè)置測(cè)試時(shí)的應(yīng)用ID
testFunctionalTest See instrumentation.
testHandleProfiling See instrumentation.
testInstrumentationRunner Test instrumentation runner class name.
versionCode 設(shè)置版本號(hào)
versionName 設(shè)置版本名稱
minSdkVersion(int minSdkVersion) 設(shè)置兼容的最小SDK版本
minSdkVersion(String minSdkVersion) 設(shè)置兼容的最小版本
proguardFile(proguardFile) 添加一個(gè)混淆文件
proguardFiles(proguardFileArray) 添加多個(gè)混淆文件
setProguardFiles(proguardFileIterable) 設(shè)置多個(gè)混淆文件
targetSdkVersion(int targetSdkVersion) 設(shè)置目標(biāo)SDK版本
targetSdkVersion(String targetSdkVersion) 設(shè)置目標(biāo)SDK版本
testOptions:設(shè)置測(cè)試相關(guān)屬性
reportDir 設(shè)置測(cè)試報(bào)告的目錄
resultsDir 設(shè)置測(cè)試結(jié)果的目錄
aaptOptions:設(shè)置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:設(shè)置Lint的屬性
abortOnError 設(shè)置是否在lint發(fā)生錯(cuò)誤時(shí)終止構(gòu)建
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:設(shè)置編譯的相關(guān)屬性
sourceCompatibility Language level of the source code.
targetCompatibility Version of the generated Java bytecode.
packagingOptions:設(shè)置APK包的相關(guān)屬性
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:設(shè)置JaCoCo的相關(guān)屬性
- version 設(shè)置JaCoCo的版本
splits:設(shè)置如何拆分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.
3蹬刷、dependencies:配置依賴
參考:[http://blog.csdn.net/xx326664162/article/details/48175069](http://blog.csdn.net/xx326664162/article/details/48175069)
參考文檔:
- 最權(quán)威的官方打包指南(需要翻墻)http://tools.android.com/tech-docs/new-build-system
- Android打包插件API(在線版)http://apdr.qiniudn.com/index.html
如果你對(duì)于手動(dòng)配置build.gradle文件還不太熟練,那么可以使用AS提供的圖形界面來配置频丘,按下CMD+;即可打開配置頁面
新特性:
Google在用Gradle最為Android打包工具的時(shí)候引入了applicationId的概念办成,這是為了打多個(gè)不同ID的APK包準(zhǔn)備的。
applicationId可以和清單文件中的packageName不一樣搂漠,我們?cè)诖a中通過getPackageName()方法拿到的是applicationId迂卢,而清單文件中配置的packageName則僅作為R.java和BuildConfig.java的存放目錄。
這樣一來通過Class.forName(getPackageName()+”.R”)來獲取R類的方式就行不通了桐汤,一定要注意而克。
applicationId和packageName的區(qū)別參考:http://blog.csdn.net/xx326664162/article/details/48468219
打包
build.gradle文件配置完成后,打開終端怔毛,進(jìn)入項(xiàng)目目錄下员萍,執(zhí)行g(shù)radle build即可打包,打包結(jié)束后在相應(yīng)module的build/outputs/apk/目錄下可以看到.apk文件
如果你是在項(xiàng)目目錄下執(zhí)行的打包命令拣度,那么會(huì)對(duì)項(xiàng)目中所有的module都打包碎绎,進(jìn)入某個(gè)module目錄下執(zhí)行打包命令就只對(duì)當(dāng)前module打包螃壤,每個(gè)module打包生成的APK都才存放在mudule的build/outputs/apk目錄下