Toolchain:
Android Studio 3.0 Canary 2:
Build #AI-171.4041253, built on May 24, 2017
JRE: 1.8.0_112-release-b736 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.12.5
Google SDK/Tools: I have downloaded the latest "all" (Android O, tools, libraries, etc. at least according to Android Studio SDK Manager).
Problem: javaClass<> is missing and Android Studio can't "import it".
What I did:
Create new Android project, target API 23 and told it to include a "basic activity".
Added the Gradle dependencies for ViewModel and Room taken from: https://developer.android.com/topic/libraries/architecture/adding-components.html
These are the lines I added to my App Module's gradle file:
compile "android.arch.lifecycle:runtime:1.0.0-alpha1"
compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"
compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
The Kotlin reference in the same Gradle is: compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" (I didn't add that one, came automatically)
I then created a simple Kotlin class called MainViewModel:
import android.arch.lifecycle.ViewModel
class MainViewModel : ViewModel() {}
Then I went to my Activity and tried to do what this Google Documentation says: https://developer.android.com/topic/libraries/architecture/viewmodel.html
It's in Java but converted to Kotlin, I think it should look like:
val mainViewModel = ViewModelProviders.of(this).get(javaClass<MainViewModel>)
The problem is that Android Studio is not finding javaClass and the fix (to press ⌥⏎) doesn’t do anything.
What am I missing?
I decided to try to use the Java To Kotlin conversion. So I created a new Activity in Java, and wrote the above code as the java documentation states.
After the class was working, I did Code -> Convert Java Class To Kotlin in Android Studio, and the resulting Kotlin class had the following line:
val viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
This compiled perfectly. I will have to learn more about which one is correct, but this one may save you some time if you’re, like me, new to the language.
Related
I'm wondering if the difference of the kotlin version between gradle setting and ide.
I can see the kotlin version of IDE(Tools -> Kotlin -> Configure Kotlin).
And there is another Kotlin version defined in build.gradle.
I want to know the difference.
I think that the version in buidl.gradle affects to real project and some dependencies.
And the version in IDE affects when we write the code.
Is my guess right?
IDE kotlin version help live coding style i.e. you get kotlin suggestion in that version.
Gradle kotlin version is your project version i.e. it compile your code on that version.
These version may be different. Your IDE kotlin version may be updated than your gradle kotlin version.
In our Android app, I just upgraded the kotlin plugin to 1.5.0.
In top level build.gradle.kts:
buildscript {
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
And in app build.gradle.kts:
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.0")
Now, when building, I get a bunch of deprecation warnings about the Duration API used in such statements:
#ExperimentalTime
val myVal = 2.seconds
Warning says: 'seconds: Duration' is deprecated. Use Duration.seconds() function instead.
OK, so I proceed with the replacement like asked:
val myVal = Duration.seconds(2)
Now, the seconds() method appears in red in Android Studio with the error message: Expression 'seconds' cannot be invoked as a function. The function 'invoke()' is not found
It seems somehow that Android Studio is mixing Kotlin 1.5 with 1.4 I guess. Is there somewhere else than the two mentioned libraries on top that should be changed to point to kotlin 1.5?
I've tried invalidating the cache and remove all build folders without success.
According to Jetbrains on the Kotlin reddit, despite some 1.5.0 artifacts being available on maven central repo allready, Kotlin 1.5.0 is not yet officially released. Some bits and pieces are missing among others the IDE plugins, which explains the problem.
https://www.reddit.com/r/Kotlin/comments/n0gt0d/is_kotlin_150_released/
I'm having trouble seeing the generated dagger classes in my project. The app builds, dagger injects the classes just fine, etc. but I can't actually view the dagger classes in my IDE - they show up as red (indicating an error), and trying to step into the class while debugging and/or viewing the source code to see how they are doing things doesn't work.
Here's a sample project that doesn't work for me: https://github.com/erikcaffrey/Dagger2-MVP-Sample
All I did was pull it, run it, and then went to the CategoryApplication class and tried to see the source for the DaggerAppComponent class, but I am not able to.
For reference, here's what I see:
Some additional information: On other computers I do not have this problem (Windows 10, MacBook Pro #2) but my main development MacBook Pro if affected by this. The dagger generated files exist in my project structure and I can view them manually - but ideally the IDE can pick these up and not show this as an error visually. I am using Android Studio 3.1.4 but this was also occurring in older versions (not sure on the exact numbers though)
The sample project I am using has the following gradle dependencies setup:
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
implementation 'com.google.dagger:dagger:2.15'
It doe snot use the android dagger components, nor the android support components, FWIW.
Update: I noticed that my Android Studio instance on my dev MacBook Pro does not list the dagger generated files in the app/build/generated/source/apt/ directory when viewing the project files in the 'Project' setting. The files on the hard drive do exist, but they do not show up in the IDE... so I doubt this is a dagger issue - just something wrong with the IDE not picking up these files.
def daggerVer = 2.12 // or latest version
implementation "com.google.dagger:dagger:$daggerVer"
implementation "com.google.dagger:dagger-android-support:$daggerVer"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"
In your build.gradle file, add this line:
dependencies {
apt 'com.google.dagger:dagger-compiler:2.0'
}
which will make the generated sources visible in android studio.
I noticed this was not an issue on Android Studio 3.2 beta, so I figured it was a configuration issue. I went ahead and uninstalled Android Studio and deleted all references to Android Studio from my machine and then reinstalled Android Studio 3.1.4. The issue is no longer occurring.
I add Gson to my gradle dependencies and nearly all dependencies stop to resolve. (additionally I get the "Error: Please select Android SDK"-Error)
I know this a well known bug and I can solve it as described in this post.
I took like 3 tries with different versions, but i just not works.
Android Studio: "Please select Android SDK"
Any hints why Gson makes here trouble?
(I'm using Kotlin.)
My Android Studio:
> Android Studio 3.1.2 Build #AI-173.4720617, built on April 13, 2018
> JRE: 1.8.0_152-release-1024-b02 amd64 JVM: OpenJDK 64-Bit Server VM by
> JetBrains s.r.o Windows 10 10.0
Relevant gradle.build snippet
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.4'
// -erro- implementation 'com.google.code.gson:gson:2.8.2'
implementation "com.google.code.gson:gson:+"
It's not recommended to use the latest version (+), use this instead:
implementation 'com.google.code.gson:gson:2.8.5'
I just updated my small Android project to Kotlin. And as a result I replaced annotationProcessor with kapt everywhere it was used in my build.gradle file. My problem is that although I don't use Guava Android Studio is allowing me to e.g. import com.google.common.base.Preconditions and use it, but when it comes to launching the app NoClassDefFoundError is thrown.
So I assume that Guava isn't compiled with my project () and that's why I am getting this error. But then why Android Studio allowing me to use it?