本文介绍了如何通过导入正确的库来避免DEX 64K LIMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译我使用的所有库时,我遇到了这个问题,它溢出了Dalvik中的 64k方法限制。当我导入支持库时,我开始遇到这个问题,因为其中一些已经包含在其他支持库中,结果溢出了限制。



有没有办法验证if一个库在当前项目中是未使用的,或者已经通过另一个库依赖项导入了?



目前,我排除了我所知道的那些,但似乎很奇怪必须做

 依赖关系{
编译fileTree(dir:'libs',include:['* .jar '])
compile'c​​om.android.support:multidex:1.0.1'
compilecom.android.support:percent:${supportLibVersion}
compile(com.android .support:design:$ {supportLibVersion}){
排除模块:'support-v4'
}
compile(com.android.support:cardview-v7:$ {supportLibVersion} ){
exclude module:'support-v4'
}

compile('com.github.nkzawa:socket.io-client:0.4.1'){
排除组别: 'org.json',module:'json'
}
compile('com.astuetz:pagerslidingtabstrip:1.0.1'){
排除模块:'support-v4'
}
// ...
}






到目前为止的解决方案:




  1. 使用Gradle插件 - 我们写了一个非常简单的回溯一段时间后使用Gradle插件,其中列出了包装方法的数量以及总数。你可以在这里找到更多关于它的信息。
  2. 只需在本网站上输入'compile'语句,它就会告诉你它是方法数量,依赖性,JAR大小和DEX大小。 Android Studio插件 - 这个出色的插件显示了Android Studio中每个依赖项的方法数量。
  3. 使用MultiDex支持库
    如果您使用Android Studio,过程非常直接。如果您不是,我强烈建议迁移,因为Google可能很快会放弃对Eclipse ADT插件和旧的基于Ant的构建系统的支持。





在您的build.gradle中添加MultiDex支持库的依赖关系

 依赖关系{
...
compile'c​​om.android.support:multidex:'
...
}

第2步

通过在gradle配置的buildType或productFlavor部分设置multiDexEnabled标志来启用多分类。

  defaultConfig {
...
multiDexEnabled true
...
}





如果你还没有创建自己的Application类,只需声明 android.support.multidex.MultiDexApplication 作为您的应用程序类,位于 An droidManifest.xml

  .... 
android:name =android.support .multidex.MultiDexApplication
...

如果您已拥有自己的Application类,使它扩展 android.support.multidex.MultiDexApplication 而不是 android.app.Application



如果您的Application类正在扩展某个其他类并且您不想或不能更改它,请重写attachBaseContext(),如下所示:

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



$ div $解析方案

你可以打开终端并运行命令 gradlew app:dependencies 来测试哪些依赖项已经包含在其他项目中作为项目的传递依赖项以及每个版本的相应版本。



例如,我为我使用的com.android.support:design库中的一个项目获得了以下依赖关系图表:

  + --- com.android.support:design:23.3.0 
| + --- com.android.support:appcompat-v7:23.3.0
| | + --- com.android.support:support-vector-drawable:23.3.0
| | | \ --- com.android.support:support-v4:23.3.0
| | | \ --- com.android.support:support-annotations:23.3.0
| | + --- com.android.support:animated-vector-drawable:23.3.0
| | | \ --- com.android.support:support-vector-drawable:23.3.0(*)
| | \ --- com.android.support:support-v4:23.3.0(*)
| + --- com.android.support:support-v4:23.3.0(*)
| \ --- com.android.support:recyclerview-v7:23.3.0
| + --- com.android.support:support-v4:23.3.0(*)
| \ --- com.android.support:support-annotations:23.3.0


I had this issue where I overflow the 64k method limit in Dalvik when compiling all the library I used. I started to have this issue when I imported the Support Library, as some are already contained in others, it ended up overflowing the limit.

Is there a way to verify if a library is unused in the current project or already imported through another library dependencies?

Currently, I'm excluding those I know for sure but it seems weird to have to do this by hand.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:multidex:1.0.1'
    compile "com.android.support:percent:${supportLibVersion}"
    compile("com.android.support:design:${supportLibVersion}") {
        exclude module: 'support-v4'
    }
    compile("com.android.support:cardview-v7:${supportLibVersion}") {
        exclude module: 'support-v4'
    }

    compile('com.github.nkzawa:socket.io-client:0.4.1') {
        exclude group: 'org.json', module: 'json'
    }
    compile('com.astuetz:pagerslidingtabstrip:1.0.1') {
        exclude module: 'support-v4'
    }
//...
}


Solutions so far:

  1. Use a Gradle plugin – We wrote an extremely easy to use Gradle plugin some time back that lists the number of methods by package along with the total number. You can find more information about it here.
  2. www.methodscount.com – Wondering how many methods a particular library will add to your application? Just enter the ‘compile’ statement on this website and it’ll tell you it’s method count, dependencies, JAR size and DEX size.
  3. Android Studio Plugin – This excellent plugin shows the method count of each dependency right inside Android Studio.
  4. Using the MultiDex Support LibraryIf you are using Android Studio, the process is very straight-forward. If you are not, I highly recommend migrating, as Google may soon drop support for the Eclipse ADT plugin and the old Ant based build system.

Step 1

Add the dependency for the MultiDex support library in your build.gradle

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

Step 2

Enable multi-dexing by setting the multiDexEnabled flag in the buildType or productFlavor section of your gradle configuration.

defaultConfig {
   ...
multiDexEnabled true
...
}

Now depending on your project, you have 3 options:

If you haven’t created your own Application class, simply declare android.support.multidex.MultiDexApplication as your application class in AndroidManifest.xml

   ....
   android:name="android.support.multidex.MultiDexApplication"
   ...

If you already have your own Application class, make it extend android.support.multidex.MultiDexApplication instead of android.app.Application

If your Application class is extending some other class and you don’t want to or can’t change it, override attachBaseContext() as shown below:

public class MyApplication extends FooApplication {
   @Override
   protected void attachBaseContext(Context base) {
      super.attachBaseContext(base);
      MultiDex.install(this);
   }
}
解决方案

You can open the terminal and run command gradlew app:dependencies to test which dependencies are already included in the others as transitive dependencies for your project along with the respective versions of each.

For example I got the following dependency chart for one of my projects for the com.android.support:design library I used:

+--- com.android.support:design:23.3.0
|    +--- com.android.support:appcompat-v7:23.3.0
|    |    +--- com.android.support:support-vector-drawable:23.3.0
|    |    |    \--- com.android.support:support-v4:23.3.0
|    |    |         \--- com.android.support:support-annotations:23.3.0
|    |    +--- com.android.support:animated-vector-drawable:23.3.0
|    |    |    \--- com.android.support:support-vector-drawable:23.3.0 (*)
|    |    \--- com.android.support:support-v4:23.3.0 (*)
|    +--- com.android.support:support-v4:23.3.0 (*)
|    \--- com.android.support:recyclerview-v7:23.3.0
|         +--- com.android.support:support-v4:23.3.0 (*)
|         \--- com.android.support:support-annotations:23.3.0

这篇关于如何通过导入正确的库来避免DEX 64K LIMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 02:49
查看更多