API 19上的Android Gradle Multidex Build问题 [英] Android Gradle Multidex Build issue on API 19

查看:81
本文介绍了API 19上的Android Gradle Multidex Build问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中启用了 multidex 来避免 65k限制 productFlavors (dev API 21和prod API 19)进行定制.在 API 21 即dev风格上构建我的项目是成功的,但是在 API 19 即prod风格上构建我的项目,则在应用任务 shrink {component} MultiDexComponents中不断给我例外

I have a project in which I have enabled multidex to avoid 65k limit and also productFlavors (dev API 21 and prod API 19) for customization. Building my Project on API 21 i.e dev flavor is successful but on API 19 i.e. prod flavor, it is continuously giving me exception in app task 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'  
}

有什么需要帮助的人??

Any help please anyone ??

推荐答案

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

Android 5.0及更高版本使用称为ART的运行时支持从应用程序APK文件加载多个dex文件.艺术在应用程序安装时执行预编译,该程序会扫描classes(.. N).dex文件并将其编译为单个.oat文件,以用于由Android设备执行.有关Android的更多信息5.0运行时,请参见ART简介.

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.

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

This is the reason why your app is working fine on API level 21.

Android 5.0之前的Multidex支持

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

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.

因此,首先确保您已导入正确的依赖关系,看来您确实做到了.

So, Firstly making sure you have imported correct dependency, which It seems you did it.

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

在清单中,将来自multidex支持库的 MultiDexApplication 类添加到application元素.

In your manifest add the MultiDexApplication class from the multidex support library to the application element.

<?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 .

Alternative to that, If your app extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex.

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

我希望它能对您有所帮助.

I hope it will help you out.

这篇关于API 19上的Android Gradle Multidex Build问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆