编译报错,方法数过多解决方案

在android开发的过程中,随着app的功能和代码的增加,总会在一次编译后遇到一种报错:android中方法数超过64k,即64 * 1024位数的限制。

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

方法数超过64k解决方案

1.minSdkVersion>=21,只需要在build.gradle中设置multiDexEnabled为true

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26

        multiDexEnabled true
    }
    ...
}

2.minSdkVersion<21,进行如下操作

  • 在AndroidMainfest.xml中添加application。
  • 调用attachBaseContent()方法调用Multidex.install(this)
public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(context);
     Multidex.install(this);
  }
}
  • build.gradle中添加
android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }
    ...
}

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

搞定收工,是不是很开心啊,终于解决这个问题了,那继续增加代码,增加功能吧,崩溃的一幕又发生了,maindex method 超过 65536 被打爆了!

maindex method 超过 65536 被打爆解决方案

在 project/build.gradle 中:

buildscript {
  repositories {
    maven { url 'https://jitpack.io' }
  }
  dependencies {
    classpath 'com.github.jokermonn:thinAnnotation:0.0.2'
  }
}
在 app/build.gradle 中:

apply plugin: 'thinAnnotation'

thinAnnotation {
  // 是否开启插件
  enable true
  // 目标注解类的路径
  shrinkClass = ['com/joker/maindexkeep/annotations/RuntimeAnn', 'com/joker/maindexkeep/annotations/Type']
  // 目标包的路径
  shrinkPackage = ['com/joker/maindexkeep/shrink']
}

主要目的:瘦身(默认删除所有的 SOURCE 时期注解)

按照以上的处理方式,又可以愉快的增加代码,增加功能了!

关注我的技术公众号,每个工作日都有优质技术文章推送。
微信扫一扫下方二维码即可关注:
图片

猜你喜欢

转载自blog.csdn.net/qq_28190653/article/details/90664714
今日推荐