build.gralde檔案詳解
阿新 • • 發佈:2018-11-03
AS中APP所有的配置盡在一個build.gradle檔案中,打包的時候也是解析build.gralde檔案來打包的,所以搞懂build.gradle檔案是至關重要的,結構如下所示
1、apply plugin用來指定用的是哪個外掛,取值有:
3、dependencies:配置依賴 參考: http://blog.csdn.net/xx326664162/article/details/48175069 參考文件:
如果你對於手動配置build.gradle檔案還不太熟練,那麼可以使用AS提供的圖形介面來配置,按下CMD+;即可開啟配置頁面
新特性: Google在用Gradle最為Android打包工具的時候引入了applicationId的概念,這是為了打多個不同ID的APK包準備的。 applicationId可以和清單檔案中的packageName不一樣,我們在程式碼中通過getPackageName()方法拿到的是applicationId,而清單檔案中配置的packageName則僅作為R.java和BuildConfig.java的存放目錄。 這樣一來通過Class.forName(getPackageName()+”.R”)來獲取R類的方式就行不通了,一定要注意。 applicationId和packageName的區別參考:http://blog.csdn.net/xx326664162/article/details/48468219
打包
build.gradle檔案配置完成後,開啟終端,進入專案目錄下,執行gradle build即可打包,打包結束後在相應module的build/outputs/apk/目錄下可以看到.apk檔案
如果你是在專案目錄下執行的打包命令,那麼會對專案中所有的module都打包,進入某個module目錄下執行打包命令就只對當前module打包,每個module打包生成的APK都才存放在mudule的build/outputs/apk目錄下
1、apply plugin用來指定用的是哪個外掛,取值有:
- com.android.application:Android APP外掛(打包得到的是.apk檔案)
- com.android.library:Android庫外掛(打包得到的是.aar檔案)
- 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 型別
- keyAlias 簽名的別名
- 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對齊優化,減小zip體積,增加執行效率(類比於ssd的4k對齊)
- proguardFile(proguardFile) 新增一個混淆檔案
- proguardFiles(proguardFileArray) 新增多個混淆檔案
- setProguardFiles(proguardFileIterable) 設定多個混淆檔案
- applicationIdSuffix 修改applicationId,在預設applicationId的基礎上加字尾。在buildType中修改applicationId時只能加字尾,不能完全修改
- 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版本
- applicationId 設定應用ID
- testOptions:設定測試相關屬性
-
- reportDir 設定測試報告的目錄
- resultsDir 設定測試結果的目錄
- reportDir 設定測試報告的目錄
- 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.
- failOnMissingConfigEntry Forces aapt to return an error if it fails to find an entry for a configuration.
- 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.
- abortOnError 設定是否在lint發生錯誤時終止構建
- 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.
- incremental Whether to enable the incremental mode for dx. This has many limitations and may not work. Use carefully.
- compileOptions:設定編譯的相關屬性
-
- sourceCompatibility Language level of the source code.
- targetCompatibility Version of the generated Java bytecode.
- sourceCompatibility Language level of the source code.
- 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.
- excludes The list of excluded paths.
- jacoco:設定JaCoCo的相關屬性
-
- version 設定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.
- abi ABI settings.
3、dependencies:配置依賴 參考: http://blog.csdn.net/xx326664162/article/details/48175069 參考文件:
- 最權威的官方打包指南(需要翻牆)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類的方式就行不通了,一定要注意。 applicationId和packageName的區別參考:http://blog.csdn.net/xx326664162/article/details/48468219
打包
build.gradle檔案配置完成後,開啟終端,進入專案目錄下,執行gradle build即可打包,打包結束後在相應module的build/outputs/apk/目錄下可以看到.apk檔案
如果你是在專案目錄下執行的打包命令,那麼會對專案中所有的module都打包,進入某個module目錄下執行打包命令就只對當前module打包,每個module打包生成的APK都才存放在mudule的build/outputs/apk目錄下