Skip to content

Commit

Permalink
Merge pull request #127 from chenenyu/dev
Browse files Browse the repository at this point in the history
1.6.3
  • Loading branch information
chenenyu committed May 25, 2019
2 parents 8d1d165 + 3c257a4 commit 9f79bdf
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ before_install:

# workaround for "http://stackoverflow.com/questions/37615379/travis-ci-build-doesnt-work-with-android-constraint-layout"
- mkdir "$ANDROID_HOME/licenses" || true
- echo -e "\nd56f5187479451eabf01fb78af6dfcb131a6481e" > "$ANDROID_HOME/licenses/android-sdk-license"
- echo -e "\n24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license"
- echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"

script:
Expand Down
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,6 @@ Router.build("test").getFragment();

建议浏览 [wiki](https://github.com/chenenyu/Router/wiki).


## 谁在使用Router

<div>
<a href="http://sj.qq.com/myapp/detail.htm?apkName=com.sankuai.erp.mcashier">
<img src="static/美团轻收银.png" width="100"/>
</a>
<a href="http://www.sixiangyun.cn/">
<img src="static/私享云.png" width="100"/>
</a>
<a href="#">
<img src="static/恒大智能家居.png" width="100"/>
</a>
<a href="https://fir.im/shouba">
<img src="static/批车吧.png" width="100"/>
</a>
<a href="https://fir.im/ebeilun">
<img src="static/e乡北仑.png" width="100"/>
</a>
<a href="https://fir.im/duihuan">
<img src="static/硬币自循环.png" width="100"/>
</a>
</div>

## 讨论

QQ group: 271849001
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.3.21'
ext.kotlin_version = '1.3.31'
repositories {
maven { url 'repo' }
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
if (Boolean.valueOf(applyRemotePlugin)) {
Expand All @@ -28,7 +28,7 @@ task clean(type: Delete) {

ext {
compileSdkVersion = 28
minSdkVersion = 14
minSdkVersion = 16
targetSdkVersion = 28

supportVersion = "28.0.0"
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (!Boolean.valueOf(applyRemotePlugin)) {
dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.android.tools.build:gradle:3.2.0'
compile 'com.android.tools.build:gradle:3.4.1'
}

sourceSets {
Expand Down
12 changes: 1 addition & 11 deletions gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'groovy'
apply plugin: 'maven-publish'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -12,7 +11,7 @@ repositories {
dependencies {
compile gradleApi()
compile localGroovy()
compileOnly 'com.android.tools.build:gradle:3.2.0'
compileOnly 'com.android.tools.build:gradle:3.4.1'
}

ext {
Expand All @@ -22,13 +21,4 @@ ext {
BINTRAY_NAME = 'router-gradle-plugin'
}

//publishing {
// // 定义目标仓库
// repositories {
// maven {
// url "${project.rootProject.projectDir.absolutePath}/repo"
// }
// }
//}

apply from: 'https://raw.githubusercontent.com/chenenyu/gradle-bintray-plugin/master/publish.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import org.gradle.api.plugins.ExtraPropertiesExtension
class RouterPlugin implements Plugin<Project> {
static final String APT_OPTION_NAME = "moduleName"

String DEFAULT_ROUTER_RUNTIME_VERSION = "1.6.2"
String DEFAULT_ROUTER_COMPILER_VERSION = "1.6.2"
String DEFAULT_ROUTER_RUNTIME_VERSION = "1.6.3"
String DEFAULT_ROUTER_COMPILER_VERSION = "1.6.3"

String androidBuildGradleVersion

Expand Down Expand Up @@ -54,9 +54,9 @@ class RouterPlugin implements Plugin<Project> {
}
}

String compileConf = 'compile'
if (is3_xVersion()) {
compileConf = 'implementation'
String compileConf = 'implementation'
if (!is3_xVersion()) {
compileConf = 'compile'
}
String aptConf = 'annotationProcessor'
if (isKotlinProject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.chenenyu.router

import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.ide.common.internal.WaitableExecutor
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import org.gradle.api.Project

import java.util.concurrent.Callable

/**
* Created by chenenyu on 2018/7/24.
*/
Expand All @@ -14,8 +17,11 @@ class RouterTransform extends Transform {

Project project

WaitableExecutor waitableExecutor

RouterTransform(Project project) {
this.project = project
this.waitableExecutor = WaitableExecutor.useGlobalSharedThreadPool()
}

@Override
Expand Down Expand Up @@ -53,47 +59,30 @@ class RouterTransform extends Transform {
long begin = System.currentTimeMillis()
project.logger.info("- router transform begin:")

Scanner.clearRecordsClasses()

transformInvocation.inputs.each { TransformInput input ->
if (!input.jarInputs.empty) {
project.logger.info("-- jarInputs:")
input.jarInputs.each { JarInput jarInput ->
// com.android.support:appcompat-v7:27.1.1 (/path/to/xxx.jar)
project.logger.info("--- ${jarInput.name} (${jarInput.file.absolutePath})")
String destName = jarInput.name
String hexName = DigestUtils.md5Hex(jarInput.file.absolutePath)
if (destName.endsWith(".jar")) { // local jar
// rename to avoid the same name, such as classes.jar
destName = "${destName.substring(0, destName.length() - 4)}_${hexName}"
execute {
transformJar(transformInvocation, jarInput)
}
File destFile = transformInvocation.outputProvider.getContentLocation(
destName, jarInput.contentTypes, jarInput.scopes, Format.JAR)
if (Scanner.shouldScanJar(jarInput)) {
Scanner.scanJar(jarInput.file, destFile)
}

FileUtils.copyFile(jarInput.file, destFile)
}
}

if (!input.directoryInputs.empty) {
project.logger.info("-- directoryInputs:")
input.directoryInputs.each { DirectoryInput directoryInput ->
project.logger.info("-- directory: ${directoryInput.name} (${directoryInput.file.absolutePath})")
File dest = transformInvocation.outputProvider.getContentLocation(
directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
project.logger.info("-- dest dir: ${dest.absolutePath}")
directoryInput.file.eachFileRecurse { File file ->
if (file.isFile() && Scanner.shouldScanClass(file)) {
project.logger.info("--- ${file.absolutePath}")
Scanner.scanClass(file)
}
execute {
transformDir(transformInvocation, directoryInput)
}

FileUtils.copyDirectory(directoryInput.file, dest)
}
}
}

waitableExecutor.waitForAllTasks()

// 找到了AptHub.class 向其注入代码
if (registerTargetFile) {
project.logger.info("begin to register code to ${registerTargetFile.absolutePath}")
Expand All @@ -104,4 +93,47 @@ class RouterTransform extends Transform {
project.logger.info("- router transform finish.")
project.logger.info("cost time: ${(System.currentTimeMillis() - begin) / 1000.0f}s")
}

void transformJar(TransformInvocation transformInvocation, JarInput jarInput) {
// com.android.support:appcompat-v7:27.1.1 (/path/to/xxx.jar)
project.logger.info("--- ${jarInput.name} (${jarInput.file.absolutePath})")
String destName = jarInput.name
if (destName.endsWith(".jar")) { // local jar
// rename to avoid the same name, such as classes.jar
String hexName = DigestUtils.md5Hex(jarInput.file.absolutePath)
destName = "${destName.substring(0, destName.length() - 4)}_${hexName}"
}
File destFile = transformInvocation.outputProvider.getContentLocation(
destName, jarInput.contentTypes, jarInput.scopes, Format.JAR)
if (Scanner.shouldScanJar(jarInput)) {
Scanner.scanJar(jarInput.file, destFile)
}

FileUtils.copyFile(jarInput.file, destFile)
}

void transformDir(TransformInvocation transformInvocation, DirectoryInput directoryInput) {
project.logger.info("-- directory: ${directoryInput.name} (${directoryInput.file.absolutePath})")
File dest = transformInvocation.outputProvider.getContentLocation(
directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
project.logger.info("-- dest dir: ${dest.absolutePath}")
directoryInput.file.eachFileRecurse { File file ->
if (file.isFile() && Scanner.shouldScanClass(file)) {
project.logger.info("--- ${file.absolutePath}")
Scanner.scanClass(file)
}
}

FileUtils.copyDirectory(directoryInput.file, dest)
}

void execute(Closure closure) {
waitableExecutor.execute(new Callable<Void>() {
@Override
Void call() throws Exception {
closure()
return null
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class Scanner {
private static
final Set<String> excludeJar = ["com.android.support", "android.arch.", "androidx."]

static void clearRecordsClasses() {
records.forEach { record ->
record.aptClasses.clear()
}
}

static boolean shouldScanJar(JarInput jarInput) {
excludeJar.each {
if (jarInput.name.contains(it))
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ org.gradle.jvmargs=-Xmx1536m
# apply router plugin
applyRemotePlugin=false
# router gradle plugin version
PLUGIN_VERSION=1.6.2
PLUGIN_VERSION=1.6.3
# router library version
ROUTER_VERSION=1.6.2
ROUTER_VERSION=1.6.3
# compiler library version
COMPILER_VERSION=1.6.2
COMPILER_VERSION=1.6.3
# annotation library version
ANNOTATION_VERSION=0.4.0
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Mar 07 12:02:40 CST 2019
#Mon Apr 29 17:02:49 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
19 changes: 12 additions & 7 deletions router/src/main/java/com/chenenyu/router/RealInterceptorChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class RealInterceptorChain implements RouteInterceptor.Chain {
@Nullable
private Class<?> targetClass; // Intent/Fragment class
@Nullable
private Object targetObject; // Intent/Fragment instance
private Object targetInstance; // Intent/Fragment instance

RealInterceptorChain(@NonNull Object source,
@NonNull RouteRequest request,
Expand Down Expand Up @@ -91,23 +91,28 @@ public void setTargetClass(@Nullable Class<?> targetClass) {
this.targetClass = targetClass;
}

public void setTargetObject(@Nullable Object targetObject) {
this.targetObject = targetObject;
public void setTargetInstance(@Nullable Object targetInstance) {
this.targetInstance = targetInstance;
}

@Nullable
public Object getTargetInstance() {
return targetInstance;
}

@NonNull
@Override
public RouteResponse process() {
if (index >= interceptors.size()) {
if (interceptors.isEmpty()) {
RouteResponse response = RouteResponse.assemble(RouteStatus.SUCCEED, null);
if (targetObject != null) {
response.setResult(targetObject);
if (targetInstance != null) {
response.setResult(targetInstance);
} else {
response.setStatus(RouteStatus.FAILED);
}
return response;
}
RouteInterceptor interceptor = interceptors.get(index++);
RouteInterceptor interceptor = interceptors.remove(0);
return interceptor.intercept(this);
}

Expand Down
12 changes: 7 additions & 5 deletions router/src/main/java/com/chenenyu/router/RealRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
import android.support.v4.app.ActivityCompat;

import com.chenenyu.router.chain.AppInterceptorsHandler;
import com.chenenyu.router.chain.AttrsProcessor;
import com.chenenyu.router.chain.BaseValidator;
import com.chenenyu.router.chain.FragmentProcessor;
import com.chenenyu.router.chain.FragmentValidator;
import com.chenenyu.router.chain.IntentProcessor;
import com.chenenyu.router.chain.IntentValidator;
import com.chenenyu.router.util.RLog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
Expand All @@ -33,6 +34,7 @@ final class RealRouter extends AbsRouter {
private final RouteInterceptor mIntentProcessor = new IntentProcessor();
private final RouteInterceptor mFragmentProcessor = new FragmentProcessor();
private final RouteInterceptor mAppInterceptorsHandler = new AppInterceptorsHandler();
private final RouteInterceptor mAttrsProcessor = new AttrsProcessor();

private static final ThreadLocal<RealRouter> mRouterThreadLocal = new ThreadLocal<RealRouter>() {
@Override
Expand Down Expand Up @@ -60,9 +62,9 @@ private void callback(RouteResponse response) {

@Override
public Object getFragment(@NonNull Object source) {
List<RouteInterceptor> interceptors = new ArrayList<>();
List<RouteInterceptor> interceptors = new LinkedList<>();
Collections.addAll(interceptors, mBaseValidator, mFragmentValidator,
mFragmentProcessor, mAppInterceptorsHandler);
mFragmentProcessor, mAppInterceptorsHandler, mAttrsProcessor);
RealInterceptorChain chain = new RealInterceptorChain(source, mRouteRequest, interceptors);
RouteResponse response = chain.process();
callback(response);
Expand All @@ -71,9 +73,9 @@ public Object getFragment(@NonNull Object source) {

@Override
public Intent getIntent(@NonNull Object source) {
List<RouteInterceptor> interceptors = new ArrayList<>();
List<RouteInterceptor> interceptors = new LinkedList<>();
Collections.addAll(interceptors, mBaseValidator, mIntentValidator,
mIntentProcessor, mAppInterceptorsHandler);
mIntentProcessor, mAppInterceptorsHandler, mAttrsProcessor);
RealInterceptorChain chain = new RealInterceptorChain(source, mRouteRequest, interceptors);
RouteResponse response = chain.process();
callback(response);
Expand Down

0 comments on commit 9f79bdf

Please sign in to comment.