java.lang.NoClassDefFoundError 使用MultiDex 后,运行时发现有些crash或者有些类无法调用 报NoClassDefFound error

首先正确使用 google的multipartdex

1. 修改Gradle,导入’com.android.support:multidex:1.0.0’,打开multiDexEnabled;

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. 修改Application.两种方法:

1) 直接把Application替换成MultiDexApplication

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

2) 在原来的Application中修改调用MultiDex.install(this);

public class HelloMultiDexApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

3 如果做了上面处理,依旧NoClassDefFound error 通过如下方式处理:

一些在二级Dex加载之前,可能会被调用到的类(比如静态变量的类),需要放在主Dex中.否则会ClassNotFoundError.
通过修改Gradle,可以显式的把一些类放在Main Dex中.
在gradle中添加

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex'
        dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()

    }
}

app/build/intermediates/multi-dex/xxx/debug/maindexlist.txt复制到$projectDir/[eg :app/]目录下,然后重命名为multidex.keep

参考
1. Android 分Dex (MultiDex)
2. Multi-dex to rescue from the infamous 65536 methods limit
3. Generating main-dex-list file
4. Building Apps with Over 65K Methods

更多问题请关注 android开发遇到问题点滴

你可能感兴趣的:(gradle,multidex,NoClassDef)