How to Make a Variable in the build.gradle File and Pass It to the Manifest File AndroidManifest.xml | Android Studio | Kotlin
last updated: 7 January 2024
In the build.gradle file, I wrote the funcCreateVariable function that creates a variable:
File build.gradle
...
android {
defaultConfig {
// my function for simple create variable
def funcCreateVariable = {constantName, constantValue ->
manifestPlaceholders += [ (constantName):constantValue]
buildConfigField "String" , "${constantName}" , "\" ${constantValue}\""
}
// my variables
funcCreateVariable ("MY_APPLICATION_LABEL" , "Hello" )
funcCreateVariable ("MY_APPLICATION_ICON" , "@mipmap/ic_launcher" )
}
}
MY_APPLICATION_LABEL is my first variable in the build.gradle file
MY_APPLICATION_ICON is my second variable in the build.gradle file
In the AndroidManifest.xml file, to use the variable, you need to write like this: ${Variable name}
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application
...
android:icon="${MY_APPLICATION_ICON} "
android:label="${MY_APPLICATION_LABEL} "
...
</application>
</manifest>
Step 1. Creating a new project
Step 2. Let's change the code in the file build.gradle
The color indicates that a new code has been added.
File build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.androidkotlinapp1'
compileSdk 33
defaultConfig {
applicationId "com.example.androidkotlinapp1"
minSdk 27
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// my function for simple create variable
def funcCreateVariable = {constantName, constantValue ->
manifestPlaceholders += [ (constantName):constantValue]
buildConfigField "String" , "${constantName}" , "\" ${constantValue}\""
}
// my variables
funcCreateVariable ("MY_APPLICATION_LABEL" , "Hello" )
funcCreateVariable ("MY_APPLICATION_ICON" , "@mipmap/ic_launcher" )
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt' ), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
Step 3. Let's change the code in the file AndroidManifest.xml
The color indicates that a new code has been added.
File AndroidManifest.xml
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="${MY_APPLICATION_ICON}"
android:label="${MY_APPLICATION_LABEL}"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidKotlinApp1"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Note 1 How to Pass the System Variable build.gradle of a File to the AndroidManifest.xml File
There is a system variable applicationId in the file build.gradle
File build.gradle
android {
namespace 'com.example.androidkotlinapp1'
compileSdk 33
defaultConfig {
applicationId "com.example.androidkotlinapp1"
minSdk 27
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}
dependencies {
...
}
I want to use applicationId in the AndroidManifest.xml file
Answer:
In the AndroidManifest.xml file, to use the variable, you need to write like this: ${Variable name}
Example:
File AndroidManifest.xml
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidKotlinApp1"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider "
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
Note 2 Can I use my variables in the MainActivity.kt file?
To use MY_APPLICATION_LABEL and MY_APPLICATION_ICON variables in the MainActivity.kt file, you just need to write:
val text1 = BuildConfig.MY_APPLICATION_LABEL
Let's take a look at the entire MainActivity.kt file:
The color indicates that a new code has been added.
File MainActivity.kt
package com.example.androidkotlinapp1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate (savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val text1 = BuildConfig.MY_APPLICATION_LABEL
// text1 = "Hello"
}
}
Explanation:
The BuildConfig class is created itself when the project is compiled and is located in the build folder: