×
=0) { let js = text.slice(pos1, pos2); + '<\/' + "script" + '>'; arrText.push(js); // next pos1 = pos2; continue; } } } break; } return arrText; } function OpenDialog(parentDiv, urlContent) { parentDiv = document.getElementById('modal-background'); // new !!!!!!! parentDiv.appendChild(document.getElementById('modal-template')); document.getElementById('modal-background').style.display = "block"; document.getElementById('modal-template').style.display = "flex"; // !!!!! document.getElementById('modal-body').innerHTML = ""; post_url(urlContent, "", function(text_from_server) { var element = document.getElementById('modal-body'); element.innerHTML = text_from_server; // add scripts var arrJSText = get_scripts(text_from_server); for (var i=0; i
dir.by
Search
Programming, development, testing
→
Android, Google Play phone, tablet (writing an app, a game)
→
Kotlin application (for Android phone) in Android Studio
→
How to Make a Variable in a build.gradle File and Pass It to a Kotlin File | Android Studio | Kotlin
Looked at
3232
times
How to Make a Variable in a build.gradle File and Pass It to a Kotlin File | 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 get the values from my variables, write like this:
${Variable name}
val
text1 = BuildConfig.
MY_APPLICATION_LABEL
Step 1. Creating a new project
Create a new simple project "Empty Views Activity" on Kotlin in Android Studio ...
Step 2. Let's change the code in the file build.gradle
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 MainActivity.kt
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:
Note! Can I use my variables in the AndroidManifest.xml file?
In the
AndroidManifest.xml
file, to use the variable, you need to write like this:
${Variable name}
Let's take a look at the entire
AndroidManifest.xml
file:
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>
← Previous topic
How to Make a Variable in the build.gradle File and Pass It to the Manifest File AndroidManifest.xml | Android Studio | Kotlin
Next topic →
Moshi (converting json text to a class object) | deserialization in Kotlin | Android Studio
Your feedback ... Comments ...
Your Name
Your comment
(www links can only be added by a logged-in user)
+ Picture
Объявления
Объявления
•
What is the Android operating system? What version numbers were in Android
•
What are ARM processors? | Android
•
Что такое AndroidX?
Java application (for Android phone) in Android Studio
•
Why Android applications are written in Java?
•
Download and install Android Studio to write programs for Android phones in Java, Kotlin
•
Open Android Studio on the computer (Windows 10)
•
Create a new project on Java with one simple Activity in Android Studio (write a program for Android phones, tablets) in Windows 10
•
Compile and run the application in Android Studio on computer (Windows) in emulator mode Android Device
•
Running the application Android Studio in debugging on computer (Windows) in emulator mode Android Device
•
Error "error while waiting for device: the emulator process for ..... has terminated" when running an application in Android Studio on a computer (Windows) in emulator mode Android Device
•
Error "Error while waiting for device: Illegal char <*> at index 0: *.lock ..." when running the application in Android Studio
•
Error "AVD is already running ..." when running the application in Android Studio
•
Error "CMake "3.18.1" was not found in SDK, PATH, or by cmake.dir property" when compiling a project to Android Studio
•
Error "Execution failed for task ":app:compressDebugAssets" when compiling a project to Android Studio
•
All errors when starting the Android application
•
What is Android SDK (a copy of the Android operating system)? Installing Android SDK 8.1 in Android Studio ...
•
Create a Android virtual device in Android Studio
•
Install HAXM
•
Activity in Android
Kotlin application (for Android phone) in Android Studio
•
Download and install Android Studio to write programs for Android phones in Java, Kotlin
•
Create a new project "Empty Views Activity" on Kotlin in Android Studio (write a program for Android phones, tablets) in Windows 10
•
Compile and run Kotlin an application in Android Studio on a computer (Windows) in emulator mode Android Device
•
Run Kotlin an application in Android Studio in debugging on a computer (Windows) in emulator mode Android Device
•
Running and debugging Kotlin Android Studio app on my phone via USB
•
Long wait when running Kotlin Android application. I see the message: "I/zygote:Waiting for a blocking GC ProfileSaver"
•
Create Android project, add TextView and show the value on the phone | Android Telephone, Android Studio, Kotlin, Windows 10
•
Copy the data in the class and object. We use the attribute @Parcelize and the interface Parcelable. | Kotlin | Android Studio
•
Error "Class is not abstract and does not implement abstract member public abstract fun describeContents(): Int defined in android.os.Parcelable" | Kotlin | Android Studio | @Parcelize | Parcelable
•
Show a window with 2 buttons: yes, no and you don't need to add any resource, layout | I call the AlertDialog function in Kotlin | Android Studio
•
Android Kotlin application takes photo using camera and draws in the application | Android Studio | Kotlin
•
Android Kotlin choose photo from gallery and draws in the application | Android Studio | Kotlin ...
•
getExternalFilesDir - function that returns the full path to an external directory where our application can place persistent files | Android Studio, Kotlin
•
getFilesDir - function that returns the full path to directory where our application can place files | Android Studio, Kotlin
•
How do I work with files media in Android? What are content Uri and file path. The difference between getExternalFilesDir and getFilesDir ... | Android Studio, Kotlin
•
How to Make a Variable in the build.gradle File and Pass It to the Manifest File AndroidManifest.xml | Android Studio | Kotlin
•
How to Make a Variable in a build.gradle File and Pass It to a Kotlin File | Android Studio | Kotlin
•
Moshi (converting json text to a class object) | deserialization in Kotlin | Android Studio
•
Moshi (converting array json to list of object) | deserialization in Kotlin | Android Studio
•
Error "Failed to find the generated JsonAdapter class for class com.example.androidkotlinapp1.MyBook" | Exception | Kotlin | Moshi | Android Studio
•
Error "A problem occurred evaluating project ':app'. Caused by: CustomMessageMissingMethodException: Could not find method kapt() for arguments [com.squareup.moshi:moshi-kotlin-codegen:1.14.0]" | When compiling a Kotlin, Moshi project in Android Studio
Jetpack application (for Android phone) in Android Studio | Kotlin
•
What is Jetpack for Android?
•
Create a new project "Jetpack Compose" on Kotlin in Android Studio (write a program for Android phones, tablets) in Windows 10
•
Compilation error "Dependency 'androidx.core:core-ktx:1.17.0' requires Android Gradle plugin 8.9.1 or higher" | Jetpack | Kotlin | Android Studio
C++ game (for Android phone) in Android Studio | Android NDK, OpenGL ES
•
What is Android NDK for Android phone? This is the C++ library for the Android phone.
•
What is Android OpenGL ES for Android phone? This is the C++ graphics library for the Android phone.
•
Create a project "Android Native C++ Game" for the phone | Android Studio, Android NDK, OpenGL ES, C++
•
Drawing a lake by points for the 2D game
•
Drawing an enemy airplane as a vector graphic in Adobe Illustrator. I take the coordinates of the points from Adobe Illustrator and add them to my 2D game on C++ OpenGL
•
Compile and run "Android Native C++ Game" in Android Studio on a computer (Windows) in Android Device emulator mode
•
Error "[CXX1405] error when building with cmake using CMakeLists.txt: C build system [configure] failed while executing cmake.exe" when compiling Android Native C++ an application to Android Studio on computer (Windows)
•
Error "ninja: error: rebuilding 'build.ninja': subcommand failed" when compiling Android Native C++ an application to Android Studio on computer (Windows)
•
Draw a triangle with paint inside in "Android Native C++ Game" for phone | Android Studio, Android NDK, OpenGL ES v1, C++
•
Download bmp file from Assets and draw textures in "Android Native C++ Game" for your phone | Android Studio, Android NDK, OpenGL ES v2 (shader), C++
•
How to get bmp file located in Assets inside apk file ? | Android Studio, NDK, C++
•
How to use alpha transparency in displaying texture using OpenGL? | Android Studio, OpenGL ES, NDK, C++
•
Why does glTexImage2D return error code 1280? | Android Studio, OpenGL ES, NDK, C++
•
What are cpp and h files in C++? | Android Studio, NDK, C++
•
How to create new h file and add to project android NDK C++? | Android Studio, NDK, C++
•
How to create new cpp file and add to project android NDK C++? | Android Studio, NDK, C++, CMakeLists.txt
•
dynamic_cast in C++ (converting a pointer to a different type and checking validity in runtime) | Android Studio, NDK, C++
•
std::map<Key, Value> is a set of keys and values in C++. | Android Studio, NDK, C++
•
Pass a function as a parameter to a function (callback) | C++ | Android Studio, NDK, C++
•
How to find event when display rotated (changed orientation) in Android phone | Android Studio, NDK, C++
•
How to handle events in Android phone (create/terminate window event, set focus, lost focus, touch in phone) | Android Studio, NDK, C++
•
Create a signed apk file in Android Studio | Android NDK, OpenGL ES
Google Play Console (for developer)
•
Creating a Google Play account developer | Google Play Console
•
The developer in Google Play Console needs to verify the identity | Google Play Console
•
The developer in Google Play Console needs to confirm developer account | Google Play Console
•
Developer account is not in use | Developer account is at risk of being closed | Google Play Console
•
Compile app and send it for production in Google Play Console | My app in Google Play Console
•
Policy status "Invalid Privacy policy" | Provide link to a valid privacy policy page | My app in Google Play Console
•
Policy status "App must target Android 15 (API level 35) or higher" | Status "You won't be able to release app updates" | My app in Google Play Console
•
Policy status "App must target Android 14 (API level 34) or higher" | Status "You won't be able to release app updates" | My app in Google Play Console
•
Policy app content "Remove Photo and video permissions permission or submit declaration" | My app in Google Play Console
•
Create an application in Google Play Console (in the option I select that it will be a game) | My game in Google Play Console
•
Important parameters: package, applicationId, versionCode, versionName, android:label (in AndroidManifest.xml, build.gradle) to create a test version for Google Play Console | My game in Google Play Console
•
Create a signed .aab file in Android Studio | My game in Google Play Console
•
Compile the game and send it for internal testing in Google Play Console | My game in Google Play Console
•
Google automatically ran tests and made pictures, a report on how the game runs on different brands of phones | My game in Google Play Console
•
How do I ask Google Play Developer Console a support question?
•
Google Play Developer Console support feedback
•
Topics about Google Play Billing & in-app purchase | Google Play Console
•
Can I use (integrate) payments in my Google game if I am a developer from Belarus? | Monetization in Google Play Console
•
How can I change the Google Play Console language? | Google Chrome
•
How to change country in payments profile? | Google Play Console
•
How do I view (open) a payment page in Google Play? | Google Play Console
Ваши вопросы присылайте по почте:
info@dir.by