Jetpack Compose and Hilt Conflict (NoSuchMethodError setContent) - android

I'm trying to develop Android application with Hilt and Jetpack Compose. Installed Gradle Hilt Plugin into Jetpack Compose application and getting an error.
Here are my gradle files:
build.gradle (Project)
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.0-alpha09'
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle (Module)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id "org.jetbrains.kotlin.kapt" version '1.5.30'
id 'dagger.hilt.android.plugin'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.ddmukhin.exampleapp"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
viewBinding true
compose true
}
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
}
composeOptions {
kotlinCompilerExtensionVersion "1.1.0-alpha02"
}
kapt {
correctErrorTypes true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
// Hilt
implementation 'com.google.dagger:hilt-android:2.38.1'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'com.google.dagger:hilt-compiler:2.38.1'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
// Lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0-alpha03"
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0-alpha03'
implementation "androidx.fragment:fragment-ktx:1.3.6"
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
// Compose
implementation 'androidx.compose.ui:ui:1.0.1'
implementation 'androidx.compose.ui:ui-tooling:1.0.1'
implementation 'androidx.compose.foundation:foundation:1.0.1'
implementation 'androidx.compose.material:material:1.0.1'
implementation 'androidx.compose.material:material-icons-core:1.0.1'
implementation 'androidx.compose.material:material-icons-extended:1.0.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07'
implementation "androidx.compose.compiler:compiler:1.1.0-alpha02"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
plugins {
id 'com.android.application' version '7.1.0-alpha09'
id 'com.android.library' version '7.1.0-alpha09'
id 'org.jetbrains.kotlin.android' version '1.5.30-RC'
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "ExampleApp"
include ':app'
My MainActivity class:
#AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent{
Greeting("Hello, world!")
}
}
#Composable
fun Greeting(text: String) {
Text(text = text, style = MaterialTheme.typography.h2)
}
}
And finally an error:
java.lang.NoSuchMethodError: No static method
setContent$default(Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/CompositionContext;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
in class Landroidx/activity/compose/ComponentActivityKt; or its super
classes (declaration of
'androidx.activity.compose.ComponentActivityKt' appears in
/data/app/com.ddmukhin.exampleapp-QlTRKYHfikMCdNcYBPcJIg==/base.apk)
at com.ddmukhin.exampleapp.MainActivity.onCreate(MainActivity.kt:34)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
P.S. I've seen Jetpack Compose and Hilt Conflict, but i've got another error + i did all steps mentioned in solution, but it didnt make sense.

Related

Dagger Hilt cannot create an instance of ViewModel having a constructor with arguments

After going through documentations and trying solutions of similar questions here on stackoverflow, nothing seems to fix the issue.
I tried altering all related dependencies, trying additional dependencies and tweaking the versions. Gradle builds successfully but there is a runtime error that through trial and error, the only method I found that could fix it was removing the argument from the ViewModel which is not desirable for me as the same setup used to work in my older projects.
RuntimeException
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 17527
java.lang.RuntimeException: Cannot create an instance of class com.example.ui.MainViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:188)
build.gradle(:app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'com.google.devtools.ksp' version '1.6.10-1.0.2'
}
kotlin {
sourceSets {
debug {
kotlin.srcDir("build/generated/ksp/debug/kotlin")
}
release {
kotlin.srcDir("build/generated/ksp/release/kotlin")
}
}
}
android {
compileSdk 31
defaultConfig {
multiDexEnabled = true
applicationId "com.example"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
freeCompilerArgs += '-Xopt-in=kotlin.RequiresOptIn'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha04'
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.activity:activity-compose:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
//lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0"
//Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
//Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
//DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0"
//Desugaring
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
//Compose Destinations
implementation "io.github.raamcosta.compose-destinations:core:1.2.1-beta"
ksp "io.github.raamcosta.compose-destinations:ksp:1.2.1-beta"
build.gradle(Project)
buildscript {
ext {
compose_version = '1.2.0-alpha02'
hilt_version = "2.40.5"
}
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.0-beta01' apply false
id 'com.android.library' version '7.2.0-beta01' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'org.jetbrains.kotlin.jvm' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
MainViewModel.kt
#HiltViewModel
class MainViewModel #Inject constructor(
private val dataStoreRepository: DataStoreRepository
) : ViewModel() {...}
MainActivity.kt
#AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val mainViewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DestinationsNavHost(navGraph = NavGraphs.root)
}
}
}
Did you create module class? like below:
#Module
#InstallIn(SingletonComponent::class)
class RepositoryModule {
#Provides
#Singleton
fun provideDataStoreRepository(): DataStoreRepository = DataStoreRepository()
}

Adding compose to buildFeatures gives NoSuchMethodError

The only changes I made were:
Added compose true in buildFeatures
Added composeOptions block
Added runtime dependency
apply plugin: 'com.android.application'
...
android {
compileSdk 31
defaultConfig {
applicationId "com.test.example"
...
}
buildFeatures {
dataBinding true
compose true
}
...
composeOptions {
kotlinCompilerExtensionVersion '1.0.4'
}
...
}
dependencies {
...
// Compose
implementation 'androidx.compose.runtime:runtime:1.0.4'
...
}
Here is the logcat:
java.lang.NoSuchMethodError: No virtual method Int$class-BusStop()I in class Lcom/example/test/data/LiveLiterals$DataClassesKt; or its super classes (declaration of 'com.example.test.data.LiveLiterals$DataClassesKt' appears in /data/app/com.example.test.debug-_m_f7seUKhs5ZD32lzD5Jw==/base.apk!classes15.dex)
at com.example.test.data.BusStop.<clinit>(Unknown Source:11)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at java.lang.Class.forName(Class.java:379)
at androidx.navigation.NavType$Companion.fromArgType(NavType.kt:146)
at androidx.navigation.NavInflater.inflateArgument(NavInflater.kt:165)
at androidx.navigation.NavInflater.inflateArgumentForBundle(NavInflater.kt:145)
at androidx.navigation.NavInflater.inflateAction(NavInflater.kt:316)
at androidx.navigation.NavInflater.inflate(NavInflater.kt:107)
at androidx.navigation.NavInflater.inflate(NavInflater.kt:114)
at androidx.navigation.NavInflater.inflate(NavInflater.kt:64)
at androidx.navigation.NavController.setGraph(NavController.kt:1026)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.kt:150)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2979)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:473)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:257)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1828)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1752)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1658)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:323)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.kt:158)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:53)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:295)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:274)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.example.test.activity.MainActivity.onCreate(MainActivity.kt:84)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-11-02 09:22:44.552 11192-11192/com.hmproductions.sgbuses.debug E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Edit: Here is full build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
android {
compileSdk 31
defaultConfig {
applicationId "com.example.test"
minSdk 24
targetSdk 31
versionCode 43
versionName "5.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
<keystore_password>
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
internal {
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
debuggable true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
minifyEnabled false
shrinkResources false
debuggable true
}
}
flavorDimensions "country"
productFlavors {
A {
dimension "country"
resConfigs("en", "X", "Y", "Z")
}
B {
dimension "country"
resConfigs("en", "something")
}
}
buildFeatures {
dataBinding true
compose true
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.4'
}
testOptions {
unitTests.includeAndroidResources = true
}
dynamicFeatures = [':dfsomething']
sourceSets {
A {
assets {
srcDirs 'src/A/assets'
}
}
B {
assets {
srcDirs 'src/B/assets'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.annotation:annotation:1.2.0'
// JUnit 4 and Mockito framework
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:2.19.1'
testImplementation 'androidx.test:core:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
// Compose
implementation 'androidx.compose.runtime:runtime:1.0.4'
// Support Libraries
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.browser:browser:1.3.0'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
// Lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
// Hilt
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"
implementation 'androidx.hilt:hilt-work:1.0.0'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
// Gson
implementation 'com.google.code.gson:gson:2.8.6'
// Room persistence library
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
implementation "androidx.room:room-ktx:2.3.0"
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.4'
implementation 'com.squareup.retrofit2:converter-gson:2.6.4'
implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.4.0-alpha10"
implementation "androidx.navigation:navigation-ui-ktx:2.4.0-alpha10"
implementation "androidx.navigation:navigation-dynamic-features-fragment:2.4.0-alpha10"
// Location
implementation "com.google.android.gms:play-services-location:17.1.0"
implementation "com.google.android.gms:play-services-maps:17.0.0"
// Firebase Analytics
implementation platform('com.google.firebase:firebase-bom:28.4.1')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-crashlytics-ktx'
implementation 'com.google.firebase:firebase-dynamic-module-support:16.0.0-beta01'
// Google Play Core
implementation 'com.google.android.play:core:1.10.2'
implementation 'com.google.android.play:core-ktx:1.8.1'
// Work Manager
implementation("androidx.work:work-runtime-ktx:2.7.0-beta01")
}
Some checks that I recommend you to do based on the official doc.
https://developer.android.com/jetpack/compose/interop/adding#setup
In project build.gradle file make sure you have the the correct version of the AGP and Kotlin.
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
...
}
In your app's build.gradle file check the for:
Min Sdk to 21
compile options to Java 8
Kotlin options to 1.8
Use the latest stable version of Android Studio ArticFox https://developer.android.com/studio
Your app's build.gradle file might looks like.
android {
compileSdk 31
defaultConfig {
applicationId "com.test.example"
minSdkVersion 21
...
}
buildFeatures {
dataBinding true
compose true
}
...
// Set both the Java and Kotlin compilers to target Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.4'
}
...
}
dependencies {
...
// Compose
implementation 'androidx.compose.runtime:runtime:1.0.4'
...
}

Cannot implement #Parcelize annotation

I am trying to implement #Parcelize annotation in kotlin to a data class, but it looks as if I forgot to implement something. Even when I try to import kotlinx.parcelize, Android Studio does not know the parcelize option. I implemented kotlin-parcelize and kotlin version is 1.4.21, which should be okay.
Here are my Gradle files:
plugins {
id 'com.android.application'
id 'kotlin-parcelize'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.benzeneapps.ciphers"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "0.0.1"
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
}
buildFeatures {
viewBinding true
dataBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.core:core-ktx:1.3.2"
implementation "androidx.room:room-ktx:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
androidTestImplementation "androidx.room:room-testing:2.2.6"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0"
}
repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
mavenCentral()
}
Project Gradle File
buildscript {
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
How I tried to implement it.
import kotlinx.parcelize.Parcelize
import android.os.Parcelable
#Parcelize
data class Cipher(val id: Int, val name: String): Parcelable
Apply kotlin-android before kotlin-parcelize:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
}
This works for me Add this to your build.gradle file
plugins {
id 'kotlin-android-extensions'
}

Unresolved refrence: requireLensFacing | Android Studio

I am getting 'Unresolved reference: requireLensFacing' for CameraSelector.Builder.requireLensFacing(lensFacing).build()
I have added camera core dependencies and I can actually open up the CameraSelector class and find the 'requireLensFacing' method inside it. But I just can't use it.
Here is my app gradle:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.devtyagi.facemaskdetector"
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'
}
buildFeatures{
mlModelBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation 'com.google.android.material:material:1.2.1'
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha20"
implementation "androidx.camera:camera-extensions:1.0.0-alpha20"
implementation "org.tensorflow:tensorflow-lite-metadata:${tflite}"
implementation "org.tensorflow:tensorflow-lite-gpu:2.2.0"
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0-rc1'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
And here is my Project Level gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.21"
ext.tflite = "0.1.0-rc1"
ext.camerax_version = "1.0.0-rc01"
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.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
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Here is the error:
Screenshot
How do I resolve this issue?
Thank You
You should use it like this val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()
you are using Builder replace it with Builder()

Getting problems when trying to setup Jetpack Compose Found interface rPluginContext

So, I'm trying to let jetpack compose run with a simple example, I have already updated my kotlin plugin to 1.4.0 and also updated all my build gradle with the jetpack compose documentation, but I got this error when compiling
java.lang.IncompatibleClassChangeError: Found interface
org.jetbrains.kotlin.backend.common.extensions.IrPluginContext, but
class was expected at
androidx.compose.plugins.kotlin.ComposeIrGenerationExtension.generate(ComposeIrGenerationExtension.kt:41)
I have also downloaded the canary version of Android studio
build.gradle project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.0-rc"
repositories {
google()
jcenter()
maven{
url "https://dl.bintray.com/kotlin/kotlin-eap/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.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
}
}
allprojects {
repositories {
google()
jcenter()
maven{
url "https://dl.bintray.com/kotlin/kotlin-eap/"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle app
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.jetpackcompose"
minSdkVersion 22
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'
}
}
buildFeatures {
// Enables Jetpack Compose for this module
compose true
}
// Set both the Java and Kotlin compilers to target Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
composeOptions {
kotlinCompilerVersion kotlin_version
kotlinCompilerExtensionVersion "0.1.0-dev13"
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.ui:ui-core:0.1.0-dev13'
implementation 'androidx.ui:ui-tooling:0.1.0-dev13'
implementation 'androidx.ui:ui-layout:0.1.0-dev13'
implementation 'androidx.ui:ui-material:0.1.0-dev13'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += ["-Xallow-jvm-ir-dependencies", "-Xskip-prerelease-check"]
}
}
But I cant compile the app, I'm trying to run the basic example
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent{
sayHello()
}
}
#Preview
#Composable
fun sayHello(){
Text("Hello World")
}
}
Does anyone knows why this error is thrown ?
Using Kotlin version 1.4.10 instead of 1.4.20 solved my problem.
Try to update Compose version to 0.1.0-dev16. Looks like 0.1.0-dev13 isn't compatible with Kotlin 1.4-rc

Categories

Resources