Skip to content

Commit

Permalink
Fix convert application
Browse files Browse the repository at this point in the history
  • Loading branch information
typ0520 committed Dec 12, 2017
1 parent 5201338 commit f72e460
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 236 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.8.5 (2017-12-12)

Bugfixes:

- 修复7.0以后版本Application强转报错的问题

## 0.8.4 (2017-11-29)

Bugfixes:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Android API 9(2.3)+ ; android-gradle-build 2.0.0+
}
dependencies {
classpath 'com.github.typ0520:fastdex-gradle:0.8.4'
classpath 'com.github.typ0520:fastdex-gradle:0.8.5'
}
}
````
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ class FastdexPlugin implements Plugin<Project> {
// Not in instant run mode, continue.
}

boolean proguardEnable = variant.getVariantData().getVariantConfiguration().getBuildType().isMinifyEnabled()
//TODO 暂时忽略开启混淆的buildType(目前的快照对比方案 无法映射java文件的类名和混淆后的class的类名)
if (proguardEnable) {
String buildTypeName = variant.getBuildType().buildType.getName()
project.logger.error("--------------------fastdex--------------------")
project.logger.error("fastdex android.buildTypes.${buildTypeName}.minifyEnabled=true, just ignore")
project.logger.error("--------------------fastdex--------------------")
boolean proguardEnable = variant.getVariantData().getVariantConfiguration().getBuildType().isMinifyEnabled()
boolean ignoreBuildType = configuration.onlyHookDebug && !"debug".equalsIgnoreCase(variant.buildType.name as String)
if (proguardEnable || ignoreBuildType) {
FastdexIgnoreTask ignoreTask = project.tasks.create("fastdex${variantName}",FastdexIgnoreTask)
ignoreTask.androidVariant = variant
ignoreTask.proguardEnable = proguardEnable
ignoreTask.ignoreBuildType = ignoreBuildType
}
else {
def javaCompile = variant.hasProperty('javaCompiler') ? variant.javaCompiler : variant.javaCompile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ class FastdexExtension {
* 当发送的补丁中包含dex时会调用 'adb shell am force-stop' 强制重启app
*/
boolean restartAppByCmd = true

/**
* 进hook debug这个build type
*/
boolean onlyHookDebug = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fastdex.build.task

import com.android.build.gradle.api.ApplicationVariant
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

/**
* Created by tong on 17/12/12.
*/
class FastdexIgnoreTask extends DefaultTask {
ApplicationVariant androidVariant
boolean proguardEnable
boolean ignoreBuildType

FastdexIgnoreTask() {
group = 'fastdex'
}

@TaskAction
def instantRun() {
String buildTypeName = androidVariant.getBuildType().buildType.getName()
project.logger.error("--------------------fastdex--------------------")
if (ignoreBuildType) {
project.logger.error("onlyHookDebug = true, build-type = ${buildTypeName}, just ignore")
}
else if (proguardEnable) {
project.logger.error("fastdex android.buildTypes.${buildTypeName}.minifyEnabled=true, just ignore")
}
project.logger.error("--------------------fastdex--------------------")
}
}
Binary file modified fastdex-gradle/src/main/resources/fastdex-runtime.dex
Binary file not shown.
2 changes: 1 addition & 1 deletion fastdex-runtime/src/main/java/fastdex/runtime/Fastdex.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private void loadPatch(FastdexApplication fastdexApplication,RuntimeMetaInfo met
PathClassLoader classLoader = (PathClassLoader) Fastdex.class.getClassLoader();
try {
Log.d(LOG_TAG,"apply dex patch: " + dexList);
SystemClassLoaderAdder.installDexes(fastdexApplication,classLoader,optDirectory,dexList);
SystemClassLoaderAdder.installDexes(classLoader,optDirectory,dexList);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package fastdex.runtime.loader;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Build;
import android.util.Log;
import java.io.File;
Expand All @@ -39,21 +38,16 @@
* Created by zhangshaowen on 16/3/18.
*/
public class SystemClassLoaderAdder {
public static final String CHECK_DEX_CLASS = "com.tencent.tinker.loader.TinkerTestDexLoad";
public static final String CHECK_DEX_FIELD = "isPatch";
private static final String TAG = "Tinker.ClassLoaderAdder";
private static final String TAG = "SystemClassLoaderAdder";
private static int sPatchDexCount = 0;

@SuppressLint("NewApi")
public static void installDexes(Application application, PathClassLoader loader, File dexOptDir, List<File> files)
public static void installDexes(PathClassLoader loader, File dexOptDir, List<File> files)
throws Throwable {
Log.i(TAG, "installDexes dexOptDir: " + dexOptDir.getAbsolutePath() + ", dex size:" + files.size());

if (!files.isEmpty()) {
ClassLoader classLoader = loader;
if (Build.VERSION.SDK_INT >= 24) {
classLoader = AndroidNClassLoader.inject(loader, application);
}
//because in dalvik, if inner class is not the same classloader with it wrapper class.
//it won't fail at dex2opt
if (Build.VERSION.SDK_INT >= 23) {
Expand All @@ -68,42 +62,9 @@ public static void installDexes(Application application, PathClassLoader loader,
//install done
sPatchDexCount = files.size();
Log.i(TAG, "after loaded classloader: " + classLoader + ", dex size:" + sPatchDexCount);
//
// if (!checkDexInstall(classLoader)) {
// //reset patch dex
// SystemClassLoaderAdder.uninstallPatchDex(classLoader);
// throw new FastdexRuntimeException(ShareConstants.CHECK_DEX_INSTALL_FAIL);
// }
}
}

public static void uninstallPatchDex(ClassLoader classLoader) throws Throwable {
if (sPatchDexCount <= 0) {
return;
}
if (Build.VERSION.SDK_INT >= 14) {
Field pathListField = ShareReflectUtil.findField(classLoader, "pathList");
Object dexPathList = pathListField.get(classLoader);
ShareReflectUtil.reduceFieldArray(dexPathList, "dexElements", sPatchDexCount);
} else {
ShareReflectUtil.reduceFieldArray(classLoader, "mPaths", sPatchDexCount);
ShareReflectUtil.reduceFieldArray(classLoader, "mFiles", sPatchDexCount);
ShareReflectUtil.reduceFieldArray(classLoader, "mZips", sPatchDexCount);
try {
ShareReflectUtil.reduceFieldArray(classLoader, "mDexs", sPatchDexCount);
} catch (Exception e) {
}
}
}

private static boolean checkDexInstall(ClassLoader classLoader) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class<?> clazz = Class.forName(CHECK_DEX_CLASS, true, classLoader);
Field filed = ShareReflectUtil.findField(clazz, CHECK_DEX_FIELD);
boolean isPatch = (boolean) filed.get(null);
Log.w(TAG, "checkDexInstall result:" + isPatch);
return isPatch;
}

/**
* Installer for platform versions 23.
*/
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
org.gradle.daemon=true

groupId=com.github.typ0520
version=0.8.4
version=0.8.5

ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=22
Expand Down
3 changes: 3 additions & 0 deletions sample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ fastdex {
//default 3
//当变化的java文件数量大于等于这个值时触发dex merge(随着变化的java文件的增多,补丁打包会越来越慢,dex merge以后当前的状态相当于全量打包以后的状态)
dexMergeThreshold = 3

//是否仅hook debug
onlyHookDebug = true
}

android {
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ buildscript {
maven { url 'https://maven.google.com' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:2.3.3'
//classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "me.tatarka:gradle-retrolambda:3.7.0"
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
classpath 'com.github.typ0520:fastdex-gradle:0.8.4'
classpath 'com.github.typ0520:fastdex-gradle:0.8.5'
}
}

Expand Down

0 comments on commit f72e460

Please sign in to comment.