Android架构师-组件化-Gradle语法(全局gradle配置)1

组件化共分为8节课程

组件化第一节:Gradle语法

环境:

什么是Gradle:是一种编程思想

代码+详解样式:

第一行代码:

注意查看打印;

Build-Toggle view

第一种打印字符串的方式:

println(“hello gradle”)

第二种打印字符串的方式

println "hello studio"

apply plugin: 'com.android.application'

println("hello gradle")
println "hello gradle"
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.mygradle"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

我们run 的时候可以看到正常打印

二:新建一个Android  Library

 我们新建的library ,我们让app和library不建立任何联系,

1:我们发现两个gradle的版本号和引用有部分重叠 ,所以我们如何统一一下呢?

2:我们发布版本的url分为debug和release版本部署不同的url  我们也来配置一下

1:在项目的根目录 新建一个File  :config.gradle

//添加多个自定义属性 可以通过ext代码块
ext{

    username = "simon"
}

2 我们在根目录的build.gradle中


//根目录的build.fradle头布局加入自定义config.gradle 相当于在layout布局中加入include
apply from: "config.gradle"

3 然后我们在app的build.gradle中

println "$username"

4 然后我们run  查看打印,app和library都能正常打印

gradle是弱类型语言:

我们刚才的调用方法是简写 ,我们还可以这样调用

println "$rootProject.ext.username"

 一样可以正常打印

除了上面的  写法 我们还能如何证明他是弱类型的语言的呢

rootProject.ext.username = 163
println "$rootProject.ext.username"

我们在app的build.gradle打印之前 进行一个类型转换,之前我们定义的username是String 类型的  现在我们定义成了int类型

再次运行,不但修改成功 ,还将librayr中的数据也修改了

运行正常 ,所以我们可以在config中加入gradle公用的配置 

 

//添加多个自定义属性 可以通过ext代码块
ext{

    username = "simon"
    //生产开发环境:正式/测试
    isRelease = true

    //简历Map存储  对象名  key都可以自定义  groovy 糖果语法,非常灵活
    androidId = [//注意数数组形式o
        compileSdkVersion: 29,
        buildToolsVersion: "29.0.3",
        applicationId: "com.example.mygradle",
        minSdkVersion: 16,
        targetSdkVersion: 29,
        versionCode: 1,
        versionName: "1.0"
    ]

    appId = [//如果还有其他的applicationId 那么就配置其他更多的
          app  : "com.example.mygradle",//这个是app build.gradle 的  applicationId "com.example.mygradle"
          library  : "com.example.mygradle"
    ]

    //生产开发环境URL
    url = [
            "debug":"http://www.baidu.com/debug",
            "release":"http://www.baidu.com/release"
    ]

    //第三方依赖库
    supportLibrary = "28.0.0" //我们把他当做变量来用 $(xxx) --因为适配了androidx 所以不能采用此方式了

    dependencies =[
        appcompat :  'androidx.appcompat:appcompat:1.1.0',
        material :'com.google.android.material:material:1.1.0',
        constraintlayout:'androidx.constraintlayout:constraintlayout:1.1.3',
        navigationfragment:'androidx.navigation:navigation-fragment:2.2.1',
        navigationui:'androidx.navigation:navigation-ui:2.2.1'
    ]


}

那么我们如何在其他module中使用这个属性呢?

我们先改app的build.gradle

apply plugin: 'com.android.application'

println("hello gradle")

println "$username"
rootProject.ext.username = 163
println "$rootProject.ext.username"

//赋值与引用
def androidId = rootProject.ext.androidId;
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies

android {
    compileSdkVersion androidId.compileSdkVersion
    buildToolsVersion androidId.buildToolsVersion

    defaultConfig {
        applicationId appId.app
        minSdkVersion androidId.minSdkVersion
        targetSdkVersion androidId.targetSdkVersion
        versionCode androidId.versionCode
        versionName androidId.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

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

    implementation support.appcompat
    implementation support.material
    implementation support.constraintlayout
    implementation support.navigationfragment
    implementation support.navigationui
}

看上去简洁很多啊

apply plugin: 'com.android.application'

println("hello gradle")

println "$username"
rootProject.ext.username = 163
println "$rootProject.ext.username"

//赋值与引用
def androidId = rootProject.ext.androidId;
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies

android {
    compileSdkVersion androidId.compileSdkVersion
    buildToolsVersion androidId.buildToolsVersion

    defaultConfig {
        applicationId appId.app
        minSdkVersion androidId.minSdkVersion
        targetSdkVersion androidId.targetSdkVersion
        versionCode androidId.versionCode
        versionName androidId.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

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

/*    implementation support.appcompat
    implementation support.material
    implementation support.constraintlayout
    implementation support.navigationfragment
    implementation support.navigationui*/

    //我们还有更加简洁的方式
    support.each{k,v ->implementation v}


}

同样 我们在library中也可以进行同样的操作 ,不过需要注意的是 library里面是没有appid的(可以自行观察下)

此时我们再去配置URL

开始之前,我们需要先了解一个类:BuildConfig

点进去,他是存在于apk中的

那么我们如何在BuildConfig中添加一个public static final 的属性呢?这样我就可以全局使用了

需要我们在app的build.gradle中的buildTypes中添加

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

我们build一下,在BuildConfig中可以查看

package com.example.mygradle;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.mygradle";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Fields from build type: debug
  public static final String debug = "http://www.baidu.com/debug";
}

一般默认是什么环境 编译出来就是什么环境。

基本的语法就是这样 ,我们现在开始拓展一些项目中使用的东西

    defaultConfig {
        applicationId appId.app
        minSdkVersion androidId.minSdkVersion
        targetSdkVersion androidId.targetSdkVersion
        versionCode androidId.versionCode
        versionName androidId.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

我们开可以针对签名打包做一些是事情

    signingConfigs{
        debug{
            //天坑 填错了  编译不通过还找不到问题
            storeFile("C://Users/.......")
            storePassword "android" //"debug"
            keyAliss "androiddebugkey"//"debug"
            keyPassword "android"// ""
        }
        release{
            //签名证书文件
            storeFile("C://Users/.......")
            storeType "netease"
            storePassword "net163"
            keyAliss "net163"
            keyPassword "net163"
            v2SigningEnabled true
        }
    }

正常我们写 都是这样的  那我们如何对上述信息进行引用呢?

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

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值