1

I have developed the application which is working fine on my device running Marshmallow, now when I try to run it on a earlier version of android (V 21) it gives the following error

java.lang.NoSuchMethodError: No Virtual methold getColor(ILandroid/content/res/Resources$Theme;) 
in class Landroid/content/res/Resources; or its super classes`

In gradle, I include:

compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.veggies.test"
    minSdkVersion 21
    targetSdkVersion 25
    versionName '1.1'

`

I have also copied the resources files e.g. colors.xml etc. from values to values-v21

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Vivek
  • 156
  • 3
  • 18

2 Answers2

3

ok.The problem is that this function getColor() has been included after api 23 so it shows the error.You will have to add check for lower versions

int color;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                color = ContextCompat.getColor(this, R.color.app_theme_color);
            } else {
                color = getResources().getColor(R.color.app_theme_color);
            }
Arpan Sharma
  • 2,142
  • 11
  • 22
1

This is your problem.

minSdkVersion 21
targetSdkVersion 25

What you are saying is that, this app only runs on devices that greater than or equal to 21 and preferably i am targeting devices with api level 25. change min=15 and target same.

Then sync, clean and run again. *the getColors was introduced after that api, that is the main problem.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • The minSdkVersion attribute declares the minimum version with which your app is compatible and the targetSdkVersion attribute declares the highest version on which you've optimized your app. – Remario Mar 01 '17 at 11:49
  • This is completely wrong and misleading, read http://stackoverflow.com/questions/26694108/what-is-the-difference-between-compilesdkversion-and-targetsdkversion – Rafael Francisco Mar 01 '17 at 11:49
  • a word for thought, consider the attribute in manifest.Lets you specify the screen sizes your application supports and enable screen compatibility mode for screens larger than what your application supports. It's important that you always use this element in your application to specify the screen sizes your application supports. – Remario Mar 01 '17 at 11:52
  • Ok, may be my understanding was completely off the chart .. which was that when I specify the minSDKVersion the android compiler sound off for any incompatible method/classes, but that is not the case and I need to manually validate and find the alternative :( – Vivek Mar 01 '17 at 12:20