How to resolve Manifest merger Failed error - android-studio

I am getting manifest merger failed error when i add elegeant number button library to my gradle files.
Manifest merger failed : Attribute application#icon
value=(#mipmap/app_logoo) from AndroidManifest.xml:20:9-41 is also
present at [com.cepheuen.elegant-number-button:lib:1.0.3]
AndroidManifest.xml:13:9-43 value=(#mipmap/ic_launcher). Suggestion:
add 'tools:replace="android:icon"' to element at
AndroidManifest.xml:18:5-56:19 to override.
I have tried adding all the versions of this library but none works

As the suggestion in error says, you need to add tools:replace="android:icon" to your AndroidManifest.xml. tools:replace means you want to replace the attribute value with your current attribute value in the AndroidManifest.xml.
You can do it something like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.yourapp">
<application
android:name=".YourApp"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
tools:replace="android:icon">
<!-- code omitted here -->
</application>
</manifest>

NOTE : Add this to your build.gradle and make sure you are using the same versions as '26.0.2' -
configurations.all {
resolutionStrategy.eachDependency {
DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '26.0.2'
}
}
}
}

Related

Animated-selector causes crash on view initialization

My goal is to create a custom switch including animated transitions between checked and unchecked state.
I easily achieved a default state and my switch looks as expected in both states and it changes its look when its state is changed.
Problem arises when I attach animated-selector instead of regular one.
I'm gettting an error like this:
android.view.InflateException: Binary XML file line #10: Binary XML
file line #10: Error inflating class android.widget.Switch
Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/switch_track_animation_unchecked_checked.xml from
drawable resource ID #0x7f060072
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)
Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/switch_track_animated_selector.xml from drawable resource
ID #0x7f060070
I can assure that I'm properly accessing those drawables etc.
I went through invalidate caches and restart, clean build, rebuild etc. and it won't work.
View sample
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/switch_thumb_selector"
android:track="#drawable/switch_track_animated_selector"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:checked="true"
android:id="#+id/switch2"/>
switch_track_animated_selector
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/checked"
android:drawable="#drawable/switch_bckg_on"
android:state_checked="true"/>
<item android:id="#+id/unchecked"
android:drawable="#drawable/switch_bckg_off"
android:state_checked="false"/>
<transition
android:fromId="#+id/unchecked"
android:toId="#+id/checked"
android:drawable="#drawable/switch_track_animation_unchecked_checked"/>
<transition
android:fromId="#+id/checked"
android:toId="#+id/unchecked"
android:drawable="#drawable/switch_track_animation_checked_unchecked"/>
</animated-selector>
switch_bckg_on.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00B05A"/>
<stroke android:width="1dp" android:color="#00B05A" />
<corners android:radius="180dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
switch_bckg_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#F1F1F1"/>
<stroke android:width="1dp" android:color="#E0E0E0" />
<corners android:radius="180dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
switch_track_animation_checked_unchecked.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:drawable="#drawable/switch_bckg_on">
<target android:name="android:drawable">
<aapt:attr name="android:animation">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:interpolator="#android:interpolator/accelerate_decelerate"
android:propertyName="strokeColor"
android:valueFrom="#A0A0A0"
android:valueTo="#1E9618"
android:valueType="intType" />
</aapt:attr>
</target>
</animated-vector>
switch_track_animation_unchecked_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:drawable="#drawable/switch_bckg_off">
<target android:name="android:drawable">
<aapt:attr name="android:animation">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:interpolator="#android:interpolator/accelerate_decelerate"
android:propertyName="strokeColor"
android:valueFrom="#A0A0A0"
android:valueTo="#1E9618"
android:valueType="intType"/>
</aapt:attr>
</target>
</animated-vector>
I also checked above approach using vector drawable instead of shape and I had the same result.
Example of vectors
ic_thumb_vector_off
<vector android:height="33dp" android:viewportHeight="300"
android:viewportWidth="300" android:width="33dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:name="background_off" android:fillColor="#FFFFFF" android:fillType="nonZero"
android:pathData="M149.926,149.926m-149.176,0a149.176,149.176 0,1 1,298.352 0a149.176,149.176 0,1 1,-298.352 0"
android:strokeAlpha="0.1" android:strokeColor="#000000" android:strokeWidth="0.5"/>
<path android:name="icon_off" android:fillColor="#9B9B9B" android:fillType="evenOdd"
android:pathData="M166.88,146.104C166.13,145.354 166.13,144.228 166.88,143.478L195.019,115.339C195.77,114.588 196.145,113.463 196.145,112.712C196.145,111.962 195.77,110.836 195.019,110.086L189.767,104.833C189.016,104.083 187.891,103.708 187.14,103.708C186.015,103.708 185.264,104.083 184.514,104.833L156.375,132.972C155.624,133.723 154.499,133.723 153.748,132.972L125.609,104.833C124.859,104.083 123.733,103.708 122.983,103.708C122.233,103.708 121.107,104.083 120.357,104.833L115.104,110.086C114.354,110.836 113.979,111.962 113.979,112.712C113.979,113.463 114.354,114.588 115.104,115.339L143.243,143.478C143.994,144.228 143.994,145.354 143.243,146.104L115.104,174.243C114.354,174.993 113.979,176.119 113.979,176.869C113.979,177.62 114.354,178.745 115.104,179.496L120.357,184.748C121.107,185.499 122.233,185.874 122.983,185.874C123.733,185.874 124.859,185.499 125.609,184.748L153.748,156.609C154.499,155.859 155.624,155.859 156.375,156.609L184.514,184.748C185.264,185.499 186.39,185.874 187.14,185.874C187.891,185.874 189.016,185.499 189.767,184.748L195.019,179.496C195.77,178.745 196.145,177.62 196.145,176.869C196.145,176.119 195.77,174.993 195.019,174.243L166.88,146.104Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
ic_thumb_vector_on
<vector android:height="33dp" android:viewportHeight="302"
android:viewportWidth="302" android:width="33dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:name="background_on" android:fillColor="#FFFFFF" android:fillType="nonZero"
android:pathData="M151,151m-150.25,0a150.25,150.25 0,1 1,300.5 0a150.25,150.25 0,1 1,-300.5 0"
android:strokeAlpha="0.1" android:strokeColor="#000000" android:strokeWidth="0.5"/>
<path android:name="icon_on" android:fillColor="#00B05A" android:fillType="evenOdd"
android:pathData="M105.523,153.198C104.806,152.5 104.448,151.453 104.448,150.754C104.448,150.056 104.806,149.009 105.523,148.31L110.536,143.422C111.968,142.026 114.117,142.026 115.549,143.422L115.907,143.772L135.602,164.371C136.318,165.069 137.393,165.069 138.109,164.371L186.093,115.841L186.451,115.841C187.883,114.444 190.032,114.444 191.464,115.841L196.477,120.728C197.91,122.125 197.91,124.22 196.477,125.616L139.183,183.573C138.467,184.272 137.751,184.621 136.676,184.621C135.602,184.621 134.886,184.272 134.17,183.573L106.239,154.246L105.523,153.198Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
Maybe I/m missing something in gradle file?
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "...customswitch"
minSdkVersion 22
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"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
I expect this view to load and not to throw an error on view init.
Your issue is that switch_track_animation_unchecked_checked.xml and switch_track_animation_checked_unchecked.xml are animated VectorDrawables, and these animate static VectorDrawable states. You are trying to animate ShapeDrawable, and not VectorDrawable and that's what is causing the exception, I think.
You need to replace switch_bckg_on.xml and switch_bckg_off.xml with <vector>...</vector> implementations to animate them from within an <animated-vector>...</animated-vector> block.
Alternatively you could replace the AnimatedVectorDrawables with StateListAnimator, although I'm not sure that will actually be able to do what you're trying to achieve here.
It seems like your switch_track_animated_selector does not have some default state without any id. You may try to add this state. This helped me in a similar situation

java.lang.IllegalArgumentException: java.lang.ClassCastException after migrating to androidx in Android Studio

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.

Error processing / merging manifest

I get this error
W:\android-studio-projects\sharedid\app\build\intermediates\merged_manifests\flavor_customer1Debug\processFlavor_customer1DebugManifest\merged\AndroidManifest.xml:49:
error: resource string/MyAppName (aka
com.customer1.app:string/MyAppName) not found.
error: failed processing manifest.
...
My gradle contains this
flavor_customer1 {
java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
manifest.srcFile "W:/android-studio-projects/sharedid/app/src/customer1/AndroidManifest.xml"
assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/customer1/assets/"]
resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/customer1/res/"]
}
I have defined MyAppName in file
"W:/android-studio-projects/sharedid/app/src/customer1/res/values/strings_specific.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="MyAppName">Customer 1</string>
</resources>
In "W:/android-studio-projects/sharedid/app/src/customer1/AndroidManifest.xml" I use the string like this
<application
android:allowBackup="true"
android:icon="#drawable/app_logo__forlarge"
android:label="#string/MyAppName"
android:theme="#style/AppBaseTheme_Customer_One"
android:name="com.shared.app.MyApp"
>
...
What am I missing? I am trying to switch over to using product flavors
Apparentlt merge depends on "main" folder also containing strings.xml even though each flavor has their own

Icons look flat in action bar

I've recently "upgraded" my android project to SDK version 26, and now the icons in my app bar look like this (run on Android 6.0), although the icon itself looks like this. I've got no idea why this is happening, I didn't change anything in the java or xml code that seems to be related to this.
I would be very happy if someone posted a solution to this as this could be a bigger problem once Android 8 is released.
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.1'
defaultConfig {
applicationId "de.jamesbeans.quadrasolve"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "digit1.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:26.0.1'
compile 'com.android.support:percent:26.0.1'
}
relevant part of activity_main.xml:
<android.support.v7.widget.Toolbar
android:id="#+id/maintoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"
android:theme="#style/Widget.AppCompat.ActionBar"
android:visibility="visible"
app:popupTheme="#style/Theme.AppCompat.Light"
app:title="QuadraSolve"
app:titleTextColor="#android:color/background_light" />
relevant part of MainActivity.java:
Toolbar maintoolbar = (Toolbar) findViewById(R.id.maintoolbar);
setSupportActionBar(maintoolbar);
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
mainmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_history"
android:icon="#mipmap/ic_history_48px"
android:title="#string/history"
app:showAsAction="ifRoom">
</item>
</menu>
Had the same issue and stumbled across this answer which seems to have fixed the problem:
https://stackoverflow.com/a/45344964/4579919
The matter is that the icon size is bigger then the expected. Obviously the scaling mechanism has changed in SDK 26 and now it leads to this UI bug. Make sure the toolbar icon resources are provided in the following sizes.

Unresolved reference: kotlinx

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.

Resources