【问题标题】:Android Gradle Multidex Build issue on API 19API 19 上的 Android Gradle Multidex 构建问题
【发布时间】:2018-03-10 14:06:53
【问题描述】:

我有一个项目,我在其中启用了 multidex 以避免 65k limitproductFlavors(开发 API 21 和产品 API 19)进行自定义。 在API 21 即开发风格上构建我的项目是成功的,但在API 19 即产品风格上,它在应用程序任务shrink{component}MultiDexComponents 中不断给我例外@

完整的错误日志:

:app:shrinkProdDebugMultiDexComponents FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:shrinkProdDebugMultiDexComponents'.
> java.io.IOException: Can't read [{Project Path}/app/build/intermediates/multi-dex/prod/debug/allclasses.jar] (Can't process class [com/olivephone/office/a/b/e/p.class] (Unknown verification type [17] in stack map frame))

build.gradle

apply plugin: 'com.android.application'

android {  
    compileSdkVersion 23  
    buildToolsVersion '23.0.0'  
    defaultConfig {  
        applicationId '{Project Name}'  
        minSdkVersion 15  
        targetSdkVersion 23  
        versionCode 1  
        versionName "1.0"  
        multiDexEnabled true  
    }  
    productFlavors {  
        dev {  
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin  
            // to pre-dex each module and produce an APK that can be tested on  
            // Android Lollipop without time consuming dex merging processes.  
            minSdkVersion 21  
        }  
        prod {  
            // The actual minSdkVersion for the application.  
            minSdkVersion 19  
        }  
    }  
    buildTypes {  
        release {  
            minifyEnabled false  
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
        }  
    }  
}  

dependencies {  
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    compile 'com.android.support:appcompat-v7:23.0.1'  
    compile 'com.android.support:recyclerview-v7:23.0.1'  
    compile 'com.android.support:cardview-v7:23.0.1'  
    compile 'com.oguzdev:CircularFloatingActionMenu:1.0.2'  
    compile 'com.google.code.gson:gson:2.3.1'  
    compile 'com.google.android.gms:play-services:8.1.0'  
    compile 'com.android.support:multidex:1.0.1'  
    compile files('libs/linkedin-j-android.jar')  
    compile files('libs/itsrts-pptviewer.jar')  
    compile files('libs/signpost-core-1.2.1.1.jar')  
    compile 'org.twitter4j:twitter4j-core:4.0.2'  
    compile files('libs/universal-image-loader-1.9.2-SNAPSHOT-with-sources.jar')
    compile files('libs/dropbox-android-sdk-1.6.3.jar')  
    compile files('libs/json_simple-1.1.jar')  
    compile 'com.joanzapata.pdfview:android-pdfview:1.0.1@jar'  
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'  
}

任何人都可以帮忙吗??

【问题讨论】:

  • 已经做到了!运气不好
  • 你在使用 Proguard 吗?
  • @Fondesa 不使用 Proguard
  • @AndiGeeky 使用 build.gradle 编辑问题
  • 问题在于编译文件('libs/itsrts-pptviewer.jar')这个。 libs文件夹中的jar文件。如果您将其删除,那么它将起作用

标签: android android-multidex


【解决方案1】:

对 Android 5.0 及更高版本的 Multidex 支持

Android 5.0 及更高版本使用一个名为 ART 的运行时,它本机 支持从应用 APK 文件中加载多个 dex 文件。艺术 在应用程序安装时执行预编译,扫描 classes(..N).dex 文件并将它们编译成单个 .oat 文件 由安卓设备执行。有关 Android 的更多信息 5.0 运行时,请参阅 ART 简介。

这就是您的应用在 API 级别 21 上运行良好的原因。

Android 5.0 之前的 Multidex 支持

Android 5.0 之前的平台版本使用 Dalvik 运行时 用于执行应用程序代码。默认情况下,Dalvik 将应用程序限制为单个 每个 APK 的 classes.dex 字节码文件。为了解决这个问题 限制,你可以使用multidex支持库,它变成 应用程序的主要 DEX 文件的一部分,然后管理对 额外的 DEX 文件及其包含的代码。

所以,首先确保你已经导入了正确的依赖,看起来你已经做到了。

dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

在您的清单中,将来自 multidex 支持库的 MultiDexApplication 类添加到应用程序元素。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

除此之外,如果您的应用扩展了Application 类,您可以覆盖attachBaseContext() 方法并调用MultiDex.install(this) 以启用multidex

public void onCreate(Bundle arguments) {
    MultiDex.install(getTargetContext());
    super.onCreate(arguments);
    ...
}

希望对你有所帮助。

【讨论】:

  • 谢谢.. Bhuvesh 我只是在我的应用程序文件中添加下面的代码就可以了,在 oncreate 中没有 BUNDLE,在此之前没有 attachBaseContext(),在此之前我只在我的构建中使用 MULTIDEX JAR,但我的应用程序在运行时崩溃低于 api 21.. 现在它的工作不知道背后的原因。 public void onCreate() { MultiDex.install(this);超级.onCreate(); // 初始化单例 sInstance = this; }
【解决方案2】:

在 API 21 中,:app:shrinkProdDebugMultiDexComponents 命令未被调用,因为 API 21 已经使用 ART 而不是 Dalvik。因此,原生支持 multidex。

对于低于 21 的 API,则执行命令 :app:shrinkProdDebugMultiDexComponents。 检查你的build.gradle 一切看起来都很好,这让我想到了以下内容。

您是否正确设置了 multidex 支持?

您是否已设置清单以支持 Multidex?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
  <application
    ...
    android:name="android.support.multidex.MultiDexApplication">
    ...
  </application>
</manifest>

或者如果你真的扩展了应用程序类,你可以这样做:

public class MyApplication extends Application {
    ...
    @Override
    protected void attachBaseContext(Context base) {
       super.attachBaseContext(base);
       MultiDex.install(this);
    }
    ...     
}

或使用这个“预建”版本

public class MyApplication extends MultiDexApplication{

 ...     

}

【讨论】:

    【解决方案3】:

    我还没有窥探这背后的逻辑,但就我而言,这是由于我的 build.gradle 文件中 compile 'com.android.support:appcompat-v7:25.3.1'compile 'com.google.android.gms:play-services:10.2.4' 的版本冲突。

    仅启用 multidex 对我不起作用。在寻找其他解决方案时,我发现有人抱怨 play-services 版本冲突的奇怪问题。所以,我回溯了代码更改,最后将 play-services 版本从 10.2.4 更改为 10.2.1,它对我有用。

    【讨论】:

      【解决方案4】:

      解决了上面的崩溃问题。我删除了一些未使用的依赖项,还删除了 rx java 依赖项 io.reactivex.rxjava 。为了替换 Rx java,我在我的包中添加了一些虚拟类,这里已经描述了 https://realm.io/docs/java/latest

      // File 1
      package io.reactivex;
      public class Flowable {
      }
      // File 2
      package io.reactivex;
      public class Observable {
      }
      // File 3
      package io.reactivex;
      public enum BackpressureStrategy {
         LATEST;
      }
      

      【讨论】:

        【解决方案5】:

        请按如下方式更改您的依赖项:

        已编辑依赖项:

        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.android.support:recyclerview-v7:23.0.1'
        compile 'com.android.support:cardview-v7:23.0.1'
        compile 'com.oguzdev:CircularFloatingActionMenu:1.0.2'
        compile 'com.google.code.gson:gson:2.3.1'
        compile 'com.google.android.gms:play-services:8.1.0'
        compile 'com.android.support:multidex:1.0.1'
        compile 'com.googlecode.linkedin-j:linkedin-j-core:1.0.416'
        compile 'oauth.signpost:signpost-core:1.2.1.2'
        compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
        compile 'com.googlecode.json-simple:json-simple:1.1'
        compile 'org.twitter4j:twitter4j-core:4.0.2'
        compile files('libs/itsrts-pptviewer.jar')
        compile files('libs/dropbox-android-sdk-1.6.3.jar')
        compile 'com.joanzapata.pdfview:android-pdfview:1.0.1@jar'
        compile 'com.facebook.android:facebook-android-sdk:4.1.0'  
        

        原因是 'compile fileTree(dir: 'libs', include: ['*.jar'])' 包含 libs 文件夹中的所有 jar 文件到 gradle..

        谢谢。!!

        【讨论】:

          【解决方案6】:

          shrinkProdDebugMultiDexComponents 任务invokes Proguard,所以这个错误来自Proguardcode

          我的猜测是您没有使用最新版本的 Olive Office SDK(可能与错误或配置错误的 Proguard 版本混淆)。如果是这种情况,请从 SDK 开发人员处获取最新版本。

          有关解决方法,请查看this 类似的错误。 this blogpost 虽然以 wontfix 状态关闭,但描述了如何修补 Proguard 代码。

          同时查看thisthat Eric Lafortune(Proguard 的作者)对类似问题的回答。

          【讨论】:

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