模块配置文件-build.gradle

位于模块的根目录下build.gradle就是此模块配置文件,用来配置模块构建配置;

4. 模块配置we文件

4.1 apply plugin

这个用来指明为Gradle配置Android Plugin来进行构建,

eg:

表明此模块是应用程序

apply plugin: 'com.android.application'

表明此模块是库模块

apply plugin: 'com.android.library'

4.2 android{ }

android构建的特定配置;

4.2.1 compileSdkVersion

指定项目编译需要的SDK API版本

compileSdkVersion 26

4.2.2  buildToolsVersion

指定SDK build-tools构建工具的版本,包括了打包工具aapt、dx等等,位于sdk目录下的build-tools目录下对应的版本;

 buildToolsVersion "23.0.2"

4.2.2 defaultConfig{}

 defaultConfig {
        applicationId "com.niubashaoye.demo.ndkdemo"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

1. applicationId:用于指定项目的包名,如果包名进行修改这样也需要修改;

2. minSdkVersion:项目最小兼容版本

3. targetSdkVersion:项目的目标版本,在此版本进过充分测试,如果高版本提供了新功能,就不会启用;

4. versionCode:项目的版本编号

5. versionName:项目的版本名称;

4.2.3 buildTypes{}

里面会有release{}和debug{}来设置

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

以release为例:

1. minifyEnabled :项目是否进行混淆(true/false)

2. proguardFiles:指定混淆时使用的文件;
    proguard-android.txt是Android SDK目录下的通用混淆规则;
    proguard-rules.pro:在模块目录下,用来编写本模块的混淆规则

4.2 productFlavors {}可选

配置多渠道打包工具,根据配置不同的逻辑、构建部分来实现版本的差异化;

例如分为免费版本和付费版本,指定哪些功能只能付费版本使用;

productFlavors {
    free {
      applicationId 'com.example.myapp.free'
    }

    paid {
      applicationId 'com.example.myapp.paid'
    }
  }

4.3 splits APK分割 可选

设置生成不同ABI版本的APK应用程序,一般不用设置;

参考:官方构建多个APK

4.4 dependencies{ }

指定当前模块的依赖关系,本地依赖库、远程依赖库和库模块依赖

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(':lib')
}

本地依赖库:implementation fileTree(include: ['*.jar'], dir: 'libs')

远程依赖库:

 implementation 'com.android.support:appcompat-v7:26.1.0'
 implementation 'com.android.support.constraint:constraint-layout:1.1.2'
 implementation 'com.android.support:design:26.1.0'

库模块依赖:implementation project(':lib')

5 优化build配置文件

1.为开发创建构建变体

2.避免编译不必要的资源

3.为您的调试构建停用 Crashlytics

4.将静态构建配置值与调试构建结合使用

5.配置 dexOptions 和启用库预 dexing

6.增加 Gradle 的堆大小并启用 dex-in-process

7.图片转换为WebP,停用PNG

猜你喜欢

转载自blog.csdn.net/niuba123456/article/details/81074171