Trying to build a simple example that uses kotlin coroutines in an activity:
lifecycleScope.launch {
val result = httpGet("http://hmkcode-api.appspot.com/rest/api/hello/Android")
textView.setText(result)
}
Can't get rid of error "unresolved reference lifecycleScope"
Relevant part of gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.2"
def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycle:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"
implementation "android.arch.lifecycle:extensions:$androidArchVersion"
}
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.3'
languageVersion = '1.3'
}
}
As per the Lifecycle KTX documentation, you must include the lifecycle-runtime-ktx artifact if you want Coroutine specific extensions.
Note that lifecycle-runtime-ktx was only introduced in Lifecycle 2.2.0, so you'll need to Migrate to AndroidX, then upgrade to Lifecycle 2.2.0 if you want to use that functionality.
Late response here but I also faced this issue and finally managed to resolve it by changing the inherited Android Activity to an AppCompatActivity.
Related
I want to implement ViewModel into my app, but I cant import viewModels() or activityViewModels(). Trying to follow tutorial but its still unresolved reference for me.
private val userViewModel: ProfileFlowFragment.UserViewModel by viewModels()
Imports:
implementation "androidx.fragment:fragment-ktx"
implementation "androidx.activity:activity-ktx"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
Did you specify the version in implementation "androidx.fragment:fragment-ktx" ?
Anyway, I have these dependencies in my Gradle file, and everything is fine.
implementation "androidx.core:core-ktx:$corektxVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_stdlibVersion"
Also add
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
The latest stable versions are below, it should work.
def activity_version = "1.2.0"
def fragment_version = "1.3.0"
implementation "androidx.activity:activity-ktx:$activity_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
I still use Butterknife on my Kotlin project but for only binding colours and drawables as there's no need for that on binding views. However, after updating my project to AndroidX I can't get the library to work anymore.
That's what I have
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
kapt 'androidx.databinding:databinding-compiler:3.5.0-alpha02'
kapt 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02'
ext.kotlin_version = '1.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
AS 3.3.1
And call them this way
#JvmField
#BindColor(R.color.just_pink)
var pink: Int = 0
#JvmField
#BindDrawable(R.drawable.rectangle_pink_btn_all_radius)
var rectanglePinkAllRadius: Drawable? = null
Making sure I have Butterknife.bind(this, view) on my onCreate method.
Thanks for your help.
In Kotlin you specify the dependencies in a similar to Java way using Kotlin Annotation processing tool (kapt) instead of annotationProcessor.
So replace
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
with this
kapt 'com.jakewharton:butterknife-compiler:10.0.0'
Recently I have dived into learning new andoirdX artefacts including Android Architecture Components.
After reading through the officials docs and a google code samples I have successfully added the necessary dependencies in both my project and app level Gradle files.
They are as follows
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
and
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 28
defaultConfig {
applicationId "_ _ _ _ _ _"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
implementation 'com.google.android.material:material:1.0.0-rc01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
def lifecycle_version = "2.0.0-beta01"
def room_version = "2.0.0-beta01"
def navigationVersion = '1.0.0-alpha06'
kapt "androidx.room:room-compiler:$room_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$navigationVersion"
implementation "android.arch.navigation:navigation-ui-ktx:$navigationVersion"
implementation "androidx.room:room-runtime:$room_version"
}
after giving Gradle sync everything worked fine, however, it showed an error saying "Manifest merge error please see logs for more info"
I have double checked with an app from google repository and everything seems normal.
Please accept my humble appreciation in advance
Recently I have run into the same issue, Before giving you the solution I would like to tell you that in recent days, due to fast-paced development tools offered by Google our days-old development technique has changed significantly.
Take jetpack as an example, which is a part of AndroidX, and adding it into your projects requires some necessary steps. Moreover, if you are like me who uses Android studio's dialogs for creating an app, you are likely to have these kinds of issues including the one you have mentioned.
The reason for this is android studio still use Appcompat-v7, androidX on the other hand, uses
androidx.appcompat:appcompat:1.0.0
see the full artifacts listing
Above all, after adding responsible dependencies you need to add these two lines in you gradle.properties file
android.useAndroidX=true
android.enableJetifier=true
Unfortunately, if have come this far you would still probably face
Manifest marge error
this is because your automated layout files still have old classpaths such as
android.support.design.widget.CoordinatorLayout
which should be like this androidx.constraintlayout.widget.ConstraintLayout
this one android.support.design.widget.AppBarLayout should be com.google.android.material.appbar.AppBarLayout and So on.
After rechecking every classpath rebuild your project and everything should be fine.
I when try to implement a Room Database, I get the following error:
java.lang.RuntimeException: cannot find implementation for com.udacity.gradle.builditbigger.Database.HilarityUserDatabase. HilarityUserDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)
I tried adding the relevant kotlin dependencies to my gradle file (shown below) but when I do, all of my Databinding classes that would normally be generated with any issues are now generating errors in my gradle console. Is there way for me to use the DataBinding library and the Room Pesistence Library?
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...
dependencies{
kapt "android.arch.persistence.room:compiler:1.0.0"
}
It did happen to me before, make sure that you have all 3 dependencies in build.gradle
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
kapt 'android.arch.persistence.room:compiler:1.0.0'
Also, a "Project Clean" after gradle synch will help as well.
Make sure kotlin-kapt is included in app-level gradle file.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
and make sure you use kapt instead of annotationProcessor. That solved my problem.
And also check Room Model, DAO, and Database files for #Entity, #Dao and #Database annotations.
For usage of Room, LiveData and ViewModel you need these libraries:
•implementation "android.arch.persistence.room:runtime:1.0.0"
•implementation "android.arch.lifecycle:extensions:1.1.0"
•kapt "android.arch.persistence.room:compiler:1.0.0"
•kapt "android.arch.lifecycle:compiler:1.1.0"
LiveData and ViewModel allows you to use the DataBinding technique.
For more info check the official page: https://developer.android.com/topic/libraries/architecture/adding-components.html
I was facing the same issue, later found that I am not using the #Database annotation for AppDatabase
Use this
#Database(entities = {RowEntity.class, WifiDetailEntity.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase
{ ...
For the newer version of the room compiler don't need to add both dependencies, Just do the following -
kapt 'android.arch.persistence.room:compiler:2.2.3'
As explained above, for ease
1- Add the following in the build.gradle(Module:app) at the very top of the file
apply plugin: 'kotlin-kapt'
2- Then add
//Room for database
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
3- Sync the file and clean the project. Done
Had a similar problem while doing Codelabs. I was able to solve it by adding the updated Room dependencies on my build.gradle(module).
You can copy and paste the implementation declarations from the Room docs page in the Android Developer site. (Note: Check in your list of dependencies for
implementation 'androidx.room:room-runtime:2.2.5'
or similar, make sure to remove it and only leave the one you copied and pasted, which would look like this
implementation "androidx.room:room-runtime:$room_version"
)
Room | Android Developers #Declaring Dependencies
Add these Dependencies to your build.gradle file
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
MhzDev answer is fine these are the updated version of the dependencies
I'm trying to create my app component, but Dagger does not generate my app component.
here is MyApplication class
class MyApplication : Application() {
companion object {
#JvmStatic lateinit var graph: ApplicationComponent
}
#Inject
lateinit var locationManager : LocationManager
override fun onCreate() {
super.onCreate()
graph = DaggerApplicationComponent.builder().appModule(AppModule(this)).build()
graph.inject(this)
}
}
and here is my AppComponent class
#Singleton
#Component(modules = arrayOf(AppModule::class))
interface ApplicationComponent {
fun inject(application: MyApplication)
}
here is screenshot
this is my project on github
here is error log
Error:(7, 48) Unresolved reference: DaggerApplicationComponent
Error:(28, 17) Unresolved reference: DaggerApplicationComponent
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
Information:BUILD FAILED
Information:Total time: 21.184 secs
Error:e: .../MyApplication.kt: (7, 48): Unresolved reference: DaggerApplicationComponent
e: Unresolved reference: DaggerApplicationComponent
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Information:4 errors
Information:0 warnings
Information:See complete output in console
my solution is to add
apply plugin: 'kotlin-kapt'
and remove
kapt {
generateStubs = true
}
This helped me to fix this issue
Add this in the top of the build.gradle
apply plugin: 'kotlin-kapt'
Inside android tag add
kapt {
generateStubs = true
}
And then replace
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
to
kapt 'com.google.dagger:dagger-compiler:2.11'
Now Rebuild the project by
Build -> Rebuild project
Please try enabling stubs generation, this might be the reason why the class is not visible at this stage of the build process. In your build.gradle file, top level:
kapt {
generateStubs = true
}
I've already downloaded your Github project. Thanks for sharing!
The answer for your issue is pretty simple:
Build -> Rebuild project
Dagger dependencies files will be recreated and app after would launched with any problem.
I checked already this with Android Studio 2.1.2 version. It works
you should remove
kapt {
generateStubs = true}
and add to the top of application gradle file
apply plugin: 'kotlin-kapt'
then Dagger will take care of the rest :)
Add this on Top of build.gradlle
apply plugin: 'kotlin-kapt'
Add this in Dependencies
kapt 'com.google.dagger:dagger-compiler:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'
After that rebuild your project . i hope it will work.
Thanks
In my case missing kapt compiler dependency. Please make sure to have below dependency too.
kapt 'com.google.dagger:dagger-compiler:2.15'
app build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.usb.utility"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.dagger:dagger-android:2.15'
compile 'com.google.dagger:dagger-android-support:2.15' // if you use the support libraries
kapt 'com.google.dagger:dagger-android-processor:2.15'
kapt 'com.google.dagger:dagger-compiler:2.15'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Also for Java users, the solution is simply to Rebuild your project.
Answer is simple :
Build -> Rebuild project
It works for me.
Only two steps should be required for mixed Java/Kotlin projects:
Add apply plugin: 'kotlin-kapt' to the top of the build file
Replace annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' with kapt 'com.google.dagger:dagger-compiler:2.14.1'
In the new version of hilt, you need to use SingletonComponent::class instead of ApplicationComponent::class
Please try adding this to your build.gradle
android {
dexOptions {
incremental false
}
EDIT: apparently a year later this can happen if you don't apply kotlin-kapt plugin. Also make sure you use kaptinstead of annotationProcessor.
In my case Rebuild didn't help. I injected two dependencies in <module> component (BookComponent):
fun inject(fragment: ListFragment)
fun inject(fragment: NotFoundFragment)
When I removed one inject, the project had built. But both injects together broke build process. Then I simply swapped them:
fun inject(fragment: NotFoundFragment)
fun inject(fragment: ListFragment)
For anyone using the newest Dagger version (above 2.30):
ApplicationComponent has been replaced by SingletonComponent.
#Module
#InstallIn(SingletonComponent::class)
class RoomModule() {
. . .
}
See this answer.
This must solve your problem:
Update (March 29, 2020)
Inside your app-level build.gradle inside dependencies block, add these lines:
//dagger2
api 'com.google.dagger:dagger:2.24'
api 'com.google.dagger:dagger-android:2.24'
api 'com.google.dagger:dagger-android-support:2.24'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
kapt 'com.google.dagger:dagger-compiler:2.24'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
kapt 'com.google.dagger:dagger-android-processor:2.24'
compileOnly 'javax.annotation:jsr250-api:1.0'
implementation 'javax.inject:javax.inject:1'
Inside android block of app-level build.gradle,
kapt {
generateStubs = true
}
At the top of the app-level build.gradle, Do this in exactly below order.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
Finally, You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Settings for New Projects>search"Annotation processor"
After this, do from Menu Build > Rebuild. You are done!
Test:
#Component
public interface ApplicationComponent {
}
Now, you can use DaggerApplicationComponent that was generated at compile-time for your ApplicationComponent interface.
public class MyApplication extends Application {
ApplicationComponent applicationComponent = DaggerApplicationComponent.create();
}
In my case, I did everything in al answers but did not fix it!
my solution: go to Directory path of your project and delete
build, app>build, .idea , .Gradle
and go back to the android studio and rebuild the project and Done!