I recently migrated my example project to androidx. But, after migration, it causes 'Render Problem' in the preview. The project runs, nevertheless, but it would be helpful to get the preview working again.
As stated in the title, the render problem is caused by "java.lang.IllegalArgumentException: java.lang.ClassCastException
at java.lang.reflect.Method.invoke". A snippet is provided below.
java.lang.IllegalArgumentException: java.lang.ClassCastException#1a8e42ac
at sun.reflect.GeneratedMethodAccessor990.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108)
at android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143)
at android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java)
at android.animation.PropertyValuesHolder.access$400(PropertyValuesHolder.java:38)
at android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387)
at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
at android.animation.ValueAnimator.setCurrentFraction(ValueAnimator.java:674)
at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:637)
at android.animation.ValueAnimator.start(ValueAnimator.java:1069)
at android.animation.ValueAnimator.start(ValueAnimator.java:1088)
at android.animation.ObjectAnimator.start(ObjectAnimator.java:852)
at android.animation.StateListAnimator.start(StateListAnimator.java:188)
at android.animation.StateListAnimator.setState(StateListAnimator.java:181)
at android.view.View.drawableStateChanged(View.java:21105)
at android.view.ViewGroup.drawableStateChanged(ViewGroup.java:7101)
at com.google.android.material.appbar.AppBarLayout.drawableStateChanged(AppBarLayout.java:393)
at android.view.View.refreshDrawableState(View.java:21160)
at android.view.View.dispatchAttachedToWindow(View.java:18379)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3397)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:42)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:335)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:391)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:195)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:540)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:666)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
This is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<merge>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="56dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tabs">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</merge>
This is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.materialdemo"
minSdkVersion 21
targetSdkVersion 28
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 'androidx.appcompat:appcompat:1.1.0-alpha05'
implementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
androidTestImplementation 'androidx.test:runner:1.2.0-beta01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-beta01'
implementation 'com.google.android.material:material:1.1.0-alpha06'
implementation 'androidx.constraintlayout:constraintlayout-solver:1.1.3'
implementation 'com.instabug.library:instabug:8.0.15.6-SNAPSHOT'
}
My project level build.gradle file:
// Top-level build file where you can add configuration options common
to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they
belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven{
url "https://oss.sonatype.org/content/repositories/snapshots"
}
maven {
url "https://jitpack.io"
}
maven {
url "https://dl.bintray.com/drummer-aidan/maven/"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Going back to implementation 'com.google.android.material:material:1.0.0' and invalidating the caches via File -> Invalidate Caches / Restart... works for me.
Also, any other theme like Theme.AppCompat.Light.NoActionBar works great, too.
Edit: I did not know that nearly no feature available in com.google.android.material:material:1.1.0-alpha06 is available in 1.0.0. So this answer is more or less useless.
This is the continuation of this question: Unable to merge dex - how to exclude proper jar?
I have not a typical android project, i am just working on the separate module, which is not connected with activies, fragments, UI etc. in my manifest i am not declaring any Android specifiacions. Here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moduleName.sampleName"
>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
</manifest>
i was having the exception:
Caused by: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
So, here is what my build.gradle file looks like now:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
packagingOptions {
exclude 'protobuf.meta'
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
sourceSets {
test.java.srcDirs += ['build/generated/source/apt/test/debug']
}
defaultConfig {
multiDexEnabled true
minSdkVersion 15
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'
}
}
}
ext.daggerVersion = '2.11'
ext.roomVersion = '1.0.0-rc1'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
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'
testImplementation "org.robolectric:robolectric:3.4.2"
kaptTest "com.google.dagger:dagger-compiler:$daggerVersion"
kaptAndroidTest "com.google.dagger:dagger-compiler:$daggerVersion"
// RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.1.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Room
implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.persistence.room:rxjava2:$roomVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"
androidTestImplementation "android.arch.persistence.room:testing:$roomVersion"
// Dagger
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
implementation 'pl.charmas.android:android-reactive-location2:2.0#aar'
implementation 'com.google.android.gms:play-services-location:11.4.2'
implementation 'com.google.android.gms:play-services-places:11.4.2'
//Jackson
implementation 'com.fasterxml.jackson.core:jackson-core:2.9.1'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.1'
implementation 'com.android.support:multidex:1.0.2'
}
My tests that are in the package 'test' are working fine.
But when i trying to run some test in my androidTest class package i get this:
Started running tests
Test running failed: Instrumentation run failed due to 'java.lang.NoClassDefFoundError'
Empty test suite.
To sum up:
The beginning of this problem is in the link above - i was having large amount of methods after adding jackson library, so i added dex support and this got me to this problem, which i really don't know how to solve - trying to exclude some depenendencies and leave only really important, trying to clean the project, deleting build package. None of them worked. If it is important i am using Kotlin in this module
You may have to add MultiDexApplication in the AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="com.example.myapp.MyApplication" >
...
</application>
If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:
public class MyApplication extends MultiDexApplication { ... }
Check this Google official blog here for MultiDex support
Below, is some source code which is being called from a non-static method. I'm getting the error
"Call requires API level 23 (current min is 15);
android.app.Fragment#getContext"
android.content.Context context = (Context) getContext();
How can I get the context object?
Under the object explorer > Gradle Scripts > build.gradle, I see this. Looks like version 23 to me. Am I looking in the correct location?
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.test.test"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}
Looks like I have Android Studio 1.5.1. I just chose the SettingsActivity project template in Android Studio. And added one class to that default code.
You need to import import android.support.v4.app.Fragment instead of import android.support.app.Fragment;
Pass getActivity() instead of getContext() as Context.
One way to solve was to cast "this" (activity) to (Context) inside the Activity onCreate method.
I am trying to try out Kotlin and the Kotlin Android extensions in Android Studio. I have tried this both in Android Studio v 1.5.1 on Ubuntu 14.04, and in Android Studio v 1.5.1 on OS X El Capitan with the same result.
Here is what I am doing:
I install the Kotlin plugin 1.0.0-beta-35950-IJ141-11
Create a new blank Android project
Convert the MainActivity file to Kotlin (via help->findaction->convert file to kotlin)
Configure the project for Kotlin
I then go into the generated content_main.xml file and add an id (hello) for the "Hello World!" TextView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.gmail.npnster.mykotlinfirstproject.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/hello"
/>
</RelativeLayout>
Then in the converted MainActivity I add a line to set the TextView. (shown below).
Android Studio then prompts me (via alt-enter) to insert this line (also shown below)
import kotlinx.android.synthetic.main.content_main.*
So at this point everything seems fine
but then when I try to compile this I get
Unresolved reference: kotlinx
Unresolved reference: kotlinx
Unresolved reference: hello
Notice that I did not install the Kotlin Android extensions plugin. As of a couple of days ago this is now supposed to be included in the main plug in and is marked as obsolete. (In fact if you try to install it when you have the latest plugin, nothing new is installed)
Anyone see what I am doing wrong?
MainActivity
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
print("setting text view value to hey")
hello.text = "hey"
val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener { view -> Snackbar.make(view, "Replace this with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show() }
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true
}
return super.onOptionsItemSelected(item)
}
}
Add kotlin-android-extensions in our buildscript's dependencies:
1. In your project-level build.gradle
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
and apply the kotlin-android-extensions plugin:
2. In your module-level build.gradle
apply plugin: 'kotlin-android-extensions'
When you use Android Studio 2.0 and kotlin 1.0.1-2, you will come up with the same wrong. You cann't configure kotlin android extensions in your project's build.gradle, you need to configure and kotlin android extensions in every Module's build.gradle like this:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
buildscript {
ext.kotlin_version = '1.0.1-2'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc2"
defaultConfig {
applicationId "com.dazd.kotlindemo"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Most importantly, even through the kotlin plugin included the kotlin android extension, you also need to configure the kotlin-android-extensions in your Module's bulid.gradle like this:
...
apply plugin: 'kotlin-android-extensions'
...
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
Of course, you could configure the kotlin plugin's classpath in your project's build.gradle.
I can't find it in the official documentation, but you must apply the plugin by adding the following to your build.gradle file:
apply plugin: 'kotlin-android-extensions'
The buildscript block containing the kotlin-android-extensions dependency apparently needs to be in the app-module build.gradle, not in the top-level one.
I found why mine didn't work. My blocks were misplaced and by moving the buildscript{} block before the plugins as follow I got it working:
buildscript {
ext.kotlin_version = '1.0.0-beta-3595'
ext.anko_version = '0.8.1'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.kotlin"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.anko:anko-sdk15:$anko_version"
compile "org.jetbrains.anko:anko-support-v4:$anko_version"
}
Removing the following import fixed the issue for me.
import android.R
add apply plugin: 'kotlin-android-extensions' in app/buildgradle.
if you have already added it, try to remove it and sync gradle, when sync is complete, then add it back and Sync Gradle again. This work for me.
The problem for me was the order in which I applied the plugins.
You must apply the kotlin-android plugin before the kotlin-android-extensions plugin
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
What worked for me in Android Studio 4.2 Beta 1 in macOS big sur to add id 'kotlin-android-extensions' to plugin section in app-level build.gradle file. So that it should look as follows.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
Then sync the project and it should work
With jetpack you have to use:
in the build.gradle (app)
buildFeatures {
dataBinding true
viewBinding true
}
plugins {
id 'kotlin-parcelize'
}
In activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
tools:viewBindingIgnore="false">
In MainActivity.kt
import com.example.appname.databinding.ActivityMainBinding // ActivityMain is for main_activity.xml, and so on for any other activity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.button.setOnClickListener {} // use binding.elementName
}
}
After applying the fixes mentioned above, I had to restart Android Studio to make it work.
If you are in the year 2021 then i guess most of the solutions you see here wont be of any help to you so try this instead
Go to your Module build.gradle file
add id 'kotlin-android-extensions' to the plugins object {}
Go to file the click on sync project with gradle files
Then go back and try to import kotlinx and that should work
In my case, adding:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
at the end of the module-level build.grade fixed the issue.
Had same issue
plugins {
id 'com.android.application'
id 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
}
removed apply plugin: to idand restarted
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id "kotlin-kapt"
}
This is how it worked for me.
When I first configured Kotlin in Project, I selected the 1.1.2-3 version instead of 1.1.2-4 and added the following line in the build.gradle app file
apply plugin: 'kotlin-android-extensions'
after that I synced the build and it worked as expected.
I found out that I had support for C++ and Kotlin at the same time which was causing build problems.
When starting a new project, ensure C++ support is unchecked, and Kotlin support is checked. That fixed the problem for me.
In my case, I had put the code referring the view in a companion object. How silly..
For me the only thing that helped was to click "File" -> "Invalidate caches / Restart..."
in my case android studio line separator changed to CRLF( I am using macbook) windows settings.
when switch to CR fix my error
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
For me I just had to add id 'kotlin-android-extensions' to plugins in build.gradle(Module:AppName.app)
And Sync it after that.
use this plug-in in your app project "Gradle script" folder in file "build.Gradle (Modules : progect_name)" :
"Kotlin-android-extensions"
then sync the projects.
I have just downloaded Android Studio, and when I try and create an Android Wear project it says:
Exception raised during rendering: android.support.wearable.view.WatchViewStub.requestApplyInsets()".
I've got no idea what this means or how I go about fixing it. My only guess was maybe SDK? This is what I've got:
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.helloworld.myapplication"
minSdkVersion 20
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:+'
compile 'com.google.android.gms:play-services-wearable:+'
}
And this is the error:
If anyone has any ideas I'd be most grateful!
Thanks
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="#layout/rect_activity_my"
app:roundLayout="#layout/round_activity_my"
tools:context=".MyActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>