Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 Android Studio 配置方法数超过 64k 限制问题 #13

Open
Bakumon opened this issue Feb 13, 2017 · 0 comments
Open

关于 Android Studio 配置方法数超过 64k 限制问题 #13

Bakumon opened this issue Feb 13, 2017 · 0 comments

Comments

@Bakumon
Copy link
Owner

Bakumon commented Feb 13, 2017

版权声明:本文为 Bakumon 原创文章,转载需在明确位置注明出处!!!#13


项目中因为使用到了 Rxjava 等库,导致方法数超过了 64k 限制。于是准备使用 multidex 方案来解决,从此掉入了坑中。

ps:一般的项目如果出现了 64k 限制问题,首先必须考虑的是替换那些只为了方便使用一个小功能而引入的比较大的库,而不是使用下面要介绍的规避 64k 限制的方法。

首先得吐槽一下,开启 multidex 后,打包的速度简直不能再慢,打完一个包,我都可以喝完一杯咖啡了。。热爱生命的我们是最好不会使用它。

1.修改模块级 build.gradle 文件

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

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

2.引用 MultiDexApplication 类

有两种方法来引用 MultiDexApplication 类,选一种使用即可。

  1. 继承 MultiDexApplication
public class MyApplication extends MultiDexApplication {
    ...
}
  1. 重写 Application 的 attachBaseContext 方法
public class MyApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

记得在清单文件中注册 Application

<application
    ...
    android:name=".MyApplication">
    ...
</application>

友情提示:
千万不要忘了引用 MultiDexApplication 类这一步骤,否则,app 在 Android 5.0 以下版本中使用 umeng 统计会闪退,错误信息如下:

java.lang.NoClassDefFoundError: com.umeng.analytics.d

这是 umeng 论坛上给出的解决方法。 http://bbs.umeng.com/thread-16063-1-1.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant