一分钟解决Android 65536问题

作者: FynnJason | 来源:发表于2018-09-12 15:48 被阅读20次

    原创不易,转载请注明出处,谢谢 O(∩_∩)O

    前言

    单个dex文件不能超过65536个方法数,这个问题在我们日常开发中是非常常见的,但当我们每次遇到时,又不能及时解决,这里为了便于快速解决,做个笔记,如何一分钟解决65536所带来的问题。

    步骤

    第一步

    在defaultConfig配置中添加 multiDexEnabled true

    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.fynnjason.app.androiddexdemo"
            minSdkVersion 19
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true //就是这句,添加进来
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    第二步

    依赖最新的multidex包

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        implementation 'com.android.support:multidex:1.0.3' //就是这句,添加进来
    }
    
    第三步

    自己的Application继承MultiDexApplication

    import android.support.multidex.MultiDexApplication;
    
    public class MyApplication extends MultiDexApplication{
        
        @Override
        public void onCreate() {
            super.onCreate();
        }
        
    }
    
    第四步

    在AndroidManifest申明自己的Application

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.fynnjason.app.androiddexdemo">
    
        <application
            android:name=".MyApplication" //就是这句,添加进来
            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">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    这样就解决了Android 65536所带来的问题

    相关文章

      网友评论

        本文标题:一分钟解决Android 65536问题

        本文链接:https://www.haomeiwen.com/subject/yqavgftx.html