一分鐘解決Android 65536問題
原創不易,轉載請註明出處,謝謝 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所帶來的問題