I have decided a few weeks ago to start converting my existing app (I wrote it in java) to kotlin.
I converted only one activity and I wanted to check the functionality before I will move forward and change all of my classes.
Unfortunately I'm getting the following error:
Unresolved reference: DaggerSearchComponent
This is how I resolve my dependencies with this activity, this code will be called from within the onCreate method:
SearchActivity.kt
override fun resolveDependency() {
DaggerSearchComponent.builder()
.applicationComponent(applicationComponent)
.searchModule(SearchModule(this))
.build().inject(this)
}
SearchComponent.java
#PerActivity
#Component(modules = SearchModule.class, dependencies = ApplicationComponent.class)
public interface SearchComponent {
void inject(SearchActivity activity);
}
The component of this activity is still written with java, although I tried to convert it with kotlin but I got the same error.
I added kotlin plugin in my build.gradle (Module:app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
build.gradle(project)
apply plugin: 'kotlin-kapt'
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
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 {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I did not find any other useful information except for adding kotlin plugin the app gradle file but it did not work for me.
When using dagger2 is it possible to use kotlin with java or should I need to convert every component/module into kotlin before testing it?
Make sure you added these dependencies in module gradle:
implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
implementation "com.google.dagger:dagger-android:$rootProject.dagger2Version"
implementation "com.google.dagger:dagger-android-support:$rootProject.dagger2Version"
kapt "com.google.dagger:dagger-android-processor:$rootProject.dagger2Version"
kapt "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"
And add
apply plugin: 'kotlin-kapt'
also to module level gradle.
Make changes in SearchComponent(Kotlin)
#PerActivity
#Component(modules = [SearchModule.class::class], dependencies = [ApplicationComponent::class])
interface SearchComponent {
fun inject(application: Application)
}
Related
I'm having a issue implement Hilt on android app...
The error is:
Expected #HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
Project gradle file
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
//Hilt
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
}
}
Model gradle file
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
dependencies {
def dagger_hilt_android = "2.38.1"
implementation("com.google.dagger:hilt-android:$dagger_hilt_android")
kapt("com.google.dagger:hilt-android-compiler:$dagger_hilt_android")
}
It is an issue with Kotlin-gradle-plugin: 1.5.20 as mentioned here https://github.com/google/dagger/issues/2684. Try upgrading your plugin to 1.5.21, and it will work.
Mohmd.h.bh,
Try applying this plugin in your build.gradle of your Android Gradle
apply plugin: 'com.google.dagger.hilt.android'
android {
// .......
}
I don't think you need to change the version of the kotlin-gradle-plugin
Regards
I was developing an App where I try to make some practices with Jetpack Compose, and I've tried to added Dagger Hilt as dependency injector too.
My problems comes, when I try to make use of #Singleton annotation is doesn'tfound, although the other clases of Dagger libraries works fine.
My project build gradle is the following:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And my module app build.gradle is this:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
....
dependecies{
...
// hilt
implementation "com.google.dagger:hilt-android:2.37"
implementation "androidx.hilt:hilt-common:1.0.0"
kapt "com.google.dagger:hilt-compiler:2.37"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation "androidx.hilt:hilt-navigation-fragment:1.0.0"
implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03"
//Inject and Singleton
implementation "javax.inject:javax.inject:1"
...
}
Finally the picture which show the error I get when I try to use #Singleton is this:
So what Am I missing?? Thanks in advance for the help !
Android Studio 3.4.2
in build.gradle:
buildscript {
ext.kotlin_version = '1.3.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
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
}
}
in app/build.gradle:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.29.0'
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
In my android project I have activity as java class - LoginActivity.java
import android.app.Activity;
import android.os.Bundle;
public class LoginActivity extends Activity{
private void testJavaMethod() {
// this ia a java code
String testJava = "Hello Java world";
}
private void testKotlinMethod() {
// this ia a Kotlin method
val testKotlin = "Hello Kotlin world"
}
In method testJavaMethod() success compile.
But testKotlinMethod() not compile.
Error in this line:
val testKotlin = "Hello Kotlin world"
error message:
Cannot resolve symbol 'val'
The question is:
Is it possible in Android project in JAVA file to has one method write on Java and another method write on Kotlin?
Is it possible in Android project in JAVA file to has one method write on Java and another method write on Kotlin?
No, sorry. A source file is either Java or Kotlin, not some mix of the two.
I am trying to use Object Library.
I read the official documentation and follow the instructions. But still, it not working.
The problem is when I try to initialize boxStore object I don't find MyObjectBox class.
val boxStore = MyObjectBox.builder().androidContext(this).build()
Here is my app module.
build.gradle (app module)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'
android {
compileSdkVersion 26
defaultConfig {
....
}
buildTypes {
release {
....
}
}
sourceSets {
main.java.srcDirs += 'src/main/java'
}
}
kapt {
generateStubs = true
arguments {
arg("objectbox.debug", true)
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
... other dependencies
//object box
implementation "io.objectbox:objectbox-android:$objectboxVersion"
// some useful Kotlin extension functions
implementation "io.objectbox:objectbox-kotlin:$objectboxVersion"
kapt "io.objectbox:objectbox-processor:$objectboxVersion"
}
And Here is my project module:
build.gradle (project)
buildscript {
ext.kotlin_version = '1.2.10'
//all version
ext.support_version = '26.1.0'
ext.objectboxVersion = '1.3.3'
repositories {
google()
jcenter()
maven { url "http://objectbox.net/beta-repo/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
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
//object box
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://maven.google.com' }
maven { url "http://objectbox.net/beta-repo/" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I am searching for the possible solution in several projects. I also follow the official demo app. But still, it not working for me?
Can anyone help me to fix this?
I think I finally found the solution: the important part is that you have to write the objectbox-gradle-plugin, above the kotlin-gradle-plugin in your project-level gradle file, the code looks like this :
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
After that, add the following lines under the other apply plugin:... in app-level gradle
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'
Most Importantly, you have to make at least one #Entity class and rebuild the project, and MyObjectBox will appear.
Your setup looks good. Note that you do not need to add the objectbox-android, objectbox-kotlin or objectbox-processor dependencies. The plugin will do it for you.
Do you have at least one #Entity class defined? For example:
#Entity
public class User {
#Id
private long id;
private String name;
}
Then Build > Make project. The annotation processor should pick up the entity and generate your MyObjectBox class.
I meet with the same question. My config is same with you.
Finally I found the reason.
#Entity
data class Person #JvmOverloads constructor(#Id var id:Long,
var name:String,
var age:Int,
var high:Int)
Like this. You must define one bean first. Thus MyObjectBox will generate.
Had also issues here. Had 3 entities, but the MyObjectBox class was not generated. I had to use the advanced kapt setting for Kotlin, which solved the problem:
kapt {
arguments {
arg("objectbox.modelPath", "$projectDir/schemas/objectbox.json")
}
}
Source: https://docs.objectbox.io/advanced/advanced-setup
I am using gradle and I am trying to add kotlin to my project. But when I am trying to add 'kotlin-android-extensions' plugin for gradle it fails to find it.
To use the plugin, you have to add it in your root level file:
buildscript {
repositories {
jcenter()
maven {
url 'https://maven.google.com'
}
}
ext.kotlin_version = '1.1.2-4'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Try to add the following to your gradle.build file
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
after that you have to make some changes in your dependencies classpath like this.
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
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
}
}
and in dependencies compile make sure to add jre7 such that.
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
After doing all that rebuild the gradle and you are all done. This worked for me because i was facing the same problem you are now, and after this it's all done. Hope it works for you tooo....
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:+"
try this