1

I will being by saying that I have seen SOME answers for this question on stackoverflow already, but only providing a quick 'fix' solution. I would like, if possible, to also understand WHY this is happening (an answer with some detailing).

Now to get to the question: I have just re-started android development, using Android Studio 2.2 . I have an ios app which I want to port to android (meaning, recreate it for Android). I have started with a basic template from Android Studio, added a Constraint Layout and 2 ImageViews using 2 PNG files that I have copied in DRAWABLE folder.

Without making any changes or whatsoever , when I try to build I get this error:

Error:(11) No resource identifier found for attribute 'srcCompat' in package 'x.y.z'. This happens for both images. Here is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/x.y.z"
xmlns:app2="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="x.y.z.MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app2:srcCompat="@drawable/pngFile1"
    android:id="@+id/imageView"
    app2:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="8dp"
    app2:layout_constraintRight_toRightOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app2:srcCompat="@drawable/pngFile2"
    android:id="@+id/imageView2"
    app2:layout_constraintBottom_toTopOf="@+id/imageView"
    app2:layout_constraintRight_toRightOf="@+id/imageView"
    android:layout_marginBottom="8dp" />

Now, here are some things that I would like to understand, in order to have a proper answer to my question:

  • some of the other answers I have found on SO propose to change the default:

    xmlns:app="http://schemas.android.com/apk/res-auto"

to:

xmlns:app="http://schemas.android.com/apk/lib/x.y.z"

It's true that this removes the error from the build, but what seems strange to me (with my little-to-none android experience), is that Android Studio creates then another similar line when i add the 2nd image:

xmlns:app2="http://schemas.android.com/apk/res-auto"

And as we can see, it uses this for the images, so the errors appear again.

I remember like one year ago i had worked on an app on Android Studio, and have used almost the same way of adding images / buttons with images and these problems were not there.

That's why I would also like to understand why this is happening and how to fix it properly.

Thanks in advance !

eemerge
  • 75
  • 2
  • 9
  • I have a project i've been working on for more than a year, and adding images was always normal and ok. Recently I discovered the "Constraints Layout", so I started using, I dropped in some imageViews, and then same problem like you've mentioned. The difference here is the "srcCompat" - why did it changed? seems like updating the App.Compat to latest version could solve the problem, I haven't done it yet, becuase i have some dependency on previous versions. – Itai Spector Nov 23 '16 at 14:01
  • have a look here it might give you some background - we're using a preview version of constraint layout, that can explain some wierd behaviours https://sites.google.com/a/android.com/tools/recent – Itai Spector Nov 23 '16 at 18:44

2 Answers2

1

Fist of all you need to concentrate on the version of android studio you are using. You should also look at the minSdkVersion and targetSdkVersion. Try to use android:src="" instead of app2:srcCompat=

app2:srcCompat= suports older APIs when loading vectorDrawable.

If you want to use it,then amend your build.gradile like,

 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }  

For more information you can refer to this link

or you can refer to this answer at stackoverflow

Community
  • 1
  • 1
A-Majeed
  • 27
  • 10
0
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/cooltext201199220690445" />

I just changed mine into like this and it worked for me I just removed that srccompat and replace it with "background" and i completely removed "xmlns:app2="http://schemas.android.com/apk/res-auto" hope this helps you :)

  • It worked for me without having to remove the xmlns:app portion. Why is this error happening? It is Android Studio which is generated the app:srcCompat – Daniel Viglione Feb 17 '17 at 22:53