前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数

【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数

作者头像
韩曙亮
发布2023-03-30 13:09:31
1.7K0
发布2023-03-30 13:09:31
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

Android Plugin DSL Reference 参考文档 :

一、gradle.properties 中配置编译参数


gradle.properties 中配置编译参数 , 注意等号两边不要有空格 ;

代码语言:javascript
复制
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true

# 配置是否在 Google Play 上架
isGooglePlay=true
# 配置当前的应用市场
market=GooglePlay

二、在 build.gradle 中配置 BuildConfig.java 生成信息


这里调用

代码语言:javascript
复制
void buildConfigField(String type, String name, String value)

方法 , 向 生成的 BuildConfig 类中添加新的字段 , 生成的字段样式为

代码语言:javascript
复制
<type> <name> = <value>;

这里需要注意 , 上述

3

个字符串原封不动的替换 ,

如果是字符串 , 需要使用如下样式声明 , 字符串外部的双引号 , 也需要手动使用转移字符串生成 ;

代码语言:javascript
复制
buildConfigField("String", "market", "\"${market}\"")

声明 BuildConfig 字段代码 :

代码语言:javascript
复制
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.classloader_demo"
        minSdkVersion 18
        targetSdkVersion 30

        // 应用是否在 Google Play 上架
        buildConfigField("boolean", "isGooglePlay", isGooglePlay)
        // 当前的应用市场
        buildConfigField("String", "market", "\"${market}\"")
    }
}

参考文档 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html

三、编译后生成的 BuildConfig 类


选择 " 菜单栏 / Build / Make Project " 选项 编译整个工程 , 或者使用 Ctrl + F9 快捷键 ;

在这里插入图片描述
在这里插入图片描述

编译完成后生成的 BuildConfig 类 :

代码语言:javascript
复制
package com.example.classloader_demo;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.classloader_demo";
  public static final String BUILD_TYPE = "debug";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Field from default config.
  public static final boolean isGooglePlay = true;
  // Field from default config.
  public static final String market = "GooglePlay";
}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、gradle.properties 中配置编译参数
  • 二、在 build.gradle 中配置 BuildConfig.java 生成信息
  • 三、编译后生成的 BuildConfig 类
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档