【问题标题】:App crashing on Android API less than 5.0(lollipop)应用程序在低于 5.0(棒棒糖)的 Android API 上崩溃
【发布时间】:2015-09-01 10:03:09
【问题描述】:

我面临的问题是我的 android 应用程序在具有 API 5.0 及更高版本的设备上运行良好,但在具有 Kitkat(4.4) 及其相应较低版本的设备上崩溃。我知道它与我的 gradle 中的构建工具有某种关系文件,但仍然无法找出问题。有人可以帮我解决这个问题。

这是我的 gradle 文件,

     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')

}

在构建项目时也会出现此错误,

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

这是 StyleableTextView 类,

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);
    }

}

这是 attrs 中的样式,

<resources>

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

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

    </declare-styleable>

</resources>

【问题讨论】:

  • 您能否更具体地了解该错误?发布堆栈跟踪
  • 是的,当然。我在“values”文件夹中的 attrs 文件中为自定义 textview 创建了一个自定义样式。在具有 5.0 但在 API 4.4 及更低版本的设备中检测到样式在您的应用中找不到自定义 TextView 以及 错误,我很确定我已将其用作“com.package.CustomTextView”
  • 值文件夹是否有版本计数器?比如/values-v22?如果是这样,您将需要为其他版本制作一些东西,或者为 Lollipop 设备添加自定义样式。
  • @Knossos 不,它没有版本计数器..
  • @force我会用它的错误堆栈更新帖子。请看看。

标签: android sdk android-gradle-plugin build-tools


【解决方案1】:

当您使用 multidex 时,请尝试使用 MultiDexAppication 而不是 Application 扩展您的应用程序类,并覆盖以下方法,这是 Android 5.0 以下所需的方法(因为 5.0 及以上版本支持 multidex)

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

并在依赖项中添加此

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

【讨论】:

  • 伙计,这篇文章是救命稻草。我一直在寻找解决方案 2 天。我也不确定这个问题。
【解决方案2】:

我刚刚遇到了这个问题,并通过将以下行添加到应用程序 AndroidManifest.xml 作为应用程序标记上的属性来解决它:

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

我有两个应用程序共享相同的资源和库。唯一的区别在于暴露的界面。我向两者都添加了multidex,但忘记将上述属性放在清单中。 NoClassDefFoundError 根本没有多大帮助。

您还可以查看以下内容: http://developer.android.com/tools/building/multidex.html

【讨论】:

    【解决方案3】:

    您可能面临的一个问题就是这个问题,如explained in the documentation

    multiDexEnabled true
    

    使用 multidex 的应用程序可能无法在运行的设备上启动 由于平台版本早于 Android 4.0(API 级别 14) 到 Dalvik linearAlloc 错误(问题 22586)。如果您的目标是 API 早于 14 的级别,请确保使用这些进行测试 平台版本,因为您的应用程序可能在 启动或加载特定的类组时。代码 缩小可以减少或可能消除这些潜在问题。

    【讨论】:

    • 当 multiDexEnabled 被移除时,构建遇到错误“错误:任务':app:dexDebug'的执行失败。> com.android.ide.common.process.ProcessException: org.gradle.process。 internal.ExecException: Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' 以非零退出值 2" 结束
    【解决方案4】:

    这可能与特定场景无关,但问题的标题与我遇到的问题以及我遇到此问题的原因有关。所以我在这里回答。 我遇到了与此类似的问题,但在我的情况下,扩展 multidexapplication 并启用 multidex 并没有完成这项工作。我最终在我的项目中使用plugin 来计算方法。通过使用它,我意识到我远未达到 65K 的限制,并且该错误与其他问题有关。我找到了this 页面,他们曾在其中谈到从设置中禁用即时运行,这实际上为我完成了这项工作。现在我没有使用多 dex 库,而且构建速度也更快。如果我再次遇到问题,我会相应地更新答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多