Localized Android Gradle build skills

Custom Gradle profile

config.gradle

In the root directory of the new config.gradle

Here Insert Picture Description
config.gradle reads as follows:

ext {
    //Android 配置
    android = [
            compileSdkVersion: 29,
            buildToolsVersion: "29.0.1",
            applicationId    : "com.xx.xx",
            minSdkVersion    : 21,
            targetSdkVersion : 29,
            versionCode      : 8,
            versionName      : "1.7"
    ]
    //依赖 配置
    dependencies = [
            "httpclient"     : 'org.apache.http.legacy',
            "appcompat"      : 'androidx.appcompat:appcompat:1.0.2',
            "bmobsdk"        : 'cn.bmob.android:bmob-sdk:3.7.3-rc1',
            "rxjava2"        : "io.reactivex.rxjava2:rxjava:2.2.2",
            "rxandroid2"     : 'io.reactivex.rxjava2:rxandroid:2.1.0',
            "okio"           : 'com.squareup.okio:okio:2.1.0',
            "gson"           : 'com.google.code.gson:gson:2.8.5',
            "okhttp3"        : 'com.squareup.okhttp3:okhttp:3.12.0',
            "tagcloud"       : 'com.moxun:tagcloudlib:1.2.0',
            "circleimageview": 'de.hdodenhof:circleimageview:3.0.0',
            "glide"          : 'com.github.bumptech.glide:glide:4.9.0',
            "recyclerview"   : 'androidx.recyclerview:recyclerview:1.0.0',
            "datepicker"     : 'cn.aigestudio.datepicker:DatePicker:2.2.0',
            "litepal"        : 'org.litepal.android:java:3.0.0',
            "eventbus"       : 'org.greenrobot:eventbus:3.1.1',
            "tablayout"      : 'com.google.android.material:material:1.0.0',
            "photoview"      : 'com.github.chrisbanes:PhotoView:2.3.0',
            "3dmap"          : 'com.amap.api:3dmap:5.0.0',
            "location"       : 'com.amap.api:location:3.3.0',
            "search"         : 'com.amap.api:search:5.0.0',
            "bugly"          : 'com.tencent.bugly:crashreport:2.1.9',
            "buglyndk"       : 'com.tencent.bugly:nativecrashreport:3.0',
            "zxing"          : 'cn.yipianfengye.android:zxing-library:2.2',
            "videoplayer"    : 'cn.jzvd:jiaozivideoplayer:7.0.5'
    ]
    //仓库
    maven = [
            "bmobUrl": "https://raw.github.com/bmob/bmob-android-sdk/master",
            "jitpack": "https://jitpack.io"
    ]
}

The introduction of a custom Gradle

The introduction of a custom configuration file in the root build.gradle Gradle project

//引入自定义Gradle配置文件
apply from: "config.gradle"

app module and the other module can be used build.gradle parameters defined in config.gradle, if data needs to be modified, only need to modify config.gradle, all module in build.gradle not need to make changes.

benefit:

  • 1. Unity
  • 2. manageable
  • 3. Version Management

app的build.gradle

The app build.gradle follows:

android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]
    buildToolsVersion rootProject.ext.android["buildToolsVersion"]

    //默认配置
    defaultConfig {
        applicationId rootProject.ext.android["applicationId"]
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]
    }
...
}

base的build.gradle

android {
    //HttpClient
    useLibrary rootProject.ext.dependencies["httpclient"]
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]
    buildToolsVersion rootProject.ext.android["buildToolsVersion"]
    defaultConfig {
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]
    }
	...
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //V7
    api rootProject.ext.dependencies["appcompat"]
    api rootProject.ext.dependencies["bmobsdk"]
    api rootProject.ext.dependencies["rxjava2"]
    api rootProject.ext.dependencies["rxandroid2"]
    api rootProject.ext.dependencies["okio"]
    api rootProject.ext.dependencies["gson"]
    api rootProject.ext.dependencies["okhttp3"]
    api rootProject.ext.dependencies["tagcloud"]
    api rootProject.ext.dependencies["circleimageview"]
    api rootProject.ext.dependencies["datepicker"]
    api rootProject.ext.dependencies["glide"]
    api rootProject.ext.dependencies["recyclerview"]
    api rootProject.ext.dependencies["litepal"]
    api rootProject.ext.dependencies["eventbus"]
    api rootProject.ext.dependencies["tablayout"]
    api rootProject.ext.dependencies["photoview"]
    api rootProject.ext.dependencies["3dmap"]
    api rootProject.ext.dependencies["location"]
    api rootProject.ext.dependencies["search"]
    api rootProject.ext.dependencies["bugly"]
    api rootProject.ext.dependencies["buglyndk"]
    api rootProject.ext.dependencies["zxing"]
    api rootProject.ext.dependencies["videoplayer"]
}

Gradle configuration constants

BuildConfig open files, view Gradle constant
Here Insert Picture Description
add the following code in buildTypes:
Here Insert Picture Description

      debug {
            //自定义Gradle常量
            buildConfigField("boolean","LOG_DEBUG","true")
            buildConfigField("String","LOG_TAG","\"YueJian\"")
            buildConfigField("String","SP_NAME","\"Config\"")
        }

After a review of BuildConfig file synchronization
Here Insert Picture Description
has been for us to build a constant need

These constants are used as follows, so that you can do some debug build operation, such as log output, whereas in the release version does not perform these operations.
Here Insert Picture Description

Gradle build to enhance the speed of the top ten tips

  • Using the latest version of the plug-in Gradle
  • Avoid activating old Multidex
  • Multidex Apk building ban
  • Minimize resource file package
  • Disabling compression png
  • png conversion webp
  • Recommended instant run
  • Do not use dynamic version identification
  • Tuning memory allocation Gradle
  • Open Gradle build the cache

Multidex Apk building ban

Gradle build three performance indicators:

  • Full amount Compile: Compile all - Open Project
  • Code incremental compilation: Modify the Java / Kotlin following code when compiled
  • Resources incremental compilation: Modify the res following resource file when compiled

Published App requires multi-channel package and all the package when the application market; and debugging time do not need so much, we can ban some files to compile.

How to stop when debugging it?

Settings-Build-Compiler-PdevBuild

Here Insert Picture Description

app的build.gradle:
Here Insert Picture Description

android {
    //调试的时候禁止,加快编译速度
    if(rootProject.hasProperty('devBuild')){
        splits.abi.enable = false
        splits.density.enable = false
        //禁用PNG压缩
        //aaptOptions.cruncherEnabled = false
    }
...

Minimize resource file package

android {
    //默认配置
    defaultConfig {
        //调试
        resConfigs("zh","xxhdpi")
	...
	}
	...
}

Disabling compression png

Here Insert Picture Description

android {
    //调试的时候禁止,加快编译速度
    if(rootProject.hasProperty('devBuild')){
        splits.abi.enable = false
        splits.density.enable = false
        //禁用PNG压缩
        aaptOptions.cruncherEnabled = false
    }
...

png conversion webp

Here Insert Picture Description

Do not use dynamic version identification

Do not use when adding a dependency Plus +

Tuning memory allocation Gradle

Here Insert Picture Description

Open Gradle build the cache

#开启Gradle构建缓存
org.gradle.caching=true

Here Insert Picture Description

Published 446 original articles · won praise 67 · views 240 000 +

Guess you like

Origin blog.csdn.net/hongxue8888/article/details/104692385