Gradle Android基本应用

注意:

Gradle 单引号/双引号

单引号引用一串字符串的时候这个字符串只是单纯的字符串,但是如果使用双引号引用,在字符串里面还支持插值操作,

def name = 'Andy'
def greeting = "Hello, $name!"

Gradle 单引号/双引号

使用Aliyun Maven仓库

方法一(只对当前工程有效)
allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        jcenter()
    }
}
方法二(修改电脑gradle默认地址,对所有工程生效)

gradle更换仓库 解决下载速度慢问题

多模块统一依赖

1.新建config.gradle

在项目根目录或者子项目中新建config.gradle文件,目录不同,引入的时候路径要注意

ext {
    supportLibraryVersion = '27.1.1'
    butterknifeVersion = '8.8.1'
    retrofitVersion = '2.4.0'
    glideVersion = '4.8.0'

    android = [compileSdkVersion: 27,
               minSdkVersion    : 14,
               targetSdkVersion : 26,
               versionCode      : 1,
               versionName      : "1.0"]

    javaTestDependencies = [
            junit: 'junit:junit:4.12'
    ]

    androidTestDependencies = [
            espresso: 'com.android.support.test.espresso:espresso-core:3.0.1',
            runner  : 'com.android.support.test:runner:1.0.1',
    ]

    annotationProcessorDependencies = [
            butterknifeCompiler: "com.jakewharton:butterknife-compiler:${butterknifeVersion}",
            glideCompiler      : "com.github.bumptech.glide:compiler:${glideVersion}",
    ]

//    depenVersion=[]
    supportDependencies = [supportV4               : "com.android.support:support-v4:${supportLibraryVersion}",
                           appcompatV7             : "com.android.support:appcompat-v7:${supportLibraryVersion}",
                           design                  : "com.android.support:design:${supportLibraryVersion}",
                           recyclerview            : "com.android.support:recyclerview-v7:${supportLibraryVersion}",
                           cardviewV7              : "com.android.support:cardview-v7:${supportLibraryVersion}",
                           constraintLayout        : 'com.android.support.constraint:constraint-layout:1.1.3',
                           MPAndroidChart          : 'com.github.PhilJay:MPAndroidChart:v3.0.3',
                           butterknife             : "com.jakewharton:butterknife:${butterknifeVersion}",
                           okhttp                  : 'com.squareup.okhttp3:okhttp:3.9.0',
                           gson                    : 'com.google.code.gson:gson:2.8.0',
                           retrofit                : "com.squareup.retrofit2:retrofit:${retrofitVersion}",
                           retrofitConverterGson   : "com.squareup.retrofit2:converter-gson:${retrofitVersion}",
                           retrofitConverterScalars: "com.squareup.retrofit2:converter-scalars:${retrofitVersion}",
                           retrofitConverterRxJava : "com.squareup.retrofit2:adapter-rxjava2:${retrofitVersion}",
                           glide                   : "com.github.bumptech.glide:glide:${glideVersion}",
                           glideOkhttp3Integration : "com.github.bumptech.glide:okhttp3-integration:${glideVersion}",

    ]
}

2.引入其他gradle配置

在主工程的build.gradle,引入自定义的config.gradle文件

相对路径从项目根目录开始
1.
apply from: "config.gradle" //自定义Gradle 引用库版本全局配置
2.
apply from: "keelibrary/configkee.gradle"
引入其他路径下的gradle
3.
apply from: "D:\\WorkSpace\\Android\\AndroidKeeMaster\\config.gradle"

3.使用

    implementation rootProject.ext.supportDependencies.supportV4
    implementation supportDependencies.constraintLayout
    implementation rootProject.ext.supportDependencies["MPAndroidChart"]
     annotationProcessor rootProject.ext.annotationProcessorDependencies.butterknifeCompiler

Gradle使用小技巧 各类库版本号同步

签名配置

签名放到根目录下

  signingConfigs {
        config {
            keyAlias 'Diabesen'
            keyPassword 'Diabesen@123'
            storeFile file('../Diabesen.jks')
            storePassword 'Diabesen@123'
        }

多工程引用同一个moudle

androidStudio 多项目依赖同一个module

引用工程下的若干个moudle,例如推送工程下的**推送和–推送。
Android 中不同项目共用通用库Module方法

打包配置APK名称

// 显示的时间格式,形如20171110
def releaseTime() {
    return new Date().format("yyyyMMdd", TimeZone.getDefault());
}

android节点下

     // AppName_1.0_20180930_release.apk
    applicationVariants.all { variant ->
        variant.outputs.all { output ->// each 改为 all
            def fileName = "AppName_${variant.versionName}_${releaseTime()}_"
            def outFile = output.outputFile
            if (outFile != null && outFile.name.endsWith('.apk')) {
                if (variant.buildType.name == ('release')) {
                    fileName = fileName + "_release.apk"
                } else if (variant.buildType.name == ('debug')) {
                    fileName = fileName + "_debug.apk"
                } else {
                    fileName = fileName + "_unknow.apk"
                }
                outputFileName = fileName  //  output.outputFile 改为 outputFileName
            }
        }
    }

你可能感兴趣的:(Android)