[Gradle] Android項(xiàng)目build.gradle詳解

原文地址
AS中APP所有的配置盡在一個(gè)build.gradle文件中,打包的時(shí)候也是解析build.gralde文件來打包的倡缠,所以搞懂build.gradle文件是至關(guān)重要的攒菠,結(jié)構(gòu)如下所示

image

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)

參考文檔:

如果你對(duì)于手動(dòng)配置build.gradle文件還不太熟練,那么可以使用AS提供的圖形界面來配置频丘,按下CMD+;即可打開配置頁面

image

新特性:

 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目錄下

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市筋帖,隨后出現(xiàn)的幾起案子奸晴,更是在濱河造成了極大的恐慌,老刑警劉巖幕随,帶你破解...
    沈念sama閱讀 211,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚁滋,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡赘淮,警方通過查閱死者的電腦和手機(jī)辕录,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來梢卸,“玉大人走诞,你說我怎么就攤上這事「蚋撸” “怎么了蚣旱?”我有些...
    開封第一講書人閱讀 157,435評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長戴陡。 經(jīng)常有香客問我塞绿,道長,這世上最難降的妖魔是什么恤批? 我笑而不...
    開封第一講書人閱讀 56,509評(píng)論 1 284
  • 正文 為了忘掉前任异吻,我火速辦了婚禮,結(jié)果婚禮上喜庞,老公的妹妹穿的比我還像新娘诀浪。我一直安慰自己,他們只是感情好延都,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,611評(píng)論 6 386
  • 文/花漫 我一把揭開白布雷猪。 她就那樣靜靜地躺著,像睡著了一般晰房。 火紅的嫁衣襯著肌膚如雪求摇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,837評(píng)論 1 290
  • 那天殊者,我揣著相機(jī)與錄音与境,去河邊找鬼。 笑死幽污,一個(gè)胖子當(dāng)著我的面吹牛嚷辅,可吹牛的內(nèi)容都是我干的簿姨。 我是一名探鬼主播距误,決...
    沈念sama閱讀 38,987評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼簸搞,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了准潭?” 一聲冷哼從身側(cè)響起趁俊,我...
    開封第一講書人閱讀 37,730評(píng)論 0 267
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎刑然,沒想到半個(gè)月后寺擂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,194評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡泼掠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,525評(píng)論 2 327
  • 正文 我和宋清朗相戀三年怔软,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片择镇。...
    茶點(diǎn)故事閱讀 38,664評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡挡逼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出腻豌,到底是詐尸還是另有隱情家坎,我是刑警寧澤,帶...
    沈念sama閱讀 34,334評(píng)論 4 330
  • 正文 年R本政府宣布吝梅,位于F島的核電站虱疏,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏苏携。R本人自食惡果不足惜做瞪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,944評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望兜叨。 院中可真熱鬧穿扳,春花似錦、人聲如沸国旷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽跪但。三九已至履羞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間屡久,已是汗流浹背忆首。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評(píng)論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留被环,地道東北人糙及。 一個(gè)月前我還...
    沈念sama閱讀 46,389評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像筛欢,于是被迫代替她去往敵國和親浸锨。 傳聞我的和親對(duì)象是個(gè)殘疾皇子唇聘,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,554評(píng)論 2 349