上篇文章講到了ant方式進(jìn)行dex分包《Android Dex分包》,本篇文章再來看一下采用gradle方式進(jìn)行dex分包的實(shí)現(xiàn)。
dex分包的gradle方式實(shí)現(xiàn)
我們用同樣的demo工程采用gradle進(jìn)行multidex分包測試。由于本人的AS已經(jīng)升到2.3.1版本匣掸,對應(yīng)的gradle版本為2.3.1,gradle插件版本升到了3.3,而gradle插件3.3版本要求buildToolsVersion版本為25及以上晰骑,而buildTools 25又要求jdk版本大于等于52,即jdk1.8绊序,所以需要將android studio切換到j(luò)dk1.8硕舆,需要自行下載jdk1.8并配置好環(huán)境即可,build.gradle中不需要配置
android studio配置jdk1.8骤公,網(wǎng)上有些教程推薦直接在build.gradle中配置即可抚官,如果是在build.gradle中指定了用jdk1.8來編譯
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
會編譯失敗,報如下錯誤
* What went wrong:
A problem occurred configuring project ':app'.
> Jack is required to support java 8 language features. Either enable Jack or remove sourceCompatibility JavaVersion.VERSION_1_8.
要求使用Jack編譯器來支持java8特性阶捆,或者移除sourceCompatibility直接編譯凌节。
如果要使用Jack編譯器,則需要在build.gradle添加如下支持
jackOptions {
enabled true
}
關(guān)于Jack編譯器趁猴,可參考《Android 新一代編譯 toolchain Jack & Jill 簡介》一文
Jack 是 Java Android Compiler Kit 的縮寫刊咳,它可以將 Java 代碼直接編譯為 Dalvik 字節(jié)碼彪见,并負(fù)責(zé) Minification, Obfuscation, Repackaging, Multidexing, Incremental compilation儡司。它試圖取代 javac/dx/proguard/jarjar/multidex 庫等工具
使用Jack編譯器來編譯之后,可以正常打包構(gòu)建余指,并且也進(jìn)行了mulitdex處理捕犬,但是dexOptions中的參數(shù)都未生效,究其原因就是由于采用了Jack編譯器來執(zhí)行編譯操作酵镜,不同與原來的 javac+dx編譯過程碉碉,二者區(qū)別如下:
//javac+dx編譯過程
javac (.java –> .class) –> dx (.class –> .dex)
//jack編譯過程
Jack (.java –> .jack –> .dex)
Jack是將java源碼編譯城.jack文件再轉(zhuǎn)化為.dex文件,不再執(zhí)行dx操作淮韭,所以配置的dexOptions沒有生效
本來google推出Jack 編譯器是準(zhǔn)備取代javac+dx的編譯方式垢粮,但是由于Jack在支持基本編譯功能之外的其他功能上存在一定的局限,所以在今年3月靠粪,Google宣布放棄Jack蜡吧,重新采用javac+dx的方式在Android里支持Java 8毫蚓。
所以我們這里沒有采用這種編譯方式,沒有在gradler腳本中配置jdk1.8昔善,而是直接在系統(tǒng)變量中更改編譯環(huán)境為jdk1.8
demo中build.gradle腳本如下
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.multidextest"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
//這里不采用jack編譯方式
// jackOptions {
// enabled true
// }
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// compileOptions {
// sourceCompatibility 1.8
// targetCompatibility 1.8
// }
dexOptions {
javaMaxHeapSize "1g"
preDexLibraries = false
additionalParameters = [ //配置multidex參數(shù)
'--multi-dex',//多dex分包
'--set-max-idx-number=30000',//每個包內(nèi)方法數(shù)上限
'--main-dex-list='+projectDir+'/main-dex-rule', //打包到主classes.dex的文件列表
'--minimal-main-dex'
]
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
//multidex支持依賴
compile 'com.android.support:multidex:1.0.0'
testCompile 'junit:junit:4.12'
}
main-dex-rule文件內(nèi)容如下:
com/example/multidextest/MainActivity$1.class
com/example/multidextest/HelperOne.class
com/example/multidextest/MainActivity.class
com/example/multidextest/ApplicationLoader.class
執(zhí)行g(shù)radle命令后元潘,得到構(gòu)建出的apk文件,通過as可以看到已經(jīng)包含了多個dex
主dex中包含指定的類文件
從dex中包含其他的未打到主dex中的類和其他依賴的jar包等
關(guān)于main-dex-rule文件的自動生成方式君仆,可以參考
可參考《Android傻瓜式分包插件》或者《android multidex異步加載》
dex文件的加載
上篇文章已經(jīng)提到翩概,apk初次安裝啟動的時候只會對主dex進(jìn)行優(yōu)化加載操作,而從dex文件需要在app啟動時手動加載返咱,AS中可以通過引入multidex包來支持從dex的加載钥庇,有三種方式,如下:
1.manifest文件中指定Application為MultiDexApplication洛姑,對于一般不需要在application中執(zhí)行初始化操作的app可以采用這種
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
……>
2.自定義Application并繼承MultiDexApplication
public class MyApplication extends MultiDexApplication{
……
}
3.重寫Application的attachBaseContext方法
public class MyApplication extends Application{
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
方式一二相同上沐,先來看方式二的實(shí)現(xiàn)只需要將ApplicationLoader類由原先繼承自Application類修改為繼承MultiDexApplication即可,無需在onCreate中添加其他加載dex的代碼楞艾。所以可以猜想参咙,MultiDexApplication中肯定是執(zhí)行了加載從dex的相關(guān)操作。下面來看MultiDexApplication的源碼
public class MultiDexApplication extends Application {
public MultiDexApplication() {
}
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
可以看到MultiDexApplication 繼承Application硫眯, 并在attachBaseContext()中調(diào)用了MultiDex.install(this)蕴侧,所以上述幾種方式本質(zhì)是相同的。
MultiDex.install()方法如下:
/**
* Patches the application context class loader by appending extra dex files
* loaded from the application apk. This method should be called in the
* attachBaseContext of your {@link Application}, see
* {@link MultiDexApplication} for more explanation and an example.
*
* @param context application context.
* @throws RuntimeException if an error occurred preventing the classloader
* extension.
*/
public static void install(Context context) {
//省略若干代碼...
try {
ApplicationInfo applicationInfo = getApplicationInfo(context);
if (applicationInfo == null) {
// Looks like running on a test Context, so just return without patching.
return;
}
synchronized (installedApk) {
String apkPath = applicationInfo.sourceDir;
//installedApk 為set集合两入,防止dex重復(fù)加載
if (installedApk.contains(apkPath)) {
return;
}
installedApk.add(apkPath);
//省略若干代碼...
ClassLoader loader;
try {
//此處獲取到的是PathClassLoader
loader = context.getClassLoader();
} catch (RuntimeException e) {
//...
return;
}
//...
try {
clearOldDexDir(context);
} catch (Throwable t) {
Log.w(TAG, "Something went wrong when trying to clear old MultiDex extraction, "
+ "continuing without cleaning.", t);
}
//data/data/<packagename>/code_cache/secondary-dexes" 即從dex優(yōu)化后的緩存的路徑
File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);
//從apk中抽取dex文件并存到緩存目錄下净宵,保存為zip文件
List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false);
if (checkValidZipFiles(files)) {
//
installSecondaryDexes(loader, dexDir, files);
} else {
Log.w(TAG, "Files were not valid zip files. Forcing a reload.");
// Try again, but this time force a reload of the zip file.
files = MultiDexExtractor.load(context, applicationInfo, dexDir, true);
if (checkValidZipFiles(files)) {
installSecondaryDexes(loader, dexDir, files);
} else {
// Second time didn't work, give up
throw new RuntimeException("Zip files were not valid.");
}
}
}
} catch (Exception e) {
Log.e(TAG, "Multidex installation failure", e);
throw new RuntimeException("Multi dex installation failed (" + e.getMessage() + ").");
}
Log.i(TAG, "install done");
}
重點(diǎn)關(guān)注MultiDexExtractor.load(context, applicationInfo, dexDir, false) 從apk中抽取出從dex,
該方法有四個參數(shù)
context 上下文
applicationInfo 應(yīng)用信息裹纳,用于獲取apk文件
dexDir dex文件優(yōu)化后的緩存路徑
forceReload 是否強(qiáng)制重新從apk文件中抽取dex
/**
* Extracts application secondary dexes into files in the application data
* directory.
*
* @return a list of files that were created. The list may be empty if there
* are no secondary dex files.
* @throws IOException if encounters a problem while reading or writing
* secondary dex files
*/
static List<File> load(Context context, ApplicationInfo applicationInfo, File dexDir,
boolean forceReload) throws IOException {
Log.i(TAG, "MultiDexExtractor.load(" + applicationInfo.sourceDir + ", " + forceReload + ")");
final File sourceApk = new File(applicationInfo.sourceDir);
//首先進(jìn)行crc校驗(yàn)
long currentCrc = getZipCrc(sourceApk);
List<File> files;
if (!forceReload && !isModified(context, sourceApk, currentCrc)) {
try {
//已經(jīng)從apk中抽取出dex文件并存到緩存目錄中择葡,則直接返回zip文件list
files = loadExistingExtractions(context, sourceApk, dexDir);
} catch (IOException ioe) {
Log.w(TAG, "Failed to reload existing extracted secondary dex files,"
+ " falling back to fresh extraction", ioe);
files = performExtractions(sourceApk, dexDir);
putStoredApkInfo(context, getTimeStamp(sourceApk), currentCrc, files.size() + 1);
}
} else {
Log.i(TAG, "Detected that extraction must be performed.");
//從apk中復(fù)制dex文件到緩存目錄
files = performExtractions(sourceApk, dexDir);
//保存時間戳、crc剃氧、dex數(shù)量等信息到sp
putStoredApkInfo(context, getTimeStamp(sourceApk), currentCrc, files.size() + 1);
}
Log.i(TAG, "load found " + files.size() + " secondary dex files");
return files;
}
forceReload為false并且已經(jīng)從apk中抽取過dex文件則直接調(diào)用loadExistingExtractions 返回dex文件的zip列表
private static List<File> loadExistingExtractions(Context context, File sourceApk, File dexDir)
throws IOException {
Log.i(TAG, "loading existing secondary dex files");
//dex文件的前綴 敏储,即data/data/packageName/code_cache/secondary-dexes/data/data/apkName.apk.classes
final String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;
//獲取dex數(shù)目
int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1);
final List<File> files = new ArrayList<File>(totalDexNumber);
//遍歷除主dex外的其他dex
for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) {
//文件名為 data/data/packageName/code_cache/secondary-dexes/data/data/apkName.apk.classes*.zip
String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;
//以zip文件形式返回
File extractedFile = new File(dexDir, fileName);
if (extractedFile.isFile()) {
//添加到list中并返回
files.add(extractedFile);
if (!verifyZipFile(extractedFile)) {
Log.i(TAG, "Invalid zip file: " + extractedFile);
throw new IOException("Invalid ZIP file.");
}
} else {
throw new IOException("Missing extracted secondary dex file '" +
extractedFile.getPath() + "'");
}
}
return files;
}
否則調(diào)用performExtractions()方法從apk中抽取dex文件
private static List<File> performExtractions(File sourceApk, File dexDir)
throws IOException {
final String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;
// Ensure that whatever deletions happen in prepareDexDir only happen if the zip that
// contains a secondary dex file in there is not consistent with the latest apk. Otherwise,
// multi-process race conditions can cause a crash loop where one process deletes the zip
// while another had created it.
prepareDexDir(dexDir, extractedFilePrefix);
List<File> files = new ArrayList<File>();
final ZipFile apk = new ZipFile(sourceApk);
try {
int secondaryNumber = 2;
//獲取classes2.dex
ZipEntry dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX);
while (dexFile != null) {
//data/data/packageName/code_cache/secondary-dexes/data/data/apkName.apk.classes*.zip
String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;
File extractedFile = new File(dexDir, fileName);
//添加到list列表中
files.add(extractedFile);
Log.i(TAG, "Extraction is needed for file " + extractedFile);
int numAttempts = 0;
boolean isExtractionSuccessful = false;
//最多重試3次
while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) {
numAttempts++;
// Create a zip file (extractedFile) containing only the secondary dex file
// (dexFile) from the apk.
//從apk中抽取classes*dex文件并重命名為zip文件保存到指定目錄
extract(apk, dexFile, extractedFile, extractedFilePrefix);
// Verify that the extracted file is indeed a zip file.
//判斷是否抽取成功
isExtractionSuccessful = verifyZipFile(extractedFile);
// Log the sha1 of the extracted zip file
Log.i(TAG, "Extraction " + (isExtractionSuccessful ? "success" : "failed") +
" - length " + extractedFile.getAbsolutePath() + ": " +
extractedFile.length());
if (!isExtractionSuccessful) {
// Delete the extracted file
extractedFile.delete();
if (extractedFile.exists()) {
Log.w(TAG, "Failed to delete corrupted secondary dex '" +
extractedFile.getPath() + "'");
}
}
}
if (!isExtractionSuccessful) {
throw new IOException("Could not create zip file " +
extractedFile.getAbsolutePath() + " for secondary dex (" +
secondaryNumber + ")");
}
//自增以讀取下一個classes*.dex文件
secondaryNumber++;
dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX);
}
} finally {
try {
apk.close();
} catch (IOException e) {
Log.w(TAG, "Failed to close resource", e);
}
}
return files;
}
抽取方法extract()
private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo,
String extractedFilePrefix) throws IOException, FileNotFoundException {
//獲取classes*.dex 對應(yīng)輸入流
InputStream in = apk.getInputStream(dexFile);
ZipOutputStream out = null;
//創(chuàng)建臨時文件
File tmp = File.createTempFile(extractedFilePrefix, EXTRACTED_SUFFIX,
extractTo.getParentFile());
Log.i(TAG, "Extracting " + tmp.getPath());
try {
//輸出為zip文件,zip文件中包含classes.dex
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmp)));
try {
ZipEntry classesDex = new ZipEntry("classes.dex");
// keep zip entry time since it is the criteria used by Dalvik
classesDex.setTime(dexFile.getTime());
out.putNextEntry(classesDex);
byte[] buffer = new byte[BUFFER_SIZE];
int length = in.read(buffer);
while (length != -1) {
out.write(buffer, 0, length);
length = in.read(buffer);
}
out.closeEntry();
} finally {
out.close();
}
Log.i(TAG, "Renaming to " + extractTo.getPath());
if (!tmp.renameTo(extractTo)) {
throw new IOException("Failed to rename \"" + tmp.getAbsolutePath() +
"\" to \"" + extractTo.getAbsolutePath() + "\"");
}
} finally {
closeQuietly(in);
tmp.delete(); // return status ignored
}
}
再回到MultiDex的install()方法中朋鞍,通過MultiDexExtractor.load()得到dex文件的zip列表后已添,調(diào)用installSecondaryDexes()
private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files)
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,
InvocationTargetException, NoSuchMethodException, IOException {
if (!files.isEmpty()) {
if (Build.VERSION.SDK_INT >= 19) {
V19.install(loader, files, dexDir);
} else if (Build.VERSION.SDK_INT >= 14) {
V14.install(loader, files, dexDir);
} else {
V4.install(loader, files);
}
}
}
根據(jù)sdk版本不同,調(diào)用對應(yīng)的方法滥酥,V19更舞、V14、V4都是MultiDex的內(nèi)部類坎吻,處理的邏輯也差不多缆蝉,這里主要看一下V19
/**
* Installer for platform versions 19.
*/
private static final class V19 {
private static void install(ClassLoader loader, List<File> additionalClassPathEntries,
File optimizedDirectory)
throws IllegalArgumentException, IllegalAccessException,
NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
/* The patched class loader is expected to be a descendant of
* dalvik.system.BaseDexClassLoader. We modify its
* dalvik.system.DexPathList pathList field to append additional DEX
* file entries.
*/
//獲取PathClassLoader的pathList成員變量,即DexPathList對象,其成員變量dexElements用于存儲dex文件相關(guān)信息
Field pathListField = findField(loader, "pathList");
Object dexPathList = pathListField.get(loader);
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
//調(diào)用makeDexElements方法刊头,內(nèi)部通過反射調(diào)用DexPathList的makeDexElements方法贝搁,返回dexElements
//參數(shù)為/code_cache/secondary-dexes緩存目錄中包含classes.dex的zip文件list以及優(yōu)化后的dex文件存放目錄
//expandFieldArray方法先獲取dexPathList對象的現(xiàn)有dexElements變量,然后建其和makeDexElements方法返回
//的dexElements數(shù)組合并芽偏,然后再將合并之后的結(jié)果設(shè)置為dexPathList對象的dexElements變量
expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList,
new ArrayList<File>(additionalClassPathEntries), optimizedDirectory,
suppressedExceptions));
if (suppressedExceptions.size() > 0) {
for (IOException e : suppressedExceptions) {
Log.w(TAG, "Exception in makeDexElement", e);
}
Field suppressedExceptionsField =
findField(loader, "dexElementsSuppressedExceptions");
IOException[] dexElementsSuppressedExceptions =
(IOException[]) suppressedExceptionsField.get(loader);
if (dexElementsSuppressedExceptions == null) {
dexElementsSuppressedExceptions =
suppressedExceptions.toArray(
new IOException[suppressedExceptions.size()]);
} else {
IOException[] combined =
new IOException[suppressedExceptions.size() +
dexElementsSuppressedExceptions.length];
suppressedExceptions.toArray(combined);
System.arraycopy(dexElementsSuppressedExceptions, 0, combined,
suppressedExceptions.size(), dexElementsSuppressedExceptions.length);
dexElementsSuppressedExceptions = combined;
}
suppressedExceptionsField.set(loader, dexElementsSuppressedExceptions);
}
}
/**
* A wrapper around
* {@code private static final dalvik.system.DexPathList#makeDexElements}.
*/
private static Object[] makeDexElements(
Object dexPathList, ArrayList<File> files, File optimizedDirectory,
ArrayList<IOException> suppressedExceptions)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Method makeDexElements =
findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class,
ArrayList.class);
return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,
suppressedExceptions);
}
}
makeDexElements()其實(shí)就是通過反射方式調(diào)用dexPathList對象的makeDexElements方法雷逆,將從dex添加到其dexElements屬性中,具體的過程在前面的文章中已經(jīng)介紹過—《android Dex文件的加載》污尉,這里不再贅述膀哲。
private static void expandFieldArray(Object instance, String fieldName,
Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
Field jlrField = findField(instance, fieldName);
Object[] original = (Object[]) jlrField.get(instance);
Object[] combined = (Object[]) Array.newInstance(
original.getClass().getComponentType(), original.length + extraElements.length);
System.arraycopy(original, 0, combined, 0, original.length);
System.arraycopy(extraElements, 0, combined, original.length, extraElements.length);
jlrField.set(instance, combined);
}
到這里MultiDex.install(this)方法的邏輯就分析完了,可以看到其中的處理步驟和上篇文章ant方式中我們手動加載從dex的方式基本上是一致的被碗,所以這兩種方式并沒有本質(zhì)上的區(qū)別某宪。