config.gradle用于全局定义项目的配置信息

在project下创建config.gradle文件:
作用:
1.统一性:每个modle用同一的版本等
2.便于管理:修改一次就能让每个modle都修改了
3.版本管理
步骤:
1,在project右键new,点击file,创建config.gradle文件;

ext{
    //android配置
    android=[ compileSdkVersion:28,
              applicationId :"com.example.meet",
              minSdkVersion :16,
              targetSdkVersion: 28,
              versionCode :1,
              versionName :"1.0",

              testInstrumentationRunner :"androidx.test.runner.AndroidJUnitRunner"

    ]
    dependencies =[

        "appcompat":'androidx.appcompat:appcompat:1.0.2'

    ]


2,在project下的build.gadle引入config.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
//引入自定义gradle配置文件
apply from:"config.gradle"
buildscript {
    
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

3,引用gradle常量:将app下的buile.gradle中的android和dependencies属性用config.gradle中的替代;

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]


    defaultConfig {
        applicationId rootProject.ext.android["applicationId"]
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]

        testInstrumentationRunner rootProject.ext.android["testInstrumentationRunner"]
    }

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

}

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

    implementation rootProject.ext.dependencies["appcompat"]
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

4,自定义gradle常量:
在app的build.gradle下用buildConfigField()方法添加,第一个参数为类型,第二参数为名字,第三个参数为值,这里添加了一个名叫LOG_DEBUG,值为true的布尔型常量。


    buildTypes {
        debug{
            buildConfigField("boolean","LOG_DEBUG","true")
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

5,查看常量:在MainActivity中写上BuildConfig,ctrl+B查看源码: Fields //from build type: debug下的就是自定义gradle常量Log_DEBUG;
(注意:如果在写上BuildConfig提示没有该类,选中找不到BuildConfig类的module ,Build ——> Make Module“module name”,生成BuildConfig类,系统自动补充import语句)

package com.example.meet;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.meet";
  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 boolean LOG_DEBUG = true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值