I am trying to add Jetpack Compose but the app crashes at launch when installed over a version without Compose with the following error logs:
E/[Koin]: Instance creation error : could not create instance for [Factory:'AppWidgetRealmDataSource']: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'MyFirstRealmTable.$stable' has been removed.
- Property 'MySecondRealmTable.$stable' has been removed.
- ...
- Property 'MyLastRealmTable.$stable' has been removed.
The app works just fine when it is installed for the first time.
The exception is thrown only if a previous version of the application without Compose was already installed.
Of course there were no changes in Realm tables in the version with Compose. So no Realm migration should be expected here.
The only thing I did is adding the Compose dependencies as stated in the Android docs :
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.0'
}
dependencies {
implementation 'androidx.activity:activity-compose:1.5.1'
implementation 'androidx.compose.material:material:1.2.1'
implementation 'androidx.compose.animation:animation:1.2.1'
implementation 'androidx.compose.ui:ui-tooling:1.2.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.2.1'
}
There is no error at compile time, only at run time.
Furthermore, when I remove compose true the app does not crash.
I really do not know what I am doing wrong and why a Realm error is thrown when I add Compose as the 2 libraries are not related at all.
I am using this for Realm: io.realm:realm-gradle-plugin:10.10.1.
Thanks
Related
I am trying to run android material design same taken from below https://github.com/material-components/material-components-android
but getting these errors :
failed
:lib:packageDebugResources
tokens.xml
Can't determine type for tag '<macro name="m3_comp_bottom_app_bar_container_color">?attr/colorSurface</macro>'
tokens.xml
Can't determine type for tag '<macro name="m3_sys_color_dark_surface_tint">?attr/colorPrimary</macro>'
fab_tokens.xml
Can't determine type for tag '<macro name="m3_comp_fab_primary_container_color">?attr/colorPrimaryContainer</macro>'
tokens.xml
Can't determine type for tag '<macro name="m3_comp_switch_selected_icon_color">?attr/colorOnPrimaryContainer</macro>'
tokens.xml
Can't determine type for tag '<macro name="m3_sys_motion_path">linear</macro>'
tokens.xml
Can't determine type for tag '<macro name="m3_sys_shape_corner_full_family">rounded</macro>'
tokens.xml
Can't determine type for tag '<macro name="m3_ref_typeface_brand_regular">sans-serif</macro>'
/Users/mac/AndroidStudioProjects/material-components-android/lib/java/com/google/android/material/bottomappbar/res/values/tokens.xml: Error: Can't determine type for tag '<macro name="m3_comp_bottom_app_bar_container_color">?attr/colorSurface</macro>'
I got the same error when upgrade to this version
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.8.0-alpha01'
But working fine in
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
Problem is solve after using current version of android studio ( updated from 4.2 to 2021.2.1 and used recommended gradle plugin )
Got the below reply from github when i open defect for same
"The error message you attached says not recognizing resource tag, which requires AGP 7.2. You must use a version later than 1.7.0-alpha02, please make sure your app build with the minimum required plugin versions."
updating the Android Gradle plugin to 7.3.3 (gradle-7.3.3-bin.zip)
things will be ok.
Replace :
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
with:
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
in build.gradle(:app) file under dependencies { }
Try downgrading material design library in build.gradle(app).
I have changed
implementation 'com.google.android.material:material:1.7.0'
to
implementation 'com.google.android.material:material:1.6.0'
And this resolved my issue.
This issue should be relevant to people who dig deeper and do not directly work with these dependencies, as it works fine for building and running apps in Android Studio.
It seems that material 1.7.0 is using illegal tag <macro>, you can see it in multiple files when looking through their release changes, specifically the **/tokens.xmlfiles. I could not find any information about this tag in AGP documentation and the only references to it I found from this git repo was this part of the code
private fun parseMacro(
element: StartElement, eventReader: XMLEventReader, parsedResource: ParsedResource): Boolean {
...
// Macros can only be defined in the default config
val defaultConfig = ConfigDescription()
if (parsedResource.config != defaultConfig) {
logError(
blameSource(source, element.location),
"<macro> tags cannot be declared in configurations other than the default configuration")
return false
}
So I'm not really sure how did they work around it.
In the future it is possible that AGP will support this tag externally and include it in their documentation, but for now you'll have to use workarounds
There is a project with compose.
There are a 2 modules in the project: core and othergames (core is depends on othergames).
I can successfully see a previews (annotated with #Preview) of compose views in core module. But when I try to see previews in othergames, preview can't be shown with error:
The following classes could not be instantiated:
- androidx.compose.ui.tooling.ComposeViewAdapter (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout.
There are details:
java.lang.NoSuchFieldError: view_tree_saved_state_registry_owner
at androidx.savedstate.ViewTreeSavedStateRegistryOwner.set(ViewTreeSavedStateRegistryOwner.java:53)
at androidx.compose.ui.tooling.ComposeViewAdapter.init(ComposeViewAdapter.kt:666)
at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:217)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:373)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:192)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:150)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:302)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:727)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:883)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Both of modules has a equal compose dependencies and settings in build.gradle files:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0-alpha03'
}
And:
dependencies {
...
implementation "androidx.compose.ui:ui:1.2.0-alpha03"
implementation "androidx.compose.ui:ui-tooling:1.2.0-alpha03"
implementation "androidx.compose.ui:ui-tooling-preview:1.2.0-alpha03"
implementation "androidx.compose.foundation:foundation:1.2.0-alpha03"
implementation "androidx.compose.foundation:foundation-layout:1.2.0-alpha03"
implementation "androidx.compose.material:material:1.2.0-alpha03"
implementation "androidx.compose.material:material-icons-core:1.2.0-alpha03"
implementation "androidx.compose.material:material-icons-extended:1.2.0-alpha03"
implementation "androidx.compose.animation:animation:1.2.0-alpha03"
implementation "androidx.compose.animation:animation-core:1.2.0-alpha03"
implementation "androidx.compose.animation:animation-graphics:1.2.0-alpha03"
implementation "androidx.compose.runtime:runtime-livedata:1.2.0-alpha03"
implementation "androidx.compose.compiler:compiler:1.2.0-alpha03"
...
Why I can't see previews in othergames module? How to solve it?
I found a solution to fix it. In module where previews broke I added library which I removed recently:
implementation 'androidx.appcompat:appcompat:1.4.1'
Once I did it, previews appeared again. I not sure this library is real reason, because core module doesn't contains this library, but still show preview.
UPDATE:
Time passed and I ran into this problem again. Now it is not solved by adding the library I mentioned above. I still don't know what to do.
Came across the exact issue earlier. Make sure everything is up to date, this includes:
Android Studio ( Help > Check for updates)
Jetpack Compose (project level build)
Kotlin (project level build)
Other than that, the compose-tooling dependency should have the same version as that of Compose itself.
Here's what solved my issue
#Preview(showSystemUi = true, showBackground = true) // Apparently, adding these two lines seems to do the magic here
#Composable
fun MyComposable(){
...
}
Well, that should be pretty much it.
Have a look at Studio BumbleBee "Render Problem" For Compose Preview
Whenever you see a "try to build the project" error, always check the gradle implementations first.
Then, move on to whether your version numbers are correct.
Alpha versions aren't always the best choices.
So, if you can't find an error in the implementations, roll back any error associated implementation to it's previous stable release.
Then check again.
Also, maybe try and create a new android studio project and check if the core implementations in both the new and your current are similar or, at least look ok for the common ones.
If they look ok, add a comment besides all the implementations that look ok. Then focus on the other implementations.
Now, assuming your error is the same, i.e. a combination of try to build the project and is associated with compose, check all your ui based implementations and, your jetpack compose implementations if you are using those. Hope it helps.
I upgraded to Jetpack Compose 1.0.0-alpha12 and started to run into issues.
Firstly, the setContent method I was using showed as deprecated.
From the Alpha 12 release notes, I noticed that it said:
ComponentActivity.setContent has moved to androidx.activity.compose.setContent in the androidx.activity:activity-compose module. (Icf416)
So I removed my import androidx.compose.ui.platform.setContent and switched it to import androidx.activity.compose.setContent, which removed the deprecation.
However, I then got an error that says:
w: Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies
w: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
e: Classes compiled by an unstable version of the Kotlin compiler were found in dependencies.
Remove them from the classpath or use '-Xallow-unstable-dependencies' to suppress errors
e: /[my path]/MainActivity.kt: (39, 9): Class 'androidx.activity.compose.ComponentActivityKt' is
compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler
And again, I was able to work around that by changing my build.gradle file to have:
kotlinOptions {
jvmTarget = '1.8'
useIR = true
// I added this line
freeCompilerArgs += "-Xallow-unstable-dependencies"
}
While that let me compile my app, I now get the following exception at runtime:
java.lang.NoSuchMethodError: No static method setContent(
Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/Com
positionContext;Lkotlin/jvm/functions/Function2;)V in class
Landroidx/activity/compose/ComponentActivityKt; or its super classes
(declaration of 'androidx.activity.compose.ComponentActivityKt' appears in [my apk]
How can I fix this and upgrade my app to Jetpack Compose 1.0.0-alpha12?
As per this issue, this issue is related to the new androidx.activity:activity-compose:1.3.0-alpha01 artifact.
From that issue:
Activity 1.3.0-alpha02 has been released and fixes this issue.
Apps using Compose alpha12 and specifically artifacts like androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 that internally use setContent should add the activity-compose:1.3.0-alpha02 dependency to their dependencies block to ensure that the 1.3.0-alpha01 artifact is not used
So to fix your app, you should:
Remove the freeCompilerArgs += "-Xallow-unstable-dependencies" line from the build.gradle file (as it is no longer needed)
Add a specific dependency on Activity Compose 1.3.0-alpha02:
implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
By adding that dependency, any direct usages of setContent as well as internal usages by androidx.compose.ui:ui-tooling:1.0.0-alpha12 or androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 will use the fixed Activity Compose 1.3.0-alpha02 release.
With Activity 1.3.0-alpha02, setContent is working, but got another error.
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'META-INF/AL2.0' from inputs:
Had to use the workaround to make it built
packagingOptions {
exclude 'META-INF/AL2.0'
exclude 'META-INF/LGPL2.1'
}
And still have the warning
Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies
I have these dependencies in my build.gradle.kts file in share module.
val coroutinesVersion = "1.3.9-native-mt"
val serializationVersion = "1.0.1"
val ktorVersion = "1.4.2"
val sqlDelightVersion = "1.4.4"
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
implementation("com.squareup.sqldelight:coroutines-extensions:$sqlDelightVersion")
}
}
...
}
When I run the app everything works fine in android app. But I get a runtime crash when running the ios app. In logs, I see that Ktor is complaining about coroutines version not being native-mt. Which I don't understand why because 1.4.# version of coroutines don't have separate native multithreaded branches.
I looked at the External Libraries folder and found that coroutines version in my case was set to 1.3.9 all the time. If I remove com.squareup.sqldelight:coroutines-extensions Everything works just fine again. But I need that dependency to consume Flow from db.
I tried excluding coroutines from sqldelight extension but it didn't compile in Xcode build.
implementation("com.squareup.sqldelight:coroutines-extensions:$sqlDelightVersion") {
exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core")
}
So my questions are:
Why does SQLDelight override kotlinx-coroutines-core version?
Why does Ktor want kotlinx-coroutines-core versions only with native-mt suffix?
How can I solve this versions problem?
1 - That's the version SQLdelight uses. Gradle made a decision to pull in that one vs what ktor wants.
2 - Ktor now needs the multithreaded version.
3 - Solutions
a - Force the version ktor wants. Define the coroutines dependency with version and strictly:
implementation(Deps.Coroutines.common) {
version {
strictly(Versions.coroutines)
}
}
b - The Coroutines extension is one file. We need to update KaMPKit and remove this, but we had a version copied into the source to avoid earlier issues with SQLDelight: https://github.com/touchlab/KaMPKit/blob/master/shared/src/commonMain/kotlin/co/touchlab/kampkit/sqldelight/CoroutinesExtensions.kt
c - SQLDelight needs a version bump. I can hack away at that over the next few days, but not sure when that will be ready. You can just wait for that?
I have used Data binding in my existing code and now I am migrating to Room for persistence.
I have followed the steps mentioned in Florina's Blog for room
My Code builds fine without java code error or BR related error when I remove room dependency
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
and its runs too, but gives Runtime exception saying database_Impl does not exists. As it couldn't generate that's file.
But after I put Annotation processor back, it give me
Error:(29, 37) error: cannot find symbol class BR
My gradle plugin used is com.android.tools.build:gradle:3.0.1
They both don't seem to work together
Steps taken so far:
Changed BaseObservable to Observable As suggested here
Updated Android Studio to 3.0.1
Tried using gradle latest plugin canary 6
Clear, Clear Cache also done
Has anyone used Room and Data binding together ?
After 4 days of efforts I finally made my code run properly.
Steps to solve the
Data binding error like
error: package com.packagename.databinding does not exist
error: cannot find symbol class CustomMainActivityBinding
The app gradle must have below code added in order to view more than 100 errors that come by default
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xmaxerrs" << "4000"
options.compilerArgs << "-Xmaxwarns" << "4000"
}
}
}
Gradle dependencies for data binding and Room arch components
annotationProcessor 'com.android.databinding:compiler:3.0.1'
implementation 'android.arch.lifecycle:extensions:1.0.0'
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
Note: Gradle plugin version is 3.0.1
I changed my all VMs to implement Observable and call
registry.notifyChange(this, BR.bar);
in case of notify change and also implement overridden methods
#Override
public void addOnPropertyChangedCallback(OnPropertyChangedCallback
callback) {
registry.add(callback);
}
#Override
public void removeOnPropertyChangedCallback(
OnPropertyChangedCallback callback) {
registry.remove(callback);
}
These things made my code Build, but it run without exceptions when I solved the Room query related errors. Which was the main reason, code was building but not running. These errors I could see when I Rebuid my project again.
UPDATE:
After Android studio 3.1.3, Message window is gone and now all build error appears under Build view. Although there is toggle available to get textview response of error, for data-binding errors it isn't sufficient.
Solution that helped me:
In Command promt/Terminal navigate to project root.
Run this command "./gradlew build --stacktrace" if Mac or ".\gradlew
build --stacktrace" if Windows.
Now search for "error:" tag and the compile time errors will show up.
I couldn't get these errors in IDE.
In my experience, the following reasons may cause the error:
Incorrect getter/setter (missing or incorrect name)
Incorrect return type in Dao
annotationProcessor or kapt issues
The default constructor is overridden (you need to keep the empty constructor for Room)
I faced this issue while adding room dependencies. Add this in the below way to resolve the error.
def roomVersion = "2.0.0-rc01"
implementation "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomVersion"