android studio 开发android 项目时gradle文件的配置说明

沈淇
2023-12-01

android studio 开发android 项目时gradle文件的配置说明

前言:在使用as开发过程中,总是需要在gradle中配置一堆东西,现在做一下总结,希望能帮到你!(技术水平有限,轻点喷)

话不多说,直接代码

//gradle代码是Groovy的语法所写的,所以要大概了解一点groovy的东西 
//获取当前时间(年月日)这两个东西在后面可能会用到
def releaseTime() {
    return new Date().format("yyyy_MM_dd", TimeZone.getTimeZone("UTC"))
}
//获取当前时间(时分秒)
def releaseTime2() {
    return new Date().format("HH_mm", TimeZone.getTimeZone("UTC"))
}
//rootProject.ext.android.compileSdkVersion 这个配置需要另写一份配置文件,稍后说明
android {
    compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion
    signingConfigs {
    //正式版本的配置文件
        release {
            keyAlias '你的keystore文件的别名'
            keyPassword '你的keystore文件的别名的密码'
            storeFile file('你的keystore文件的位置')
            storePassword '你的keystore文件的密码'
        }
        //测试版本的配置文件
        config {
            keyAlias '你的keystore文件的别名'
            keyPassword '你的keystore文件的别名的密码'
            storeFile file('你的keystore文件的位置')
            storePassword '你的keystore文件的密码'
        }
    }
    defaultConfig {
        applicationId "你的项目包名"
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //引用的外部依赖中有so文件需要配置
        ndk {
            //根据需要 自行选择添加的对应cpu类型的.so库。
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }
    }
    //在调试的时候,构建的apk文件类似于正式包,例如调用微信或者qq的第三方服务的时候需要正式包,测试的时候可以这样配置
    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable true
            signingConfig signingConfigs.release
            shrinkResources false
        }
        debug {
            signingConfig signingConfigs.release
        }
    }
    //打包时自动生成文件名
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                // 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
                def fileName = "你的项目名称_ver${defaultConfig.versionName}_${releaseTime()}.apk"
//                output.outputFile = new File(output.outputFile.parent, fileName)
//                outputFile = outputFile.name.replaceAll(".apk", fileName)
                outputFileName = fileName //Android studio 3.+ 版本需要这样设置
            }
        }
    }
    sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
    //配置这个可以启用Lambda表达式
       compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:25.4.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //高德地图
    implementation 'com.amap.api:location:latest.integration'
    compile 'com.android.support:multidex:1.0.1'
    //beacon
    compile 'org.altbeacon:android-beacon-library:2.9'
    这个配置需要在配置文件中写好 as3.+推荐使用implementation 引用
 //mid包
    implementation rootProject.ext.dependencies["midLib"]
}

配置文件

配置文件的名称命名必须以.gradle 为文件格式 例如:conf.gradle

根据需要配置

``` python
ext {

android = [
        compileSdkVersion: 25,
        buildToolsVersion: "26.0.2",

        minSdkVersion    : 15,
        targetSdkVersion : 23,

        versionCode      : 1,
        versionName      : '1.0.0',

        VSupportSdk      : '25.4.0',
        VRetrofitSdk     : "2.1.0",
        VOkhttp          : "3.4.2",
        VRxlifecycle     : "1.0",
        wangLib          : "2.1.3",
        circleDialog     : "2.4.0",
        xinge            : "3.2.2-release",
        wup              : "1.0.0.E-release",
        mid              : "4.0.6-release"

]
dependencies = [
        "appcompat-v7"               : "com.android.support:appcompat-v7:${android["VSupportSdk"]}",
        "support-v4"                 : "com.android.support:support-v4:${android["VSupportSdk"]}",
        "design"                     : "com.android.support:design:${android["VSupportSdk"]}",
        "annotations"                : "com.android.support:support-annotations:${android["VSupportSdk"]}",
        "recyclerview-v7"            : "com.android.support:recyclerview-v7:${android["VSupportSdk"]}",

        "butterknife"                : "com.jakewharton:butterknife:8.4.0",
        "butterknife-apt"            : "com.jakewharton:butterknife-compiler:8.4.0",
        "eventbus"                   : "org.greenrobot:eventbus:3.0.0",
        "glide"                      : "com.github.bumptech.glide:glide:3.7.0",
        "picasso"                    : "com.squareup.picasso:picasso:2.5.2",
        "xrecyclerview"              : "com.github.limedroid:ARecyclerView:v1.1.0",
        "avi-loading"                : "com.wang.avi:library:1.0.2",

        "gson"                       : "com.google.code.gson:gson:2.6.2",
        "rxandroid"                  : "io.reactivex:rxandroid:1.2.1",
        "rxjava"                     : "io.reactivex:rxjava:1.1.6",
        "retrofit"                   : "com.squareup.retrofit2:retrofit:${android["VRetrofitSdk"]}",
        "retrofit-converter-gson"    : "com.squareup.retrofit2:converter-gson:${android["VRetrofitSdk"]}",
        "retrofit-adapter-rxjava"    : "com.squareup.retrofit2:adapter-rxjava:${android["VRetrofitSdk"]}",
        "okhttp3-logging-interceptor": "com.squareup.okhttp3:logging-interceptor:${android["VOkhttp"]}",
        "okhttp3"                    : "com.squareup.okhttp3:okhttp:${android["VOkhttp"]}",
        "rxlifecycle"                : "com.trello:rxlifecycle:${android["VRxlifecycle"]}",
        "rxlifecycle-android"        : "com.trello:rxlifecycle-android:${android["VRxlifecycle"]}",
        "rxlifecycle-components"     : "com.trello:rxlifecycle-components:${android["VRxlifecycle"]}",
        "rxpermissions"              : "com.tbruyelle.rxpermissions:rxpermissions:0.9.1@aar",

        "canary-debug"               : "com.squareup.leakcanary:leakcanary-android:1.4-beta2",
        "canary-release"             : "com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2",
        "wangLib-library"            : "com.wang.avi:library:${android["wangLib"]}",
        "circleDialog"               : "com.mylhyl:circleDialog:${android["circleDialog"]}",

        "xingeLib"                      : "com.tencent.xinge:xinge:${android["xinge"]}",
        "wupLib"                        : "com.tencent.wup:wup:${android["wup"]}",
        "midLib"                        : "com.tencent.mid:mid:${android["mid"]}",

]

}
“`
*注意 写完配置文件之后,需要在项目的根目录下引入一下:

apply from: “conf.gradle”

如有错误请指正

 类似资料: