【轉載】Android Studio大課堂 - 6.1.打包 - 詳解build.gradle

AS中APP全部的配置盡在一個build.gradle文件中,打包的時候也是解析build.gralde文件來打包的,因此搞懂build.gradle文件是相當重要的,結構以下所示
html


apply plugin用來指定用的是哪一個插件,取值有:
java

  • com.android.application:Android APP插件(打包獲得的是.apk文件)android

  • com.android.library:Android庫插件(打包獲得的是.aar文件)api


android用來指定Android打包插件的相關屬性,其包含以下節點
android-studio

  • compileSdkVersion(apiLevel):設置編譯時用的Android版本app

  • buildToolsVersion(buildToolsVersionName):設置編譯時使用的構建工具的版本ide

  • 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 類型gradle

  • 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:配置依賴

參考文檔:
最權威的官方打包指南(須要fanqiang)http://tools.android.com/tech- ... ystem
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或AAR都才存放在mudule的build/outputs目錄下

相關文章
相關標籤/搜索