Kotlin MyClass.class.java Unresolved reference: java - android

In Java Android we use MyActivity.class to start a new activity. I was playing with Android Studio 3 beta1 and tried to convert it to kotlin. It gets converted to MyActivity::class.java but this doesn't compile. It gives me Unresolved reference: java. What would be the correct equivalent?
Project Build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta1'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
App Build.gradle:
apply plugin: 'com.android.application'
android {
signingConfigs {
}
compileSdkVersion 25
buildToolsVersion "25.0.2"
lintOptions {
abortOnError false
}
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "app"
minSdkVersion 15
targetSdkVersion 25
versionCode 104
versionName "4.0.11"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
flavorDimensions "mode"
productFlavors {
free {
dimension "mode"
}
paid {
dimension "mode"
applicationId "app.paid"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.google.android.gms:play-services-ads:10.2.6'
releaseCompile 'ch.acra:acra:4.9.1'
// compile 'ch.acra:acra:4.9.1'
compile 'com.google.firebase:firebase-core:10.2.6'
compile 'com.google.firebase:firebase-database:10.2.6'
compile 'com.google.firebase:firebase-auth:10.2.6'
compile 'com.google.android.gms:play-services-auth:10.2.6'
compile 'com.github.frangsierra:rx2firebase:1.1.3'
}
apply plugin: 'com.google.gms.google-services'

Try adding following changes in your gradle file.
Add classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.3-2" in your project build.gradle file.
build.gradle(:project)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.3-2" // New
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
...
...
And add
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
two lines in app build.gradle file.
build.gradle(:app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions' // New
apply plugin: 'kotlin-android' // New
android {
...
...
...
...
}
dependencies {
...
...
}
apply plugin: 'com.google.gms.google-services'
This must solve your problem as i have updated My Android Studio to 3.0 beta 1 and having so issues.
I Hope this helps.

I think this was an bug. I just updated to beta2 an the problem was solved.

Related

Using Kotlin in a library module without using it in the app module

I'm trying to use Kotlin in a library module without using it in the app module. The app module only uses Java and does not use any Kotlin classes from the library. Gradle won't compile hoever:
Error:(2, 1) A problem occurred evaluating project ':<Library>'.
> Plugin with id 'kotlin-android' not found.
Changes I made to include Kotlin:
{library root} / build.gradle
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
...
}
allprojects {
repositories {
jcenter()
}
}
{library root} / {library module} / build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
...
dependencies{
...
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
When I add the same to the app module, the project compiles without issue, but I'd like to avoid adding it in the app module because I'd like to use this library in multiple apps without making code changes to those apps
Gradle version used : 3.3
android gradle plugin version: 2.3.3
Edit: #Jushua's answer works, but it still requires to update the project root build.gradle. I was hoping for a solution where only the dependency on the library would have to be added to make the whole thing work.
I am able to do that without any problem.
build.gradle (Project)
buildscript {
ext.kotlin_version = "1.1.4"
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:2.3.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
build.gradle (app)
apply plugin: "com.android.application"
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.app"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
}
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2", {
exclude group: "com.android.support", module: "support-annotations"
})
compile "com.android.support:appcompat-v7:26.0.1"
testCompile "junit:junit:4.12"
compile project(path: ":library")
}
build.gradle (library)
apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 17
targetSdkVersion 26
versionCode 13
versionName "1.1.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
}
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2", {
exclude group: "com.android.support", module: "support-annotations"
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "com.android.support:appcompat-v7:26.0.1"
testCompile "junit:junit:4.12"
}
You just need to add in your module build:
buildscript {
// (Optional) ext.kotlin_version = '1.1.4-3'
repositories {
google()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
and you could either declare the kotlin_version variable in that specific build, or declaring it in the project root build file, since the kotlin version could be updated from other modules:
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta3'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
You can add buildscript section with kotlin plugins only in your library:
buildscript {
ext.kotlin_version = '1.1.4-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"
And now you can include library to application, without modification of root build.gradle
compile project(path: ":library")

Issue with databinding using Kotlin for Android

I am trying to enable data binding for my Android project using Kotlin. I have Kotlin plugin enabled, but I am not able to enable data binding I keep getting the following error
Error:(66, 0) Could not find method kapt() for arguments [com.android.databinding:compiler:2.0.0-beta6] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
I have the following dependencies for data binding in my gradle file
dependencies {
...
kapt 'com.android.databinding:compiler:2.0.0-beta6'
}
kapt {
generateStubs = true
}
Edit: With Kotlin 1.1 and Kapt3 it works slightly different:
in your project build.gradle:
buildscript {
ext {
...
plugin_version = "2.3.1"
kotlin_version = "1.1.2-3"
...
}
...
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.android.tools.build:gradle:$plugin_version"
...
}
}
and in your app build.gradle:
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"
...
android {
...
dataBinding {
enabled = true
}
...
}
dependencies {
...
kapt "com.android.databinding:compiler:$plugin_version"
...
}
It's really important that the databinding compiler version and the plugin version are identical.
Also note that with kapt3, you shouldn't use the generateStubs flag anymore.
Old Answer
Having the Android Studio plugin enabled isn't enough, you also need to adjust your gradle files a little bit (add and apply the kotlin-gradle-plugin)
Here are excerpts of my gradle files with working Java and Kotlin Databinding
In your project build.gradle:
buildscript {
...
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.5"
classpath 'com.android.tools.build:gradle:2.2.3'
...
}
}
and in your app build.gradle:
apply plugin: "kotlin-android"
...
android {
...
dataBinding {
enabled = true
}
...
}
kapt {
generateStubs = true
}
dependencies {
...
kapt "com.android.databinding:compiler:2.2.0"
...
}
(I'm using a newer version of the databinding compiler here, you probably should do this as well)
Try to include the missing blocks in your gradle files with the help of the following reference gradle file source.
App level Build.Gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.adventure.abc"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin/com/dougritter/marvelmovies'
}
dataBinding {
enabled = true
}
}
kapt {
generateStubs = true
}
dependencies {
//Compatibility
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
kapt 'com.android.databinding:compiler:2.3.0'
//Libraries
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile project(':domain')
compile project(':androidutils')
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.jakewharton.timber:timber:4.5.1'
}
Project level Build.Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
You don’t need to include any extra libraries to enable view binding
remove kapt "androidx.lifecycle:lifecycle-compiler:2.2.0" and add
android {
..
buildFeatures {
viewBinding true
}
}
to build.gradle

Cannot invoke method version() on null object

i'm trying to use the Jgrapht library but it needs lambdas...
Here's my code:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
jcenter()
}
dependencies {
classpath "gradle.plugin.me.tatarka:gradle-retrolambda:3.3.0"
classpath 'com.android.tools.build:gradle:2.2.1'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And the :app
apply plugin: "me.tatarka.retrolambda" version "3.3.0"
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.3"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "it.univpm.gruppoids.iotforemergencyandnavigation"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
jackOptions {
enabled true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
compile 'org.jgrapht:jgrapht-core:1.0.0'
}
The error is: Error:(1, 0) Cannot invoke method version() on null object
Open File
Where and what is the error? thanks
You're mixing two kinds of syntaxes. Use either
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "gradle.plugin.me.tatarka:gradle-retrolambda:3.3.0"
}
}
apply plugin: 'me.tatarka.retrolambda'
or
plugins {
id "me.tatarka.retrolambda" version "3.3.0"
}
(gradle-retrolambda README), see also the gradle documentation.
I have the same issue.
You'll need to update a few things:
android/build.gradle:
i) classpath 'com.google.gms:google-services:4.0.1'
ii) Make sure google() appears above jcenter() in both buildscripts and allprojects
android/app/build.gradle:
Update Android libs to the latest specified here:
https://firebase.google.com/support/release-notes/android#20180523
if you have other libraries (for me, it's firebase) make sure to check build.gradle of the libraries, too.

Error:(3, 0) Cause: org/apache/commons/lang3/StringUtils

I get the following error
Error:(3, 0) Cause: org/apache/commons/lang3/StringUtils
when I try to add data binding in my Android project.
My dependencies include :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha7'
classpath 'me.tatarka:gradle-retrolambda:3.2.2'
classpath 'com.android.databinding:dataBinder:1.0-rc1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
My gradle wrapper is : distributionUrl=https://services.gradle.org/distributions/gradle-2.2.1-all.zip
My gradle file :
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.android.databinding'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.quizviz.workbook.myworkbook"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-jackson:2.0.0-beta3'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.0.1'
}
It took me a while to notice this problem is caused by google updating the way to use data binding library. You can see more information from here:
http://developer.android.com/tools/data-binding/guide.html.
You can just remove these two lines of code:
apply plugin: 'com.android.databinding'
And this one in buildscript's dependencies:
classpath 'com.android.databinding:dataBinder:1.0-rc1'
Then add the dataBinding section to your build.gradle like this.
buildscript {
...
}
android {
...
dataBinding {
enabled = true
}
...
}
dependencies {
...
}
Here you go. This works for me :).

Getting error in retrolambda error in android studio 1.0

I am working in android studio 1.0
the gradle file is showing this error:
Execution failed for task ':app:compileRetroLambdaDebug'
apply plugin: 'android'
apply plugin: 'retrolambda'
android {
compileSdkVersion 21
buildToolsVersion '19.1.0'
defaultConfig {
applicationId "xxxxxxxx"
minSdkVersion 10
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:21.0.2'
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'io.reactivex:rxandroid:0.23.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0-rc1'
classpath 'me.tatarka:gradle-retrolambda:2.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
what else changes I have to make in the gradle file. Please suggest something...
I had a similar error while updating the dependencies in eclipse. Try to update to the newest plugin versions and change retrolambda to me.tatarka.retrolambda, since the retrolambda plugin is deprecated and will be removed soon. At least it's what I did to fix the error.
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'android'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:2.5.0'
classpath 'com.android.tools.build:gradle:1.0.1'
}
}
...

Categories

Resources