I'm creating a new Android project and I adding Dagger 2, but it fails with an error related to androidx libraries.
After using a template from within Android Studio, I've added a few lines, kotlin-kapt, and the dependencies for Dagger 2.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.xxxx.yyyy"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
androidExtensions {
experimental = true
}
kapt {
generateStubs = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
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 'com.google.android.material:material:1.0.0-alpha1'
// Image Loading
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
// Dagger
implementation 'com.google.dagger:dagger-android:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'
// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.3'
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
// Navigation
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha01'
// Pretty Logger
implementation 'com.orhanobut:logger:1.15'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha1'
}
When trying to build the application, it fails with the following errors.
error: cannot find symbol
public final class MainActivityViewModel extends android.arch.lifecycle.ViewModel {
^
symbol: class ViewModel
location: package android.arch.lifecycle
error: cannot find symbol
private final androidx.lifecycle.MediatorLiveData
^
symbol: class MediatorLiveData
location: package androidx.lifecycle
error: cannot find symbol
private androidx.navigation.NavController navController;
^
symbol: class NavController
location: package androidx.navigation
...
I've tried with androidx libraries, and the older libraries but both fail. If I removed the Dagger 2 dependencies it will build and run fine.
I'm currently using Android Studio 3.2 Canary 17.
Any idea what's going wrong?
Thanks.
Go to Settings -> Build, Execution, Deployment -> Compiler,
uncheck the 'Configure on demand' option if set.
Also make sure you are using latest gradle and Android Gradle plugin
I managed to track down the cause of the issue, it wasn't related to the build.gradle itself but the version of Java I was using.
Within Android Studios I have changed the JDK to use Java 10.
However, changing this setting back to using the embedded JDK has made the application compile successful.
Related
After updating my Android Studio Canary version to 3.1, I started receiving Kotlin unresolved references for functions belonging to the kotlin standard library, and the issue also seems to be affecting Android Studio's ability to import the correct library.
I believe my issue is similar to this. Per a recent comment, changing my gradle kotlin version to 1.5.0 fixed the "unresolved reference" issue, but compose beta06 does not support 1.5.0 yet.
I was wondering if anybody had any luck solving this. I believe I have tried to update as many of my dependencies as well as clean building, invalidating cache and restarting.
Here is my gradle file.
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.leetcards"
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'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.4.32'
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
dependencies {
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.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.0-alpha07'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
// Navigation
def nav_version = "1.0.0-alpha10"
implementation "androidx.navigation:navigation-compose:$nav_version"
// Accompanist
def accompanist_version = "0.9.0"
// implementation "com.google.accompanist:accompanist-coil:$accompanist_version"
// Networking (Retrofit and Moshi)
def retrofit_version = "2.9.0"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
def moshi_kotlin_version = "1.12.0"
def moshi_converter_version = "2.9.0"
implementation "com.squareup.moshi:moshi-kotlin:$moshi_kotlin_version"
implementation "com.squareup.retrofit2:converter-moshi:$moshi_converter_version"
// Firebase
implementation platform('com.google.firebase:firebase-bom:27.1.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.firebaseui:firebase-ui-auth:7.1.1'
implementation 'com.facebook.android:facebook-android-sdk:4.x'
// Facebook login
implementation 'com.facebook.android:facebook-android-sdk:[5,6)'
// CameraX core library using the camera2 implementation
def camerax_version = "1.0.0"
// The following line is optional, as the core library is included indirectly by camera-camera2
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
// If you want to additionally use the CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
// If you want to additionally use the CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha24"
}
I too have the same problem with Compose project in Android Studio after upgrade to Kotlin 1.5.0. Unresolved references seem to come from the mismatch between Kotlin version defined in project gradle files and Kotlin version bundled with Kotlin plugin. There is some old thread on SO describing similar issue in IntelliJ: link
Summarizing, there are 2 conflicting situations:
Switching to Kotlin 1.5.0 throws error about Jetpack Compose incompatibility;
Downgrading to Kotlin 1.4.32 allows to compile and install the app, however problem with unresolved references arises. Kotlin plugin cannot be easily downgraded (or can it?).
As a temporary measure I am switching between two Kotlin versions, in hope of finding better solution.
I am implementing OCR results in android using kotlin language by this tutorial
I am unable to solve this ActivityCompat error
import android.support.v4.app.ActivityCompat
My Gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.ocrofflinesample"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
// implementation 'androidx.support:appcompat-v7:'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.gms:play-services-vision:15.0.1'
implementation "org.jetbrains.anko:anko:0.10.4"
// Logger
implementation 'com.orhanobut:logger:2.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
How to resolve this `ActivityCompat error`?
You have to use Androidx dependency. Change the android.support.v4.app.ActivityCompat to androidx.core.app.ActivityCompat
Check this out for pre-Androidx to Androidx dependencies mapping.
You might have either used AndroidX while creating project, or you have used AndroidX dependencies in non - androidX artifact.
Either you need to migrate it to androidX as suggested above.
Also, if you have used app:layout_scrollFlags in layouts, you
might wanna check out the scroll constants after refactor because
recently when I refactored, I didn't get any build error but when the
activity with scrollFlags loaded, it crashed and upon finding
out, it was a refactor issue due to that it was unable to change
constant names as per AndroidX.
The app works fine with minSdkVersion 24 but upon reducing it to minSdkVersion 19 I am getting few errors. On running it in my device but there is no error upon gradle sync.The error log is posted. The dependencies work well with minSdkVersion 24, but not in minSdkVersion 19 , getting me the below listed errors and I need it to work on the lower version like minSdkVersion 19. Any help would be appreciated.
ERROR LOG HERE:
4 errors ,1 warning
Calculate task graph 122ms
Run tasks 2m 7s 746ms null
Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
com.android.builder.dexing.DexArchiveBuilderException: Failed to process C:\Users\hp\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common-java8\1.1.1\795d68cd761d093ccb235d1d91b8fd17c2ae25ff\common-java8-1.1.1.jar
com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.
com.android.tools.r8.CompilationFailedException: Compilation failed to complete
com.android.tools.r8.utils.AbortException
null
Default interface methods are only supported starting with Android N (--min-api 24): void android.arch.lifecycle.DefaultLifecycleObserver.onCreate(android.arch.lifecycle.LifecycleOwner)
My app level gradle file.(minSdkversion 24 here)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.hp.mkchat"
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
multiDexEnabled true
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 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-core:11.4.2'
implementation 'com.google.firebase:firebase-auth:11.4.2'
implementation 'com.android.support:support-v4:27.1.1-beta1'
implementation 'com.google.firebase:firebase-database:11.4.2'
implementation 'com.google.firebase:firebase-storage:11.4.2'
implementation 'com.google.firebase:firebase-messaging:11.4.2'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp:okhttp:2.5.0'
implementation 'com.firebaseui:firebase-ui-database:3.1.0'
implementation 'id.zelory:compressor:2.1.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:1.1.1" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:1.1.1"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:1.1.1"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:1.1.1"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.1"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}
apply plugin: 'com.google.gms.google-services'
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Check if your source compatibility & target compatibility is not Java-8. Set that to Java-8.
Removing these lines from the gradle helped remove the errors and run successfully on my phone for making it run in below android version 7
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:1.1.1" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:1.1.1"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:1.1.1"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:1.1.1"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.1"
i can't fix it by all the answer above, but i figured out the solution. I remove library appcompat v4 from my libs project folder.
yourprojectfolder > app > libs > appcompat...rar
delete that and restart your android studio.
but when I build it gives me an error
recently android updated for kotlin
and my android gradle vesion is 3.1.2
and kotlin version is 1.2.41
android studio 3.1.2
i'm targeting android p
this is latest version so i can't find more documentation on this problem and
please help me to find out this problem
android issues
Program type already present: android.support.v4.media.MediaBrowserCompat$ConnectionCallback$StubApi21
build.gradle(app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 'android-P'
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.xxxxxxx.xx"
minSdkVersion 18
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.anko:anko-commons:0.10.4'
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
implementation "androidx.appcompat:appcompat:1.0.0-alpha1"
implementation "androidx.legacy:legacy-support-v4:1.0.0-alpha1"
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation "com.google.android.material:material:1.0.0-alpha1"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha1'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
//firebase massaging
implementation 'com.google.firebase:firebase-messaging:15.0.2'
implementation 'com.google.firebase:firebase-core:15.0.2'
def lifecycle_version = "2.0.0-alpha1"
def room_version = "2.0.0-alpha1"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// room database persistent
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
//data binding
//kapt 'com.android.databinding:compiler:3.1.2'
}
apply plugin: 'com.google.gms.google-services'
please help me what is problem I can't find out
i removed Androidx from my whole project but isue still parsist then
i found solution and replace
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
to
implementation 'androidx.core:core-ktx:0.3'
and my problem is solved.
anyway thanks all second-time I'm going to use androidx (Android + Kotlin)
I'm waiting for jetpack stable version.
Actually this is a bug in Canary 14. You should wait for Canary 15 ;-)
Check out this session.
have you tried downloading android studio 3.2 canary 14? there is a new option to convert your files to androidX and will give you a preview of what it's going to do. Back up your files or at least use git to make sure it's safe.
thanks for the edit paul
This question already has answers here:
Unresolved reference DaggerApplicationComponent
(16 answers)
Closed 4 years ago.
I've been facing this problem for 3 hours. I couldn't find any logical reason to what is happening. The problem is, Only when I use the generated Dagger classes, and after Gradle do the Build every reference to these classes became red and I get the error:
Error: Unresolved reference: DaggerAppComponent
But, If I comment out every line in the App class reference any of these generated Dagger classes. The build process completed successfully. I tried doing clean/ rebuild/ make project. but it doesn't work.
Some information about my environment:
Build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "xxxxx"
minSdkVersion 19
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'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:viewmodel:1.1.0"
implementation "android.arch.lifecycle:livedata:1.1.0"
implementation "android.arch.persistence.room:runtime:1.0.0"
implementation "android.arch.lifecycle:runtime:1.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
kapt "android.arch.lifecycle:compiler:1.1.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
kapt "com.google.dagger:dagger-compiler:2.8"
compile "com.google.dagger:dagger:2.8"
compile "com.google.dagger:dagger-android:2.8"
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'
}
Kotlin version: ext.kotlin_version = '1.1.51'
Android Studio version: 3.0.1
dependencies section in Build.Gradle file for the project (not the app module):
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
}
App class:
class App:Application() {
private val component:AppComponent by lazy {
DaggerAppComponent.builder()
.build()
}
override fun onCreate() {
super.onCreate()
component.inject(this)
}
}
If anyone can figure out what is the problem, it would be much appreciated.
P.S. I'm still learning Dagger2 and I'm new to Kotlin and Native development in general. I came from C# and Xamarin background.
You seem to be missing the application of the kapt Gradle plugin, as described in the kapt documentation. Basically, add this after your existing plugins:
apply plugin: 'kotlin-kapt'
(If this helps, this question should be marked as a duplicate of this one)