What is Gradle

Gradle is a build tool that helps you build and package your projects. You can configure your own tasks and easily reference third party libraries

Gradle configuration file for the root project


apply from: "config.gradle"// You can reference your own files with the suffix. Gradle, and extract all configuration information to the server you need to reference for your custom Gradle buildScript {// Remote Maven Google JCenter Jitpack connection Gradle repositories {Google () jCenter () The mission of the classpath'com. Android. Tools. Build: gradle: 3.4.1 track'}} // Configure allprojects {repositories {Google () jCenter () maven {url"https://jitpack.io"}}} // Task clean(type: Delete) {
    delete rootProject.buildDir
}
Copy the code

Configure the App Module

// Indicate that this module is running, and others will have libary.... apply plugin:'com.android.application'Android {buildToolsVersion compileSdkVersion pileSdkVersion/rootProject.ext.android.com/compiled version RootProject. Ext. Android. BuildToolsVersion / / build tools version of compile time using defaultConfig {/ / project applicationId package name RootProject. Ext. Android. Run applicationId / / minimum SDK version B minSdkVersion rootProject. Ext. Android minSdkVersion / / target version TargetSdkVersion rootProject. Ext. Android. TargetSdkVersion / / version number versionCode rootProject. Ext. Android versionCode/name/version VersionName rootProject. Ext. Android. Open the subcontract multiDexEnabled versionName / /true// Junit unit teststestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"// Chinese resources resConfigs'en'// set up the support SO library architecture abiFilters'armeabi'.'x86'.'armeabi-v7a'/ /,'x86_64'.'arm64-v8a'} // Use only the resource file resConfigs under xhdpi"xhdpi"// Subcontract, specify a class in main dex multiDexEnabledtrue
        multiDexKeepProguard file('multiDexKeep.pro') // Confusion rules for classes packaged in the main dex, no special requirements are given an empty file multiDexKeepFile file('maindexlist.txt'// Specify which classes to put in main dex} // Enable databind support for dataBinding {enabled =true} / / signature information signingConfigs {production {/ / signature file path storeFile rootProject. Ext. Signing storeFile storePassword / / password RootProject. Ext. Signing. StorePassword / / alias keyAlias rootProject. Ext. Signing keyAlias / / alias password keyPassword RootProject. Ext. Signing. KeyPassword}} / / build type buildTypes {/ / debug debug mode {/ / additional information versionNameSuffix can release name behind"-debug"// Package name suffixes control the package name applicationIdSuffix for different build types".debug"// Custom resource information is equivalent to the contents of string. XML under value"string"."app_name". RootProject. Ext. Android. DebugAppName / / manifest parameter configuration items icon of desktop icon is displayed in the debug environment manifest manifest file reference manifestPlaceholders = [app_icon: rootProject. Ext. Android. DebugAppIcon] / / configuration signature information signingConfig signingConfigs. If production / / open confuse minifyEnabledfalse// Pack the APK zipAlignEnabled for ziyoufalseRemove shrinkResourcesfalseProguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'// Enable this function to debug the debuggabletrue// Custom parameters are used in Buildconfig where different environments define different urls. BuildConfigField ("String"."API_URL", rootProject. Ext apiUrl. DebugUrl)} / / advance online preRelease {resValue"string"."app_name", rootProject.ext.android.preReleaseAppName
            manifestPlaceholders = [app_icon: rootProject.ext.android.preReleaseAppIcon]
            zipAlignEnabled true
            shrinkResources true
            versionNameSuffix "-preRelease"
            applicationIdSuffix ".preRelease"
            signingConfig signingConfigs.production
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            buildConfigField("String"."API_URL", rootProject. Ext apiUrl. PreRelease)} / / online the official release {resValue"string"."app_name", rootProject.ext.android.releaseAppName
            manifestPlaceholders = [app_icon: rootProject.ext.android.releaseAppIcon]
            zipAlignEnabled true
            shrinkResources true

            signingConfig signingConfigs.production
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            buildConfigField("String"."API_URL". RootProject. Ext apiUrl. Release)} / / modified apk package name applicationVariants. All {variant - > the variant. The outputs. All {output - > outputFileName ="app-${variant.name}-${defaultConfig.versionName}-${defaultConfig.versionCode}.apk"}} // JDK configuration compileOptions {sourceCompatibility JavUncomfortable.VERSION_1_8 targetCompatibility javUncomfortable.VERSION_1_8} //lib package configurationsourceSets {
        main {
            jniLibs.srcDirs = ['libs'}} // Multi-channel/multi-environment configuration flavorDimensions("default")
    productFlavors {
        qudao1 {
            dimension "default"// Customize the project name resValue"string"."app_name"."Channel 1"// Custom arguments call buildConfigField in BUildConfig ("String"."orgId".'" "')
        }
        qudao2 {
            dimension "default"
            resValue "string"."app_name"."Channel 2"
            buildConfigField("String"."orgId", rootProject. Ext. Channel. CsOrgId)}} / / execution lint check, have any error or warning, termination of the build, we can turn off it. lintOptions { abortOnErrorfalse
        checkReleaseBuilds false} // The file repeats packagingOptions {exclude'lib/arm64-v8a/ffmpeg.so'Variants -> variants. Outputs. Each {output -> variants -> outputs.if (variant.getBuildType().isMinifyEnabled()) {
                variant.assemble.doLast{
                        copy {
                            from variant.mappingFile
                            into "${projectDir}/mappings"
                            rename { String fileName ->
                                "mapping-${variant.name}.txt"}}}}}}} configurations {// Exclude the Commons module at compile time:'commons'// Exclude com.squareup.retrofit2: adapter-rxjava all*. Exclude group: com.squareup.retrofit2: Adapter-rxjava all*.'com.squareup.retrofit2', module: 'adapter-rxjava'Configurations. All {resolutionStrategy {force'com. Squareup. Retrofit2: adapter - rxjava: 2.1.0'} count count count count count count count count count'libs', include: ['*.jar'] // dynamic version number implementation'com.android.support.constraint:constraint-layout:+'
    implementation project(path: ':utilslib') / / repeat reference questions the compile (' com. Squareup. Retrofit2: adapter - rxjava: 2.1.0 ') {/ / conflict priority force = use the versiontrue// Exclude module based on build name:'rxjava'// Exclude group:'com.squareup.retrofit'// Exclude group: // exclude group:'com.squareup.retrofit', module: 'rxjava'// Disable dependency transitive = for this dependencyfalse}}Copy the code

Custom gradle

Need to reference apply from: “config.gradle” in the root project

ext {

    apiUrl = [

            releaseUrl   : '"http://www.baidu.com/"',
            preReleaseUrl: '"http://www.baidu.com/"',
            debugUrl     : '"http://www.baidu.com/"',
    ]
    appKey = [
            buglyAppId: '"4a8eea659f"'
    ]
    signing = [
            storeFile    : file("./app/jks/key.jks"),
            storePassword: "123456",
            keyAlias     : "news",
            keyPassword  : "123456"
    ]
    android = [
            applicationId    : "com.ll.ll",
            compileSdkVersion: 28,
            minSdkVersion    : 19,
            targetSdkVersion : 28,

            debugAppName     : "App Preview",
            releaseAppName   : "App Official",
            preReleaseAppName: "Pre-launch version",
            debugAppIcon     : "@mipmap/ic_launcher_round",
            releaseAppIcon   : "@mipmap/ic_launcher",
            preReleaseAppIcon: "@mipmap/ic_launcher",

            versionCode      : 1,
            versionName      : "1.0.0"
    ]


    libsVersion = [
            supportLibraryVersion = "28.0.0",
    ]


    dependencies = [
            //support
            appcompat_v7: "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion",
            design      : "com.android.support:design:$rootProject.supportLibraryVersion",
//            recyclerview_v7: "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion",
//            cardview_v7    : "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"]},Copy the code