Gradle(一)

配置config.gradle

ext {

    isRelease = false

    androidId = [
            "compileSdkVersion": 29,
            "buildToolsVersion": "29.0.1",
            "minSdkVersion"    : 21,
            "targetSdkVersion" : 29,
            "versionCode"      : 1,
            "versionName"      : "1.0"
    ]

    url = [
            "debug"  : "htts://www.google.com",
            "release": "htts://www.facebook.com"
    ]

    appId = [
            "applicationId": "com.easy.combine_module"
    ]

    androidX = "1.1.0"

    dependencies = [

            "appcompat"       : "androidx.appcompat:appcompat:${androidX}",
            "constraintlayout": "androidx.constraintlayout:constraintlayout:1.1.3",
            "legacySupportV4"       : "androidx.legacy:legacy-support-v4:1.0.0",
            "viewpager"    : "androidx.viewpager:viewpager:1.0.0"
    ]


}

引入:apply from: "config.gradle"

build.gradle使用

apply plugin: 'com.android.application'

def androidId = rootProject.ext.androidId
def url = rootProject.ext.url
def support = rootProject.ext.dependencies

android {
    compileSdkVersion androidId.compileSdkVersion
    buildToolsVersion androidId.buildToolsVersion
    defaultConfig {
        applicationId androidId.applicationId
        minSdkVersion androidId.minSdkVersion
        targetSdkVersion androidId.targetSdkVersion
        versionCode androidId.versionCode
        versionName androidId.versionName


        // 开启分包
        multiDexEnabled true
        // 设置分包配置
        // multiDexKeepFile file('multidex-config.txt')

        // 将svg图片生成 指定维度的png图片
        // vectorDrawables.generatedDensities('xhdpi','xxhdpi')
        // 使用support-v7兼容(5.0版本以上)
        vectorDrawables.useSupportLibrary = true
        // 只保留指定和默认资源
        resConfigs('zh-rCN')

        // 配置so库CPU架构(真机:arm,模拟器:x86)
        // x86  x86_64  mips  mips64
        ndk {
            //abiFilters('armeabi', 'armeabi-v7a')
            // 为了模拟器启动
            abiFilters('x86', 'x86_64')
        }

        // 源集 - 设置源集的属性,更改源集的 Java 目录或者自由目录等
        sourceSets {
            main {
                if (!isRelease) {
                    // 如果是组件化模式,需要单独运行时
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                    java.srcDirs = ['src/main/java']
                    res.srcDirs = ['src/main/res']
                    resources.srcDirs = ['src/main/resources']
                    aidl.srcDirs = ['src/main/aidl']
                    assets.srcDirs = ['src/main/assets']
                } else {
                    // 集成化模式,整个项目打包
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                }
            }
        }
    }

    signingConfigs {
        debug {
            // 天坑:填错了,编译不通过还找不到问题
            storeFile file('/Users/wangtao/.android/debug.keystore')
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
        release {
            // 签名证书文件
            storeFile file('./module.key')
            // 签名证书的类型
            storeType "taoge666"
            // 签名证书文件的密码
            storePassword "taoge666"
            // 签名证书中密钥别名
            keyAlias "taoge"
            // 签名证书中该密钥的密码
            keyPassword "taoge123"
            // 是否开启V2打包
            v2SigningEnabled true
        }
    }

    buildTypes {

        debug {
            buildConfigField("String", "debug", "\"${url.debug}\"")
        }
        release {
            minifyEnabled false
            buildConfigField("release", "release", "\"${url.release}\"")
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    support.each { k, v -> implementation v }
}

你可能感兴趣的:(Gradle(一))