android方法数超过64K的解决方案

在android开发的过程中,随着app的功能和代码的增加,总会在一次编译后遇到这个错误:

Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
复制代码

这就是android中方法数超过64k,即64 * 1024位数的限制。在android官方api中给出了这个问题的解决方案《配置方法数超过 64K 的应用》,让你完美的规避64k的限制。

1.如果你的minSdkVersion的设置>=21,只需要在build.gradle中设置multiDexEnabled为true
android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26

        multiDexEnabled true
    }
    ...
}
复制代码
2.如果你的minSdkVersion的设置<21,就需要如下操作了

1.在AndroidMainfest.xml中添加application。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myapp">
   <application
           android:name="MyApplication" >
       ...
   </application>
</manifest>
复制代码

2.调用attachBaseContent()方法调用Multidex.install(this)。

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(context);
     Multidex.install(this);
  }
}
复制代码

3.build.gradle中添加

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
复制代码

就搞定了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值