6

The problem I am facing is that my android application works well on devices having API 5.0 and above but crashes on devices having Kitkat(4.4) and its corresponding lower versions.I know that its somehow related to the build tools in my gradle file,but still not able to figure out the problem.Could someone please help me out with this.

This is my gradle file,

     buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'android'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}


android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.abc.example"
        minSdkVersion 10
        targetSdkVersion 22
        multiDexEnabled true
        versionCode 12
        versionName "1.0"
    }
    buildTypes {
        release {
            multiDexEnabled true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    android {
        packagingOptions {
            exclude 'META-INF/LICENSE'
        }
    }
    android {
        packagingOptions {
            exclude 'META-INF/NOTICE'
        }
    }
}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT@aar') {
        transitive = true
        exclude module: 'httpclient'
        exclude group: 'httpclient'
    }

    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:mockwebserver:2.3.0'
    compile 'de.hdodenhof:circleimageview:1.2.2'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.android.gms:play-services-maps:7.5.0'
    compile files('libs/bitlyj-2.0.0.jar')

}

Also getting this error when project is built,

Caused by: java.lang.NoClassDefFoundError: com.package.R$styleable
            at com.package.widgets.StyleableTextView.<init>(StyleableTextView.java:23)

This is StyleableTextView class,

public class StyleableTextView extends TextView {

    public StyleableTextView(Context context) {
        super(context);
    }

    public StyleableTextView(Context context, AttributeSet attrs) {
        super(context.getApplicationContext(), attrs);
        UiUtil.setCustomFont(StyleableTextView.this, context, attrs,
                R.styleable.com_eywa_wonk_widgets_StyleableTextView,
                R.styleable.com_eywa_wonk_widgets_StyleableTextView_font);
    }

    public StyleableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context.getApplicationContext(), attrs, defStyle);
        UiUtil.setCustomFont(StyleableTextView.this, context, attrs,
                R.styleable.com_eywa_wonk_widgets_StyleableTextView,
                R.styleable.com_eywa_wonk_widgets_StyleableTextView_font);
    }

}

This is the styleable in attrs,

<resources>

    <attr name="font" format="string" />

<declare-styleable name="com.package.widgets.StyleableTextView">
        <attr name="font" />

    </declare-styleable>

</resources>
Android solutions
  • 111
  • 1
  • 1
  • 7
  • Can you be more specific about the error ? Post the stack trace – forcewill Sep 01 '15 at 10:07
  • Yeah sure.I have created a custom styleable for a custom textview in my attrs file in "values" folder.The styleable gets detected in devices having 5.0 but in devices with API 4.4 and less, it says cannot find the custom TextView in your app along with the error and I am pretty sure i have used it as "com.package.CustomTextView" – Android solutions Sep 01 '15 at 10:12
  • Does the values folder have a version counter? Such as /values-v22? If so, you will need to make something for other versions, or add the custom styleable only for Lollipop devices. – Knossos Sep 01 '15 at 10:13
  • @Knossos no it doesn't have a version counter.. – Android solutions Sep 01 '15 at 10:16
  • @forcewill I have updated the post with its error stack.Please have a look. – Android solutions Sep 01 '15 at 10:17
  • @Knossos updated the post a little please can you take a look.? – Android solutions Sep 01 '15 at 10:17
  • @Androidsolutions does the error occurs in release, debug or both ? Can you post the declare-styleable also ? – forcewill Sep 01 '15 at 10:21
  • Yes I can post that..and yes it occurs in all builds be it debug or release and if I remove multiDexEnabled="true" build fails with error "non zero exit value as 2". – Android solutions Sep 01 '15 at 10:36
  • @forcewill updated the post with the declare-styleable.Please do have a look. – Android solutions Sep 01 '15 at 10:39
  • I don't know if this could be the cause but you're using plugin `android` and plugin `com.android.application` plugin 'android' is deprecated and you should use only the later maybe something on the build is going wrong because you have both, try and remove android one – forcewill Sep 01 '15 at 10:56
  • @forcewill will try that as well.. – Android solutions Sep 01 '15 at 11:37

4 Answers4

27

When you are using multidex , then try to extend your application class with MultiDexAppication instead of Application and override below method this required for Android below 5.0 (because 5.0 and above support to the multidex)

   @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(BaseApplication.this);
    }

and in dependencies add this

compile 'com.android.support:multidex:1.0.1'

Irshad
  • 3,071
  • 5
  • 30
  • 51
Shriram Ram
  • 286
  • 3
  • 3
  • 1
    Man, this post is a life saver. I had been looking for a solution for 2 days. I wasn't sure of the issue either. – drulabs Mar 16 '16 at 13:30
3

I just had this problem and solved it by adding the following line to the applications AndroidManifest.xml as an attribute on the application tag:

android:name="android.support.multidex.MultiDexApplication"

I have two apps which share the same resources and libraries. The only difference is in the exposed interface. I added multidex to both but forgot to put the above attribute in the manifest for one. The NoClassDefFoundError did not help much at all.

You can also check out the following: http://developer.android.com/tools/building/multidex.html

Victor Ude
  • 415
  • 4
  • 13
0

One issue that you may face is this one, as explained in the documentation:

multiDexEnabled true

Applications that use multidex may not start on devices that run versions of the platform earlier than Android 4.0 (API level 14) due to a Dalvik linearAlloc bug (Issue 22586). If you are targeting API levels earlier than 14, make sure to perform testing with these versions of the platform as your application can have issues at startup or when particular groups of classes are loaded. Code shrinking can reduce or possibly eliminate these potential issues.

Knossos
  • 15,802
  • 10
  • 54
  • 91
  • when multiDexEnabled removed then build encounters an error as "Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' finished with non-zero exit value 2" – Android solutions Sep 01 '15 at 10:40
0

This might not be relevant to the particular scenario but as the heading of the question relates to the problem I encountered and the reason I came across this question. So I am answering it here. I encountered an issue similar to this, but in my case extending multidexapplication, and enabling multidex weren't doing the job. I ended up using a plugin for counting methods in my project. By using that I realised that I am no way near the 65K limit and the bug is related to something else. I found this page where they someone had talked about disabling instant-run from Settings, which actually did the job for me. Now I am not using the multi-dex library and the builds are faster as well. If I encounter the issue again, I will update the answer accordingly.

Akshay
  • 1,020
  • 6
  • 10