I’m mainly interested in Mobile Application Development, Kotlin, Flutter, Java, Android Library, Book. For random bits follow me on Twitter/Instagram/Medium/Goodreads : @fevziomurtekin. For more info about my professional career, visit my Linkedin profile
© 2023. All rights reserved.
Hello everyone, In my previous article, I said that“I want to start new article series about Gradle”. And this article will be the second article of that series. In this article, I’ll explain the new feature a.k.aversion catalog which is developed by Gradle and providing to declare a conventional file.
Firstly, let’s start with what is TOML?
According to its website,TOML aims to be a minimal configuration file format that’s easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.So thanks to its feature which is mapped to a hash table make it is easier to match it with a Gradle script.
Also, you can see thislink for more detail.
So what should we know while developing with TOML?
The TOML file consists of 4 major sections
So, what to consider before implementing this feature? Let’s see about it now.
7.4.2
enableFeaturePreview("VERSION_CATALOGS")
insettings.gradlelibs.versions.toml
file inside yourgradle folderIf you have followed these points, we can move on to the step-by-step use of this feature.
1) First, open thelibs.versions.toml
and then add the version of each library we use.
[versions]kotlin="1.7.20"mockito="4.3.1"gradlePlugins="7.2.0"# it is mentioned that it should be at least 7.2.0
2) Then, it included these libraries which are declared version
[libraries]kotlinBom={module="org.jetbrains.kotlin:kotlin-bom",version.ref="kotlin"}mockitoCore={module="org.mockito:mockito-core",version.ref="mockito"}mockitoInline={module="org.mockito:mockito-inline",version.ref="mockito"}mockitoJupiter={module="org.mockito:mockito-junit-jupiter",version.ref="mockito"}
3) Now as you can see from the examples above, We have three libraries that we can bundle.
[bundles]mockito=["mockitoCore","mockitoInline","mockitoJupiter"]
4) It is declared the plugins that we use, under to plugins sections.
[plugins]android={id="com.android.application",version.ref="gradlePlugins-agp"}kotlinAndroid={id="org.jetbrains.kotlin.android",version.ref="kotlin"}kapt={id="org.jetbrains.kotlin.kapt",version.ref="kotlin"}
Let’s look at the final form of ourlibs.versions.toml
file.
[versions]kotlin="1.7.20"mockito="4.3.1"gradlePlugins="7.2.0"# it is mentioned that it should be at least 7.2.0[libraries]kotlinBom={module="org.jetbrains.kotlin:kotlin-bom",version.ref="kotlin"}mockitoCore={module="org.mockito:mockito-core",version.ref="mockito"}mockitoInline={module="org.mockito:mockito-inline",version.ref="mockito"}mockitoJupiter={module="org.mockito:mockito-junit-jupiter",version.ref="mockito"}[bundles]mockito=["mockitoCore","mockitoInline","mockitoJupiter"][plugins]android={id="com.android.application",version.ref="gradlePlugins"}kotlinAndroid={id="org.jetbrains.kotlin.android",version.ref="kotlin"}kapt={id="org.jetbrains.kotlin.kapt",version.ref="kotlin"}
6) As you can see, we have declared libraries that we’ll use. So, let’s see how we’ll use them in Gradle files.
plugins{claspath(libs.plugins.android)claspath(libs.plugins.kotlinAndroid)claspath(libs.plugins.kapt)}
implementation(libs.bundles.mockito)
In conclusion, we’ve talked about TOML language, how to use TOML with Gradle and how to create and uselibs.versions.toml
.I hope this blog post has been helpful to you. See you in the next posts.
Hi everyone,
After a long time, I want to start new article series about Gradle. In this article series, I will try to talk about Gradle tips, new Gradle technologies and Gradle optimizations. Before talking about these, let’s talk, about what is the matter of Gradle for the Android development world, how to use it, and why we need to use Gradle.
if you’re ready, let’s start!
Gradle is an open-source build automation tool focused on flexibility and performance. To use the Gradle build script, we writeGroovy orKotlin DSL.
To write a few reasons why we should use Gradle and a few advantages:
High performance, give high performance due to avoiding unnecessary work. Because only runs that what need to do work. And Gradle uses various caches to reuse outputs from previous builds.
JVM foundation, Gradle runs on the JVM. This is a bonus for users familiar with Java, since build logic can use the standard Java APIs.
Convention
Extensibility, most builds have special requirements that require custom build logic. For example, they add many new build concepts such as flavors and build types.
IDE support, Gradle is supported by different IDE. Like Android Studio, IntelliJ IDEA, Eclipse, VSCode, and NetBean.
Insight, with Build Scan, provides extensive information about a build that you can use to identify issues.
You can reachthis if you want more details about Gradle.
So let’s see the basic usage of Gradle both groovy and Kotlin DSL in Android.
The Android build system compiles app resources and source code and packages them into APKs or Android App Bundles that you can test, deploy, sign, and distribute.
At that point, Android studio is based on Gradle and uses it. Gradle provides several features that are specific to building Android apps.
As an advanced build toolkit, automate and manage the build process while letting you define flexible, custom build configurations. Each build configuration can define its own set of code and resources while reusing the parts common to all versions of your app. The Android Gradle plugin works with the build toolkit to provide processes and configurable settings that are specific to building and testing Android apps.
With Gradle, you can use and manage tobuild types,product flavors,build variants,manifest entries,add/remove dependencies,signing,code and resource shrinking andmultiple APK support.
We’ll look at these regulations in more detail in the next articles. However, right now let’s review mainly Gradle files.
When an Android project is created once, this project there will be three different Gradle files and two different properties files. These Gradle files aresettings.gradle,build.gradle (in the top-level),build.gradle, and these Gradle properties files aregradle.properties andlocal.properties
Here let’s see how to use it.
pluginManagement{/** * The pluginManagement {repositories {...}} block configures the * repositories Gradle uses to search or download the Gradle plugins and * their transitive dependencies. Gradle pre-configures support for remote * repositories such as JCenter, Maven Central, and Ivy. You can also use * local repositories or define your own remote repositories. The code below * defines the Gradle Plugin Portal, Google's Maven repository, * and the Maven Central Repository as the repositories Gradle should use to look for its * dependencies. */repositories{gradlePluginPortal()google()mavenCentral()}}dependencyResolutionManagement{/** * The dependencyResolutionManagement {repositories {...}} * block is where you configure the repositories and dependencies used by * all modules in your project, such as libraries that you are using to * create your application. However, you should configure module-specific * dependencies in each module-level build.gradle file. For new projects, * Android Studio includes Google's Maven repository and the Maven Central * Repository by default, but it does not configure any dependencies (unless * you select a template that requires some). */repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories{google()mavenCentral()}}rootProject.name="My application"include(":app")
Here let’s see how to use it.
plugins{/** * Use `apply false` in the top-level build.gradle file to add a Gradle * plugin as a build dependency but not apply it to the current (root) * project. Don't use `apply false` in sub-projects. For more information, * see Applying external plugins with same version to subprojects. */id("com.android.application")version"7.3.1"applyfalseid("com.android.library")version"7.3.1"applyfalseid("org.jetbrains.kotlin.android")version"1.7.20"applyfalse}
In this file, we can define the application id, compile SDK, default configs, build types, and dependencies for each module.
/** * The first section in the build configuration applies the Android Gradle plugin * to this build and makes the android block available to specify * Android-specific build options. */plugins{id("com.android.application")}/** * The android block is where you configure all your Android-specific * build options. */android{/** * The app's namespace. Used primarily to access app resources. */namespace="com.example.myapp"/** * compileSdk specifies the Android API level Gradle should use to * compile your app. This means your app can use the API features included in * this API level and lower. */compileSdk=33/** * The defaultConfig block encapsulates default settings and entries for all * build variants and can override some attributes in main/AndroidManifest.xml * dynamically from the build system. You can configure product flavors to override * these values for different versions of your app. */defaultConfig{// Uniquely identifies the package for publishing.applicationId="com.example.myapp"// Defines the minimum API level required to run the app.minSdk=21// Specifies the API level used to test the app.targetSdk=33// Defines the version number of your app.versionCode=1// Defines a user-friendly version name for your app.versionName="1.0"}/** * The buildTypes block is where you can configure multiple build types. * By default, the build system defines two build types: debug and release. The * debug build type is not explicitly shown in the default build configuration, * but it includes debugging tools and is signed with the debug key. The release * build type applies ProGuard settings and is not signed by default. */buildTypes{/** * By default, Android Studio configures the release build type to enable code * shrinking, using minifyEnabled, and specifies the default ProGuard rules file. */getByName("release"){isMinifyEnabled=true// Enables code shrinking for the release build type.proguardFiles(getDefaultProguardFile("proguard-android.txt"),"proguard-rules.pro")}}/** * The productFlavors block is where you can configure multiple product flavors. * This lets you create different versions of your app that can * override the defaultConfig block with their own settings. Product flavors * are optional, and the build system does not create them by default. * * This example creates a free and paid product flavor. Each product flavor * then specifies its own application ID, so that they can exist on the Google * Play Store, or an Android device, simultaneously. * * If you declare product flavors, you must also declare flavor dimensions * and assign each flavor to a flavor dimension. */flavorDimensions+="tier"productFlavors{create("free"){dimension="tier"applicationId="com.example.myapp.free"}create("paid"){dimension="tier"applicationId="com.example.myapp.paid"}}}/** * The dependencies block in the module-level build configuration file * specifies dependencies required to build only the module itself. * To learn more, go to Add build dependencies. */dependencies{implementation(project(":lib"))implementation("androidx.appcompat:appcompat:1.5.1")implementation(fileTree(mapOf("dir"to"libs","include"tolistOf("*.jar"))))}
So and now let’s look at Gradle properties files. First of all, Gradle properties, located in your root project directory, that you can use to specify settings for the Gradle build toolkit itself.
For this article, I tried to answer the questions, what is Gradle?, what are the advantages of Gradle? and how to use it on Android. So we’ll look at Gradle in more detail in the next posts. See you!
Hello everyone,
In this article, I’ll talk aboutFragmentResult
, which allow us communicate between fragments.
I’ll try to answer question like What is FragmentResult, In what situations should we use it, How to use.
Let’s start by explaining what isFragmentResult
.
We’ve heard Android doesn’t support to target (setTargetFragment()
/getTargetFragment()
) recent updates.
Several solutions were offered as an alternative to these structures. One of them is FragmentResult structures.
You’ll communicate between the two fragments, but you don’t want use viewmodel or arguments, sofragmentresult
is for you.
Give you a more detailed example, you can also use situations like in the scenario I’ll do and use in a mini application.
Scenario: We’ve application that use the Navigation Component. In this application, we want to open a dialog in a trailer and change the trailer according to the result from the dialog.
First of all, I mentioned that I’ll use navigation component in the application.
Graph structure I use;
<?xml version="1.0" encoding="utf-8"?><navigationxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/nav_graph.xml"app:startDestination="@id/homeFragment"><fragmentandroid:id="@+id/homeFragment"android:name="com.fevziomurtekin.myapplication.HomeFragment"android:label="fragment_home"tools:layout="@layout/fragment_home"/><dialogandroid:id="@+id/homeDialog"android:name="com.fevziomurtekin.myapplication.HomeDialog"android:label="HomeDialog"/></navigation>
As you can see I setup very simple. It consists of just one fragment and dialog.
If we talk about HomeFragment;
classHomeFragment:Fragment(){privatevartvResult:TextView?=nullprivatevarbtnShowDialog:Button?=nulloverridefunonCreateView(inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?):View?=inflater.inflate(R.layout.fragment_home,container,false)overridefunonViewCreated(view:View,savedInstanceState:Bundle?){super.onViewCreated(view,savedInstanceState)tvResult=view.findViewById(R.id.tv_result)btnShowDialog=view.findViewById(R.id.btn_show_dialog)// this listen to result.parentFragmentManager.setFragmentResultListener(REQUEST_KEY,this,{key,data->if(key==REQUEST_KEY){valresult=data.getBoolean("data").let{isAccept->if(isAccept)"Accepted"else"Rejected"}tvResult?.text=result}})// show bottom dialogbtnShowDialog?.setOnClickListener{findNavController().navigate(R.id.homeDialog)}}}
As you can see in the code above, I’m listening to result returned withsetFragmentResultListener
. The result comes to us as key and bundle data. We handle this too.
If we look at our DialogFragment where this is data is sent.
classHomeDialog:BottomSheetDialogFragment(){privatevarbtnAccept:Button?=nullprivatevarbtnReject:Button?=nulloverridefunonStart(){super.onStart()(requireView().parentas?View)?.let{safeView->BottomSheetBehavior.from(safeView).apply{state=BottomSheetBehavior.STATE_EXPANDEDsetBottomSheetCallback(object:BottomSheetBehavior.BottomSheetCallback(){overridefunonStateChanged(p0:View,p1:Int){if(p1==BottomSheetBehavior.STATE_DRAGGING){state=BottomSheetBehavior.STATE_EXPANDED}}overridefunonSlide(p0:View,p1:Float){}})}}}overridefunonCreateView(inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?):View?=inflater.inflate(R.layout.dialog_home,container,false)overridefunonViewCreated(view:View,savedInstanceState:Bundle?){super.onViewCreated(view,savedInstanceState)btnAccept=view.findViewById(R.id.btnAccept)btnReject=view.findViewById(R.id.btnReject)setListeners()}privatefunsetListeners(){btnAccept?.setOnClickListener{setResult(true)}btnReject?.setOnClickListener{setResult(false)}}privatefunsetResult(isAccept:Boolean){parentFragmentManager.setFragmentResult(REQUEST_KEY,bundleOf("data"toisAccept))findNavController().popBackStack()}overridefunonCancel(dialog:DialogInterface){super.onCancel(dialog)setResult(false)}}
In thesetFragmentResult
in the dialog, I send the result returned from the dialog.
You can access the application described in the article from thelink.
As seen in our examples, FragmentResult provides us with a very good alternative in communication between fragments with its simple use.
I hope it was useful, and see you in the next articles. 🖐🏼
This post is to The Turkish of this article is also available inMedium.
Hello everyone,In this article, I’ll talk about the DataStore structure, which is one of the Android Jetpack components. I’ll explain in code by giving examples with DataStore Preferences.
So what is a DataStore, why should we use it? Let’s start by giving information about!
DataStore is a Jetpack component that is used to store our local data as an asecron with Kotlin Coroutine and Flow structures instead of SharedPreferences.
So why should we use this structure when there is SharedPreferences? Let’s explain by comparing the two structures.
If we look at the this in more detail, there are two different type of structure. These;
We can provide storage using both types. But Proto provides type security. You also define a schema when usingProto.
You must define this schema under indexsrc/main/proto/directory
. This diagram contains the types of objects you’ll use.
More detailed information can be foundat.
Let’s look at its setup and usage now.
….
dependencies{// Preferences DataStoreimplementation"androidx.datastore:datastore-preferences:1.0.0-alpha02"// Proto DataStoreimplementation"androidx.datastore:datastore-core:1.0.0-alpha02"}
We’ve added our Datastore libraries to project by including our dependencies inbuild.gradle
.
Firstly we created class in the Datastore Preferences structure and created the data writing and reading fuctions that we’ll use.
classBasePreferences(privatevalcontext:Context){companionobject{valBASE_KEY=preferencesKey<String>(name="key")}privatevaldataStore:DataStore<Preferences>=context.createDataStore(name="pref")suspendfunsaveValue(value:String){dataStore.edit{preferences->preferences[BASE_KEY]=value}}vallastSavedValue:Flow<String>=dataStore.data.map{preferences->preferences[BASE_KEY]?:"default value"}}
Let’s simply show how we can write and use these values using to viewmodelScope in the viewModel.
classBaseViewModel(privatevalapp:Application,//repository):ViewModel{varbasePreferences=BasePreferences(app.applicationContext)vardataStoreLiveData:LiveData<String>=MutableLiveData()varbaseValue:String=""// we can write to value with the method.funsaveValue(valv:String){viewModelScope.launch{basePreferences.saveValue(v)}}// The method we created to observe the data.// We can listen to our value in the activity or fragment class where we will use our view model.fungetLiveData(){viewModelScope.launch{dataStoreLiveData=basePreferences.lastSavedValue().asLiveData(viewModelScope.coroutineContext+Dispatchers.Default)}}// we can read to value with the function.fungetValue(){basePreferences.lastSavedValue().collect{value->baseValue=value}}}
As I wrote in the commentline, we can listen to our livedata type variable in our activity or fragment class, where we’ll use our viewmodel and the necessary actions according to the charge.
Or we can collect and read data directly.
As seen in our examples, it is advantageous to use the DataStore Coroutine and Flow structure. It is a more reliable, more useful solution than SharedPreferences to securely perform asecron processing in UI threats.
See you in the next articles.
Basically,Interceptors are like security guards at the airport. They decide our passage and stop.
It has two different tasks. These;
There are two different interceptors.
addInterceptor()
.addNetworkInterceptor()
.These add to Interceptor in OkHttp.
funmyHttpClient():OkHttpClient{valbuilder=OkHttpClient().newBuilder().addInterceptor(/*our interceptor*/)returnbuilder.build()}
If we create to own interceptor, like code this bellow done.
classErrorInterceptor:Interceptor{overridefunintercept(chain:Interceptor.Chain):Response{valrequest:Request=chain.request()valresponse=chain.proceed(request)when(response.code()){400->{//Show Bad Request Error Message}401->{//Show UnauthorizedError Message}403->{//Show Forbidden Message}404->{//Show NotFound Message}// ... and so on}returnresponse}}
As understood in the code, we make the necessary logging according to the returned response.
This article hopefully help to your. See you in the next articles.