Android studio项目中的gradle.properties文件作用

一、简介

在使用Android Studio新建Android项目之后,在项目根目录下会默认生成一个gradle.properties文件,该文件是项目级别的Gradle配置文件,我们可以在里面做一些Gradle文件的全局性的配置,也可以将比较私密的信息放在里面,防止泄露。

二、用法

1、在各个模块的build.gradle里面直接引用
gradle.properties里面定义的属性是全局的,可以在各个模块的build.gradle里面直接引用.

在gradle.properties文件中新增下面属性:

COMPILE_SDK_VERSION=28
MIN_SDK_VERSION=15
TARGET_SDK_VERSION=28
SUPPORT_APPCOMPAT_V7_VERSION=28.0.0

注意:在gradle.properties中定义的属性默认是String类型的,如果需要int类型,需要添加XXX as int后缀。

在app/build.gradle文件中引用:

android {
    compileSdkVersion COMPILE_SDK_VERSION as int
    defaultConfig {
        minSdkVersion MIN_SDK_VERSION as int
        targetSdkVersion TARGET_SDK_VERSION as int
        ...
    }
    ...
}

dependencies {
    ...
    implementation "com.android.support:appcompat-v7:${SUPPORT_APPCOMPAT_V7_VERSION}"
    ...
}

2、在Java代码或者xml布局文件中使用
在gradle.properties中添加配置:

# Java 代码使用
DEFAULT_NICK_NAME=maolegemi
DEFAULT_NUMBER=10086

# xml文件调用
USER_NAME=wangdandan
TEXT_SIZE=20sp
TEXT_COLOR=#ef5350

在app/build.gradle中读取属性,对这些属性进行转化:

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

        debug{
            // Java代码调用
            buildConfigField "String", "defaultNickName", "\"${DEFAULT_NICK_NAME}\""
            buildConfigField "Integer", "defaultNumber", DEFAULT_NUMBER

            // xml布局文件调用
            resValue "string", "user_name", "${USER_NAME}"
            resValue "dimen", "text_size", "${TEXT_SIZE}"
            resValue "color", "text_color", "${TEXT_COLOR}"
        }
    }
}

上述代码中的buildConfigField方法的是BuildType#buildConfigField,包含三个参数:类型、变量名、变量值。resValue方法定义在BuildType#resValue中,也包含三个参数:类型、变量名、变量值。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("defaultNickName:" + BuildConfig.defaultNickName);
        System.out.println("defaultNickName.type:" + BuildConfig.defaultNickName.getClass().getSimpleName());
        System.out.println("defaultNumber:" + BuildConfig.defaultNumber);
        System.out.println("defaultNumber.type:" + BuildConfig.defaultNumber.getClass().getSimpleName());
    }

在xml布局文件中调用:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:textSize="@dimen/text_size"
    android:textColor="@color/text_color"
    />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网小熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值