how do i correctly implement safeargs? - kotlin

I am trying to follow a tutorial in order to implement and use a room database YouTube-link.
My problem is that my gradle files look so different compared to what is shown in the video that I have no idea where to put the id "navigation.safeargs.kotlin" and the
classpath "androidx.navigation-safe-args-gradle-plugin:2.3.0-rc01"
or anything else that I might need. I have tried a bunch of places but it never works and I couldn't really find an answer, so if anyone knows, pls help me out.
build.gradle(Project:...):
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(Module:...):
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example. ..."
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.exifinterface:exifinterface:1.3.3'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation fileTree(dir: "libs", include: ["*.jar"])
// Room components
implementation "androidx.room:room-runtime:2.4.2"
kapt "androidx.room:room-compiler:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
androidTestImplementation "androidx.room:room-testing:2.4.2"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
}
settings.gradle(...):
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "..."
include ':app'

Go straight to the source IMO
Top-level build.gradle:
buildscript {
repositories {
google()
}
dependencies {
val nav_version = "2.4.2"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
App/module build.gradle:
plugins {
id("androidx.navigation.safeargs")
// or for Kotlin-only projects
id("androidx.navigation.safeargs.kotlin")
}
So in your top-level file, you need google() in your repositories block, and the classpath in your dependencies block. If you don't have one of those blocks, just create it - they go inside the buildscript block, see? The nav_version val is just a variable so you can make all your Navigation components use the same version by referring to that, and the actual version number is only set in one place. You can just write the number in the dependency statement if you like - make sure all the Navigation components are using the same version though!
Stick the id("androidx.navigation.safeargs") line in your module's build file's plugins block. The order sometimes matters for some of these, so try it at the bottom. The syntax is a little different from how they look in your current file, but it's just another way of writing it - either's fine, you can mix and match

Use this one:
id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false

In your build.gradle (project level) try adding:
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id 'androidx.navigation.safeargs.kotlin' version '2.5.1' apply false
}
and in the build.gradle (module level) add:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs.kotlin'
}

Related

The binary version of its metadata is 1.8.0, expected version is 1.6.0. Module was compiled with an incompatible version of Kotlin

C:/Users/LENOVO/.gradle/caches/transforms-2/files-2.1/2bcd0a6b95744b6f0ee26f9336bd22eb/jetified-annotation-jvm-1.6.0-beta01.jar!/META-INF/annotation.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.
buildscript {
ext.kotlin_version = '1.4.32'
ext.anko_version='0.10.8'
repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
classpath 'com.google.gms:google-services:4.3.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// Add the dependency for the Performance Monitoring plugin
classpath 'com.google.firebase:perf-plugin:1.4.1'
}
}
This project contains a few incompatible or even dangerous things, that also lead you to the problem you're having:
jcenter() is not in service anymore, so it should be replaced by mavenCentral() or your dependencies will not be correctly resolved.
You specify both Kotlin 1.4.32 (no longer maintained) and 1.6.10 (no longer ideal, but still usable) and the classes will definitely not be compatible between them.
Some of your other dependencies might be even newer and use Kotlin 1.8.0, so your older runtimes won't be able to read their metadata.
Recently, I faced a similar issue on creating a new project in Android studio with Compose Material3 template.
So I upgraded below libraries/plugins solve my issue. I also attached my gradle files for reference.
kotlin version to 1.8.0
kotlinCompilerExtensionVersion '1.4.0'
compose_version to 1.3.1
build.gradle(app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.hometech.anatomymuscles'
compileSdk 33
defaultConfig {
applicationId "com.hometech.anatomymuscles"
minSdk 23
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
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'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.0'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.compose.material3:material3:1.1.0-alpha05'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}
build.gradle(project)
buildscript {
ext {
compose_version = '1.3.3'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}

Failed to resolve: androidx.compose.ui:ui-tooling and other modules

While building gradle I encounter a warning: Failed to resolve: androidx.compose.ui:ui-tooling
and recommends adding Google maven repository but this is already specified in settings.gradle. I tried different versions for compose_ui_version and kotlin compiler extension version. How can I resolve this issue?
buildscript {
ext {
compose_ui_version = "1.4.0-alpha05"
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}
and app's build.gradle:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.bettehomes'
compileSdk 33
defaultConfig {
applicationId "com.example.bettehomes"
minSdk 25
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
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'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerVersion "1.7.20"
kotlinCompilerExtensionVersion "1.4.0-beta05"
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
def composeBom = platform("androidx.compose.compose-bom:2022.12.00")
implementation composeBom
androidTestImplementation composeBom
implementation 'com.google.maps.android:maps-compose'
implementation 'com.google.android.gms:play-services-maps'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose'
implementation "androidx.compose.ui:ui-tooling"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation 'androidx.compose.material3:material3'
// Android Studio Preview Support
debugImplementation "androidx.compose.ui:ui-tooling"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4"
debugImplementation "androidx.compose.ui:ui-test-manifest"
}
Have you tried adding the following into the buildscript section inside your project level gradle file?
repositories {
mavenCentral()
}
I was having the same problem.
Eventually I tried to add the version explicitly like this:
implementation "androidx.compose.ui:ui-tooling-preview:1.3.2"
Notice that 1.3.2 is the version imported by the BOM version you are using.
Obviously it was still not working but at least now I got a more descriptive error message.
It turned out I need compileSdkVersion 33 (I was using 31).
Since I don't want to increment yet my compileSdkVersion, I will have to downgrade ui-tooling-preview to explicitly use version 1.1.1
I can see you are already using compileSdkVersion 33, however adding explicitly the version might help you get a more descriptive error message.

import com.firebase.ui.auth.AuthUI not working

ive being trying to write a loggin with google , phone and email activity, but the app is not importing : com.firebase.ui.auth.AuthUI
this is my app build gradle
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.stockmng"
minSdk 22
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation 'com.google.firebase:firebase-auth:21.1.0'
implementation 'com.google.firebase:firebase-firestore:24.4.1'
implementation 'com.google.firebase:firebase-database:20.1.0'
implementation 'com.firebaseui:firebase-ui-auth:6.2.1'
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'com.google.android.gms:play-services-auth:20.4.0'
//apply plugin: 'com.android.application'
implementation 'com.github,MrNouri:DynamicSizes:1.0'
}
and my project gradle
buildscript {
repositories{
google()
mavenCentral()
gradlePluginPortal()
maven { url 'https://jitpack.io'}
}
dependencies {
classpath 'com.google.gms:google-services:4.3.14'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
i tried adding, changing updating the implentations in app build gradle,
i have permission.internet and ACCES_NETWORK_STATE added to the manifest.
a configured the api keys,and oAuth updated,
also adding in google maven and maven central in repository en the project gradel,
nothing works.

How can I add serialization dependencies to my kotlin project?

I am trying to add json serialization dependencies in my kotlin project but it doesnt work and lots of error appear when I sync project. I dont know why. I did as in the serialization dependency addition section on the kotlin web site, but it didn't work. how do I add it to my codes ?.
Build.Gragle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
compose_version = '1.0.1'
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Build.Gradle(module)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'org.jetbrains.kotlin.android.extensions'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.enestigli.dictionaryapp"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
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'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
androidExtensions {
experimental = true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
//implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
// Coroutine Lifecycle Scopes
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
// Room
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
// To use Kotlin annotation processing tool (kapt)
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:2.3.0"
//Material theme3
implementation "androidx.compose.material3:material3:1.0.0-alpha12"
implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha12"
//skrape{it}
//implementation 'org.jsoup:jsoup:1.12.1'
//compose
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
}
EDIT
I found the solution
In this case, you need to install whatever Kotlin version is. In my case like this
my kotlin version is : 1.5.21
and I added this dependency in gradle.plugin(module)
plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.21'
}
and I added this
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1'
}
and that's it, now you can use serialization in your data class like this.
for example
import kotlinx.serialization.Serializable
#Serializable
data class example(
val example: String,
)

Android Kotlin Two-Way databinding BR class error

I'am trying to do a basic android application using kotlin and two way data binding to show on a text view the changes in a edit text. But i'am getting this error :
w: [kapt] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: android.databinding.annotationprocessor.ProcessDataBinding (DYNAMIC).
when i'am trying to do with one way data binding it is working well it show me the Contact class's name field successful
my codes:
build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(module:app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
dataBinding{
enabled = true
}
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.mvvm.demo"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
my Contact.kt
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
class Contact( var Id:Int,
var Name:String,
var Email:String):BaseObservable() {
#Bindable
var _name: String = Name
set(value) {
field = value
notifyPropertyChanged(BR._name)
}
get() = field
}
unfortunately, this error has not yet been resolved. I hope the warning will be resolved in the next update. I've been waiting for updates since November because of this warning
source: https://issuetracker.google.com/issues/110061530#comment28