0

java.lang.NoClassDefFoundError in Android 5.1.1 but I got running well on 6.0.1

Here is the class which have the error:

public class DuelsTextView extends AppCompatTextView {

    int fontType;

    public DuelsTextView(Context context) {
        super(context);
        init(null);
    }

    public DuelsTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public DuelsTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public void init(AttributeSet attrs) {

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DuelsTextView);
            fontType = a.getInteger(R.styleable.DuelsTextView_font_type, 0);
        }

        try {

            Typeface myTypeface = null;

            if (fontType == 0) {
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/BreeSerif-Regular.ttf");
            } else if (fontType == 1) {
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/MarkoOne-Regular.ttf");
            }

            this.setTypeface(myTypeface);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here is the gradle file:

apply plugin: 'com.android.application'

android {

    compileSdkVersion 25
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.adamvarhegyi.duelsofcodrer"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile('com.android.support:appcompat-v7:25.0.1') {
        exclude module: 'support-v4'
    }
    compile('com.android.support:recyclerview-v7:25.0.1')

    compile('com.makeramen:roundedimageview:2.2.1')
    compile('org.apache.commons:commons-lang3:3.4')
    compile('de.hdodenhof:circleimageview:2.1.0')
}

If I change the extending to simple TextView it's working fine, however android studio suggests me that I would have to use AppCompatTextView for custom views.

Why is this happening? What should I modify?

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

2 Answers2

4

Change these dependencies

compile('com.android.support:appcompat-v7:25.0.1') {
        exclude module: 'support-v4'
    }
    compile('com.android.support:recyclerview-v7:25.0.1')

to

compile('com.android.support:appcompat-v7:25.0.0') 

compile('com.android.support:recyclerview-v7:25.0.0')

Don't know why you exclude v4, but if it's not for some reason I would keep it.

Don't know why its happening, but It happened to me recently and a fix was to match buildToolsVersion with support libraries version.

Vygintas B
  • 1,624
  • 13
  • 31
  • 1
    Compile SDK version has to match major version of support libs. As for build tools version, use the latest (26.0.1 as of August 19, 2017). – Eugen Pechanec Aug 19 '17 at 09:48
0

I suspect the issue is related to using an older support library or by excluding the support-v4 module. Also, the compile configuration is now deprecated and should be replaced by implementation or api.

Your final gradle configuration should look like this,

implementation 'com.android.support:appcompat-v7:26.0.1'
implementation 'com.android.support:recyclerview-v7:26.0.1'

Finally, make sure to test once again with/without excluding the support-v4module.

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50