BuildConfig

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.test.hellokotlin"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            //打包后在versionName后面加上后缀,同时加上版本号
            versionNameSuffix "-release" + android.defaultConfig.versionCode
            applicationIdSuffix ".release"
            //是否开启混淆
            minifyEnabled false
            //混淆文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //signingConfig signingConfigs.release 签名文件
            buildConfigField("String", "BASE_URL", '"http://www.baidu.com/"')
            buildConfigField("String", "OTHER_URL", "\"http://dev.other.com/\"")
            buildConfigField "String", "APP_NAME", '"你好,Kotlin"'
            buildConfigField("boolean", "LOG_DEBUG", "false")
        }
        debug {
            versionNameSuffix "-debug" + android.defaultConfig.versionCode
            applicationIdSuffix ".debug"
            //buildConfigField 配置格式为: "type","name","value" 形式,
            //如果参数是String类型,"value"外部需加单引号申明内部是String类型的,或者使用转义符\
            buildConfigField("String", "BASE_URL", '"http://dev.baidu.com/"')
            buildConfigField("String", "OTHER_URL", "\"http://dev.other.com/\"")
            buildConfigField "String", "APP_NAME", '"hello,Kotlin"'
            buildConfigField("boolean", "LOG_DEBUG", "true")
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

在Studio中生成的目录在app/build/source/BuildConfig/Build Varients/package name/BuildConfig.java 文件下

debug

/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.ningbinchong.hellokotlin;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.test.hellokotlin.debug";
  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-debug1";
  // Fields from build type: debug
  public static final String APP_NAME = "hello,Kotlin";
  public static final String BASE_URL = "http://dev.baidu.com/";
  public static final boolean LOG_DEBUG = true;
  public static final String OTHER_URL = "http://dev.other.com/";
}

release

/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.ningbinchong.hellokotlin;

public final class BuildConfig {
  public static final boolean DEBUG = false;
  public static final String APPLICATION_ID = "com.test.hellokotlin.debug";
  public static final String BUILD_TYPE = "release";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0-release1";
  // Fields from build type: release
  public static final String APP_NAME = "你好,Kotlin";
  public static final String BASE_URL = "http://www.baidu.com/";
  public static final boolean LOG_DEBUG = false;
  public static final String OTHER_URL = "http://dev.other.com/";
}

在代码中使用

Log.i(TAG,BuildConfig.BASE_URL);
点赞