I'm trying to implement the Safe Args plugin, but my auto-generated class/method MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment() can't seem to find the specified arguments from the nav_graph. I don't think the problem is with the plugin not generating the classes, since I'm able to reference the necessary class and method - my issue is with the action method not recognizing my argument and giving me a warning:
Too many arguments for public final fun actionMainScreenFragmentToNoteScreenFragment(): NavDirections defined in com.example.notelite.ui.mainscreen.MainScreenFragmentDirections
I've applied all necessary plugins and dependencies on gradle.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'kotlin-parcelize'
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.notelite"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//Navigation Components
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
//Room Database
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
//Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
//Test helpers
testImplementation "androidx.room:room-testing:$room_version"
//Dagger Hilt - Dependency Injection
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
//Preferences DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-beta01"
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.4.30'
ext.hilt_version = '2.35'
repositories {
google()
jcenter()
}
dependencies {
def nav_version = "2.3.5"
classpath 'com.android.tools.build:gradle:4.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_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
}
Here's my nav_graph code:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns: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"
app:startDestination="#id/mainScreenFragment">
<fragment
android:id="#+id/mainScreenFragment"
android:name="com.example.notelite.ui.mainscreen.MainScreenFragment"
android:label="MainScreenFragment"
tools:layout="#layout/fragment_mainscreen">
<action
android:id="#+id/action_mainScreenFragment_to_noteScreenFragment"
app:destination="#id/noteScreenFragment" />
<argument android:name="noteid"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="#+id/noteScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteScreenFragment"
android:label="NoteScreenFragment"
tools:layout="#layout/fragment_note_screen">
<action
android:id="#+id/action_noteScreenFragment_to_noteEditScreenFragment"
app:destination="#id/noteEditScreenFragment" />
</fragment>
<fragment
android:id="#+id/noteEditScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteEditScreenFragment"
android:label="NoteEditScreenFragment"
tools:layout="#layout/fragment_edit_note_screen"/>
</navigation>
Here's the code snippet I'm trying to call the method in - just defining a navigation action and trying to pass an argument parameter:
override fun onNoteClick(note: NoteEntity) {
val noteid = note.id
val action = MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment(noteid)
}
"noteid" is the key value of an item in a Room database table. I've specified the type accordingly. Any ideas? Couldn't find anyone with a similar problem. I've tried clean and rebuild, restarting Android Studio, updating some plugins, updating IDE...nothing worked. What's funny is I can't seem to find the generated classes on the java(generated) folder, even though I'm able to reference them on my fragment - but that might be just me being a newbie and just really not finding them
Your <argument> needs to be associated with the <fragment> destination you are navigating to: your NoteScreenFragment:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns: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"
app:startDestination="#id/mainScreenFragment">
<fragment
android:id="#+id/mainScreenFragment"
android:name="com.example.notelite.ui.mainscreen.MainScreenFragment"
android:label="MainScreenFragment"
tools:layout="#layout/fragment_mainscreen">
<action
android:id="#+id/action_mainScreenFragment_to_noteScreenFragment"
app:destination="#id/noteScreenFragment" />
</fragment>
<fragment
android:id="#+id/noteScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteScreenFragment"
android:label="NoteScreenFragment"
tools:layout="#layout/fragment_note_screen">
<argument android:name="noteid"
app:argType="integer"
android:defaultValue="0" />
<action
android:id="#+id/action_noteScreenFragment_to_noteEditScreenFragment"
app:destination="#id/noteEditScreenFragment" />
</fragment>
<fragment
android:id="#+id/noteEditScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteEditScreenFragment"
android:label="NoteEditScreenFragment"
tools:layout="#layout/fragment_edit_note_screen"/>
</navigation>
Related
My android studio version is
Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021
After updating IDE and kotlin to 1.6, I opened a project I was working on and Android Stidio marked data binding fields as red, showing the following error:
In text:
Cannot access 'androidx.databinding.Observable' which is a supertype of
'com.cioccarellia.wordbucket.databinding.ActivityMainBinding'.
Check your module classpath for missing or conflicting dependencies
The weird thing is that everything compiles and the app works just fine, these errors are just in the IDE, and they pop up every time I access views through data binding.
I haven't changed my dependencies since updating android studio. Anyone else has the same problem / solution for this situation?
The Main Activity xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.cioccarellia.wordbucket.activity.MainActivity">
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
General build.gradle:
buildscript {
ext {
kotlin_version = "1.6.0"
}
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.10'
classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.3"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
classpath "com.github.ben-manes:gradle-versions-plugin:0.39.0"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
Application build.gradle:
plugins {
id "com.android.application"
id "kotlin-android"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
id "com.google.gms.google-services"
id "androidx.navigation.safeargs.kotlin"
id "com.github.ben-manes.versions"
id "kotlin-parcelize"
id "com.google.firebase.crashlytics"
}
android {
compileSdk 31
defaultConfig {
applicationId "com.cioccarellia.wordbucket"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
buildTypes {
debug {
versionNameSuffix ".dev"
}
release {
debuggable false
// Enables code shrinking, obfuscation, and optimization
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
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 {
dataBinding true
}
sourceSets {
main.res.srcDirs = [
"src/main/res",
"src/main/res-public"
]
android.sourceSets.all {
java.srcDir("src/$name/kotlin")
}
}
lintOptions {
// if set to true (default), stops the build if errors are found.
abortOnError false
// if true, only report errors.
ignoreWarnings false
}
}
dependencies {
// AndroidX
implementation "androidx.core:core-ktx:1.7.0"
implementation "androidx.appcompat:appcompat:1.4.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.2"
implementation "com.google.android.material:material:1.6.0-alpha01"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
// Lifecycle
def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
// DI -> Hilt
implementation "com.google.dagger:hilt-android:2.40.3"
kapt "com.google.dagger:hilt-compiler:2.40.3"
// Import the Firebase BoM
implementation platform("com.google.firebase:firebase-bom:28.4.2")
// When using the BoM, you don"t specify versions in Firebase library dependencies
implementation "com.google.firebase:firebase-crashlytics-ktx"
implementation "com.google.firebase:firebase-analytics-ktx"
implementation "com.google.firebase:firebase-auth-ktx"
implementation "com.google.firebase:firebase-firestore-ktx"
// LeakCanary
debugImplementation "com.squareup.leakcanary:leakcanary-android:2.7"
// Logging
implementation "com.jakewharton.timber:timber:5.0.1"
// Styles
implementation 'com.airbnb.android:paris:2.0.1'
kapt 'com.airbnb.android:paris-processor:2.0.1'
// JUnit Default Dependencies
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}
Try to restart the IDE. If that does not work, clean the project be going to Build > Clean Project at the top of the window, then completely rebuild it by going to Build > Rebuild Project.
Edit:
Another reason is that Android Studio will no longer support binding to straight elements, so you have have to specify the binding yourself.
To do this, add BuildFeatures in the application gradle file:
android {
compileSdk 31
//This right here
buildFeatures{
viewBinding true
}
defaultConfig {
applicationId "com.cioccarellia.wordbucket"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
Then create a new binding by adding this to your activity file:
//Declare the binding
private lateinit val binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
//Initialize the binding
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
//Change the content view to the root of the binding
setContentView(binding.root)
}
Then reference binding when using your views:
binding.toolbar.title = bucket.name
Updating android studio fixed the issue
I Stuck with the problem. I have upgrade project grade to 7.0.1 and Kotlin 1.5.30. Before upgrading everything was working fine. But after upgrading, I am start getting the following error for each project. Even, I have tried to create new project also. There is also same issue I am facing. Please check the below image.
Below, I am writing all the files of my project. Which is responsible for data binding and view binding.
Project base build.gradle
buildscript {
ext.kotlin_version = '1.5.30'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Application based build.gradle file
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdk 30
defaultConfig {
applicationId "colour.moon"
minSdk 21
targetSdk 30
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_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
buildFeatures {
dataBinding true
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
}
settings.gradle file Here this file automatically containing the given code. I though to put it here for easy understanding
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ':app'
This is main file, Which is producing the error. Please check it carefully.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="colour.moon.MainVM" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
When, I am converting my view to layout then the above error is coming. Please let me know, How to solved it? And If we need to remove layout then how to use data binding in the XML file!
I will be very thankful for your answer on it!
So, I've been trying for hours to make notification works but without success.
My goal is to send a notification from the Firebase console and receive it on the phone. This means that I should only add the library to my application and configure a project on Firebase.
In my app I'm already using firebase for storage, analytics and ads without problem.
I've followed the official tutorial and also this video
I've not added the service in the manifest because I don't need to do any particular message handling
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.myapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.myapp">
<activity android:name="com.domain.myapp.MainActivity"
android:screenOrientation="portrait">
<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}.provider"
android:exported="false"
android:requestLegacyExternalStorage="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="myid"/>
</application>
</manifest>
root level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.20"
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.5'
def nav_version = "2.3.4"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_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
}
Application level build file
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id 'com.google.gms.google-services'
}
android {
signingConfigs {
release {
storeFile file('keystore')
storePassword 'psw'
keyAlias 'key0'
keyPassword 'psw'
}
}
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.domain.myapp"
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
def AD_UNIT_ID = "AD_UNIT_ID"
debug {
buildConfigField "String", AD_UNIT_ID, AD_UNIT_ID_TEST
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "String", AD_UNIT_ID, AD_UNIT_ID_PROD
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-storage:19.2.1'
implementation 'com.google.firebase:firebase-messaging-ktx:21.0.1'
implementation 'com.google.firebase:firebase-analytics-ktx:18.0.2'
def nav_version = "2.3.4"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
implementation "androidx.navigation:navigation-compose:1.0.0-alpha09"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation("com.github.bumptech.glide:glide:4.12.0")
implementation("com.github.bumptech.glide:recyclerview-integration:4.11.0")
kapt 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'com.firebaseui:firebase-ui-storage:7.1.1'
implementation 'com.github.smarteist:autoimageslider:1.4.0'
implementation 'com.google.android.gms:play-services-ads:19.8.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
This is my App class:
class App: Application() {
override fun onCreate() {
super.onCreate()
MobileAds.initialize(this)
}
}
And this is my main activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupAdViewInto(adContainer)
}
private fun setupAdViewInto(adContainer: LinearLayout, adSize: AdSize = SMART_BANNER) {
val adView = AdView(this)
adView.adSize = adSize
adView.adUnitId = AD_UNIT_ID
adContainer.addView(adView)
adView.loadAd(AdRequest.Builder().build())
}
}
On the firebase console I see this:
Note: I've tried crating a new app from scratch and to do the same operation and that it's working!
I got it working, but I don't know how... I'm thinking is Android Studio's fault.
I've done these steps
created a new project on firebase
downloaded and imported the new google-services.json
cleaned cache data and reinstalled the app on the emulator using the button Run 'app' from Android Studio
the app is still showing stuff from the old firebase application, like it's using the old google-services.json
deleted gradle's build folder from Android Studio
reinstalled the app as before using the old configuration
notifications are now working
WTF!??
I'm thinking that the problem was due to gralde/android studio.
I've lost like 5 hours on this... I love programming :) :)
I just created a new project on Android Studio 3.3 Canary 3 with Kotlin enabled. Then I also enabled data binding, but I'm getting an error saying that it could not find the DataBindingComponent class.
Here is my gradle file
buildscript {
apply from: 'versions.gradle'
addRepos(repositories)
dependencies {
classpath deps.android_gradle_plugin
classpath deps.kotlin.plugin
classpath deps.kotlin.allopen
classpath deps.navigation.safe_args_plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
repositories {
google()
}
}
allprojects {
addRepos(repositories)
}
task clean(type: Delete) {
delete rootProject.buildDir
}
My module gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'
android {
compileSdkVersion build_versions.target_sdk
buildToolsVersion build_versions.build_tools
defaultConfig {
applicationId "arca.advanced.mg.com.myapplication"
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation deps.support.app_compat
implementation deps.support.recyclerview
implementation deps.support.cardview
implementation deps.support.design
implementation deps.support.legacy
implementation deps.navigation.fragment_ktx
implementation deps.room.runtime
implementation deps.lifecycle.runtime
implementation deps.lifecycle.extensions
implementation deps.lifecycle.java8
implementation deps.retrofit.runtime
implementation deps.retrofit.gson
implementation deps.glide.runtime
implementation deps.dagger.runtime
implementation deps.dagger.android
implementation deps.dagger.android_support
implementation deps.constraint_layout
implementation deps.kotlin.stdlib
implementation deps.timber
implementation deps.rx.java
implementation deps.rx.android
kapt deps.dagger.android_support_compiler
kapt deps.dagger.compiler
kapt deps.room.compiler
kapt deps.lifecycle.compiler
}
my fragment file
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="arca.advanced.mg.com.arca.ui.splash.SplashViewModel" />
</data>
<RelativeLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
</layout>
and here is my error
I think it's kapt conflict.
Try to turn off Dagger if it's possible and check errors from Room like this:
Fields annotated with #Relation cannot be constructor parameters. These values are fetched after the object is constructed.
Cannot find setter for field.
Cannot figure out how to save this field into database.
If it will not help, try to turn off libs with kapt's one by one and check the error log carefully.
Check with ./gradlew app:dependencies transitive dependencies, maybe problems with androidx.
Also check gradlew app:dependencies --configuration kapt maybe you will find something suspicious
For some reason I always get confronted with the following error:
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists, is public, and has an empty constructor that is public
I was following the Guide on Android Developers yet the Error above occurs right after starting the App.
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="projects.ferrari.rene.sken.ui.main.MainFragment"
android:label="main_fragment"
tools:layout="#layout/main_fragment" >
<action
android:id="#+id/action_mainFragment_to_takeImageFragment"
app:destination="#id/takeImageFragment" />
</fragment>
<fragment
android:id="#+id/takeImageFragment"
android:name="projects.ferrari.rene.sken.ui.takeimage.TakeImageFragment"
android:label="take_image_fragment"
tools:layout="#layout/take_image_fragment" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="#navigation/nav_graph"
app:defaultNavHost="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package projects.ferrari.rene.sken
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}
override fun onSupportNavigateUp()
= findNavController(R.id.my_nav_host_fragment).navigateUp()
}
build.gradle (app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 'android-P'
defaultConfig {
applicationId "projects.ferrari.rene.sken"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def anko_version = "0.10.5"
def nav_version = "1.0.0-alpha01"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation 'com.google.android.material:material:1.0.0-alpha1'
implementation 'com.google.android.gms:play-services-vision:15.0.2'
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
implementation 'com.github.pqpo:SmartCropper:v1.1.3#aar'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'
}
Basically I am using Kotlin, AndroidX and targeting Android P. I really can't figure out where the problem lies.
EDIT There might be an issue with the import. For navigation I don't import it using AndroidX. In Build your first App with Jetpack they use implementation 'androidx.navigation:navigation-fragment:' + rootProject.navigationVersion yet I could not figure out which version they were using so I read the Architecture Component Release Notes where it says for AndroidX '2.0.0-alpha1' should be used. Unfortunately this can not be resolved.
For Java:
dependencies {
def nav_version = "2.3+"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
}
For Kotlin:
dependencies {
def nav_version = "2.3+"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}