问题
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>
回答1:
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'
回答2:
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
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/32328806/app-crashing-on-android-api-less-than-5-0lollipop