Android Gradle语法

打印字符串:

println("hello gradle")
println "hello gradle"

 

在项目根目录下,创建一个文件config.gradle;

ext{
    username="zp"
}

在项目级下的build.gradle中的头部引入:

apply from:"config.gradle"
​
buildscript {
  ...
}
...

然后就可以在所有module中的build.gradle中使用:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}
​
println("这是一个gradle打印")
println "这是一个gradle打印"
println "${rootProject.ext.username}"
println "${username}"//两者等同
//赋值与引用
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies
​
android {
compileSdk 30
    
    defaultConfig {
        applicationId appId.applicationId
        minSdk androidId.minSdk
        targetSdk androidId.targetSdk
        versionCode androidId.versionCode
        versionName androidId.versionName
​
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
  ...
}
...

以来的引入方式使用的一直都是简写:

implementation 'androidx.appcompat:appcompat:1.2.0'//简写,推荐使用简写方式
implementation group:'androidx.appcompat',name:'appcompat',version:'1.2.0'//标准写法
​
//使用引入的方式:
implementation support.appcompat
implementation support.material
​
//最简洁的方式:
support.each{k,v -> implementation v}
ext {

    isRelease = true
    //建立map存储,对象名,key都可以自定义,groovy糖果语法,非常灵活
    androidId = [
            minSdk     : 21,
            targetSdk  : 30,
            versionCode: 1,
            versionName: "1.0"
    ]
    appId=
    [
            applicationId: "com.xxxx.xxxx",
            zh : "",
            th : ""
    ]
    url = [
            debug  : "http://www.baidu.com/",
            release: "http://www.hao23.com/"
    ]
    supportLibiray = "1.2.0"
    //第三方库
    dependencies  = [
            "appcompat":"androidx.appcompat:appcompat:${supportLibiray}",
            "material":"com.google.android.material:material:1.3.0",
    ]
}

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

println("这是一个gradle打印")
println "这是一个gradle打印"
println "${rootProject.ext.androidId}"
//赋值与引用
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies
def url = rootProject.ext.url
def isRelease = rootProject.ext.isRelease

android {
    compileSdk 30

    defaultConfig {
        applicationId appId.applicationId
        minSdk androidId.minSdk
        targetSdk androidId.targetSdk
        versionCode androidId.versionCode
        versionName androidId.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

        //将svg图片生成 指定纬度的png图片
        //vectorDrawable.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/Androidmainfest.xml'
                    java.srcDirs = ['src/main/java']
                    res.srcDirs = ['src/main/resources']
                    aidl.srcDirs = ['src/main/aidl']
                    assets.srcDirs = ['src/main/assets']
                }else{
                    //如果是组件化模式,需要单独运行时
                    manifest.srcFile 'src/main/AndroidManifext.xml'
                }
            }
        }
    }

    //签名配置(隐形坑:必须写在buildTypes之前)
    signingConfigs{
        debug{
            //天坑:填错了编译不通过,而且找不到问题
            //固定位置
            storeFile file("/Users/yogurt/.android/debug.keystore")
            //debug的key
            storePassword "androiddebug"
            keyAlias "androiddebug"
            keyPassword "android"
        }
        release{
            //签名证书文件
            storeFile file('/Users/xxxx/demo/xxxx/xxx.jks')
            //签名证书的类型
            storeType "xxxxx"
            //签名证书文件的密码
            storePassword "xxxxx"
            //签名证书中的密钥别名
            keyAlias "xxxxx"
            //签名证书中该密钥的密码
            keyPassword "xxxxx"
            //是否开启v2打包
            v2SigningEnabled true
        }
    }


    buildTypes {
        debug{
            //对构建类型设置签名信息
            signingConfig signingConfigs.debug
            buildConfigField("String","debug","\"${url.debug}\"")
        }
        release {
            minifyEnabled false
            //对构建类型设置签名信息
            signingConfig signingConfigs.release
            buildConfigField("String","debug","\"${url.release}\"")
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    //AdbOptions 可以对 adb 操作选项添加配置
    adbOptions{
        //配置擦欧哦超时时间,单位毫秒
        timeOutInMs = 5 * 1000_0
        //adb install 命令的选项配置
        installOptions '-r','-s'
    }
    //对 dx 操作的配置,接受一个DexOptions 类型的闭包,配置由 DexOptions 提供
    dexOptions {
        //配置执行 dx 命令是为了为其分配的最大堆内存
        javaMaxHeapSize "4g"
        //配置是否预执行 dex Libraries 工程,开启后回提高增量构建速度,不过会影响 clean 构建的速度,默认 true
        preDexLibraries = false
        //配置是否开启 jumbo 模式,代码方法是会超过65535 需要强制开启才能构建成功
        jumboMode true
        //配置Gradle运行 dx 命令时使用的线程数量
        threadCount 8
        //配置multidex参数
        additionalParameters = [
                '--multi-dex',//多dex分包
                '--set-max-idx-number-50000',//每个包内方法数量上限
                //'--main-dex-list=' + '/multidex-config.txt',//打包到主classes.dex的文件列表
                '--miniman-main-dex'
        ]
    }
    //执行 gradle lint 命令即可运行 lint 检查,默认生成的报告在 outputs/lint-results.html 中
    lintOptions{
        //遇到 lint 检查错误会终止构建,一般设置为false
        abortOnError false
        //将警告当作错误来处理(老版本:warningAsErrors)
        warningsAsErrors false
        //检查新的API
        check 'NewApi'
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.3.2'

//    implementation support.appcompat
//    implementation support.material

    //最简洁的方式:
    support.each{k,v -> implementation v}

//    implementation group:'androidx.appcompat',name:'appcompat',version:'1.2.0'
//    implementation 'androidx.appcompat:appcompat:1.2.0'
//    implementation 'com.google.android.material:material:1.3.0'


    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值