build.gradle 使用 gradle.properties 中文字符乱码问题

在 AndroidManifest.xml 文件中间可以通过 build.gradle 做中转,去获取 gradle.property 中的变量,主要是在使用 jenkins 自动构建的时候动态配置构建参数。

给出一个 gradle.property 文件

1
2
3
4

# Project-wide Gradle settings.

APP_NAME = 安卓应用
APP_ID = com.example

这里的字符串可以不用加引号。前面是字段名,后面的是字段对应的值。可以在 build.gradle 文件里直接使用这些字段的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

apply plugin: 'com.android.application'

def getAppName() {
    return new String(APP_NAME.getBytes("UTF-8"), "UTF-8")
}

ext {
    appVersionCode = 277
    appVersionName = 277
    appid = APP_ID
    appName = getAppName()
}

android {
    flavorDimensions "default"
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.xjj.cloud.ypjczd"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName appVersionName
        resValue("string", "SERVER_URL", XJJ_SN_SERVER_URL)
        resValue("string", "useVpn", XJJ_USE_VPN)
    }

    productFlavors {
        example {
            applicationId appid
            manifestPlaceholders = [APP_ICON: "@drawable/ic_launcher",
                APP_NAME: appName]
        }
    }

}

上面是在 app 目录底下的 builde.gradle 文件的部分内容。

在 ext 里面定义的变量 appid 和 appName 是可以在当前的 build.gradle 中直接使用,这两个变量后面的赋值 APP_ID 和 APP_NAME 是自动从 gradle.property 中读取。这里定义了一个 getAppName 是因为直接使用 APP_NAME 会导致乱码。所以这里转换了一下。另外记得去 setting - editor - File Encodings 里面把所有的编码都改为 UTF-8 的。如下图所示:

Setting.png

然后为了在 AndroidManifest.xml 文件里面使用,所以我们在 build.gradle 文件里面的 manifestPlaceholders 定义了 APP_ICON 和 APP_NAME。

1
2
3
4
5
6
7

    productFlavors {
        example {
            applicationId appid
            manifestPlaceholders = [APP_ICON: "@drawable/ic_launcher",
                APP_NAME: appName]
        }
    }

这样子,我们就可以在 AndroidManifest.xml 文件中通过 "${APP_ICON}" 这种方式来调用 build.gradle 中的变量。如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:installLocation="auto"
          package="com.xjj.demo">


    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="${APP_ICON}"
        android:label="${APP_NAME}"
        android:roundIcon="${APP_ICON}"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.example.MyActivity"
            android:alwaysRetainTaskState="true"
            android:hardwareAccelerated="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

所以 AndroidManifest.xml 就通过 build.gradle 文件来调用 gradle.property 中定义的变量了。

 

转自:关于 AndroidManifest.xml 通过 build.gradle 使用 gradle.properties 中文字符乱码问题 | 码农家园

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值