gradle configuration skills for a complete Android project

Gradle configuration skills

I summarized four gradle configuration skills, which can be more convenient for debugging and packaging in Android projects
1. Use gradle's custom Property to realize unified management of configuration and dependency of Android project
2. Use gradle's productFlavors to implement Android project multi-channel packaging
3. Use gradle to realize the coexistence of debug version and release version of Android project
4. Use gradle to modify apk file names in batch

Complete Android project gradle configuration:

The skills used in the complete gradle configuration can refer to the above summary of the four gradle techniques

Of the project root build.gradle to configure

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    }
}

plugins {
    id "com.jfrog.bintray" version "1.6"
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext {
    //Android configs
    configs = [
            applicationId    : "com.example.myapplication",
            compileSdkVersion: 25,
            buildToolsVersion: '25.0.2',
            minSdkVersion    : 17,
            targetSdkVersion : 25,
            versionCode      : 17,
            versionName      : '3.1.5'
    ]

    // App dependencies
    supportLibraryVersion = '25.1.1'
    junitVersion = '4.12'
    multidexVersion = '1.0.1'
    gsonVersion = '2.4'
    butterknifeVersion = '8.4.0'
    ......
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

In the app directory build.gradle to configure

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

def buildTime() {
    def date = new Date()
    def formattedDate = date.format('yyyy-MM-dd', TimeZone.getTimeZone("UTC"))
    return formattedDate
}

android {

    signingConfigs {
        release {
            storeFile file('../yourapp.keystore')
            keyAlias 'your password'
            keyPassword 'your alias'
            storePassword 'your password'
        }
    }

    compileSdkVersion configs.compileSdkVersion
    buildToolsVersion configs.buildToolsVersion

    defaultConfig {
        applicationId configs.applicationId
        minSdkVersion configs.minSdkVersion
        targetSdkVersion configs.targetSdkVersion
        versionCode configs.versionCode
        versionName configs.versionName
        // Enabling multidex support.
        multiDexEnabled true
    }

    buildTypes {
        release {
            //Open confusion
            minifyEnabled true
            //Automatically read signature profile when packaging
            signingConfig signingConfigs.release
            //Turn on zip alignment
            zipAlignEnabled true 
            //Remove useless resource files
            shrinkResources true 
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //Set the release version of the app_name and application_id
            manifestPlaceholders = [
                    APP_NAME      : "@string/app_name",
                    APPLICATION_ID: "@string/application_id"
            ]
            //Set the release version to contain only so packages of armeabi and armeabi-v7a
            ndk {
                abiFilters "armeabi", "armeabi-v7a"
            }
            //Batch modify the generated apk file name
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // The output APK name is AppName_v1.0_2017-02-09_wandoujia.apk
                        def apkFile = "AppName_v${defaultConfig.versionName}_${buildTime()}" +
                                "_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, apkFile)
                    }
                }
            }
        }

        debug {
            //Set the registration of debug version as < application ID >. Debug
            applicationIdSuffix ".debug"
            //Set the debug version of the app_name and application_id
            manifestPlaceholders = [
                    APP_NAME      : "@string/app_name_debug",
                    APPLICATION_ID: "@string/application_id_debug"
            ]
            //Set the debug version to include x86 so files
            ndk {
                abiFilters "armeabi", "armeabi-v7a", "x86"
            }
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    dexOptions {
        javaMaxHeapSize "2g"
    }

    //Multi channel packaging of friendship and Alliance
    productFlavors {
        wandoujia {}
        baidu {}
        _360 {}
        _91 {}
        anzhuomarket {}
        huawei {}
        xiaomi {}
        lenovo {}
        tencent {}
        coolapk {}
        meizu {}
        vivo {}
        ali {}
        yulin {}
    }

    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

dependencies {
    testCompile "junit:junit:$rootProject.junitVersion"
    compile "com.android.support:multidex:$rootProject.multidexVersion"
    compile "com.google.code.gson:gson:$rootProject.gsonVersion"
    //MaterialDesign
    compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
    compile "com.android.support:design:$rootProject.supportLibraryVersion"
    //CardView
    compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
    //RecyclerView
    compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
    //butterKnife
    compile "com.jakewharton:butterknife:$rootProject.butterknifeVersion"
    apt "com.jakewharton:butterknife-compiler:$rootProject.butterknifeVersion"
    ......
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

Transferred from: http://blog.csdn.net/lj402159806/article/details/54958056

Tags: Android Gradle ButterKnife Maven

Posted on Tue, 30 Jun 2020 12:40:10 -0400 by Tomcat13