Android学习之路------解决dex方法数超过65535的问题

前言

今天使用AS编译一个demo时,因为引用了android原生jar包class.jar和其他一些jar,在构建编译时出现以下错误:
Execution failed for task ':hdmiindemo:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

然后网上找了一下资料普遍的方法是在build.gradle中加入以下两段代码

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        //加上这句话
        multiDexEnabled true


    }

}

dependencies {

    //加上这句话
    compile 'com.android.support:multidex:1.0.0'
}

这时候编译完成,可以正常生成apk,但是在运行时又报如下错误:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hdmiindemo/com.example.hdmiindemo.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.hdmiindemo.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.hdmiindemo-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.hdmiindemo-1, /system/lib, /vendor/lib, /system/lib/egl]]
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.ActivityThread.access$800(ActivityThread.java:135)
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
[28:10:45:10]E/AndroidRuntime(15505):        at android.os.Handler.dispatchMessage(Handler.java:102)
[28:10:45:10]E/AndroidRuntime(15505):        at android.os.Looper.loop(Looper.java:136)
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.ActivityThread.main(ActivityThread.java:5017)
[28:10:45:10]E/AndroidRuntime(15505):        at java.lang.reflect.Method.invokeNative(Native Method)
[28:10:45:10]E/AndroidRuntime(15505):        at java.lang.reflect.Method.invoke(Method.java:515)
[28:10:45:10]E/AndroidRuntime(15505):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:815)
[28:10:45:10]E/AndroidRuntime(15505):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
[28:10:45:10]E/AndroidRuntime(15505):        at dalvik.system.NativeStart.main(Native Method)
[28:10:45:10]E/AndroidRuntime(15505): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.hdmiindemo.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.hdmiindemo-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.hdmiindemo-1, /system/lib, /vendor/lib, /system/lib/egl]]
[28:10:45:10]E/AndroidRuntime(15505):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
[28:10:45:10]E/AndroidRuntime(15505):        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
[28:10:45:10]E/AndroidRuntime(15505):        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
[28:10:45:10]E/AndroidRuntime(15505):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
[28:10:45:10]E/AndroidRuntime(15505):        ... 11 more
[28:10:45:10]W/ActivityManager( 1458):   Force finishing activity com.example.hdmiindemo/.MainActivity


最后在更改一下manifest,在application下加入

android:name="android.support.multidex.MultiDexApplication">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme"
        android:name="android.support.multidex.MultiDexApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
    application>

最终可以正常运行

你可能感兴趣的:(Android学习)