Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#649 #650

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

#649 #650

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/externalDex/make_patchdex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

dx --dex --output patch.dex classes

adb push patch.dex /data/local/tmp/arouter/external/patch.dex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.core.LogisticsCenter;
import com.alibaba.android.arouter.demo.extrasdex.ExternalDexProvider;
import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
import com.alibaba.android.arouter.demo.testinject.TestSerializable;
Expand All @@ -21,7 +23,9 @@
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,6 +67,8 @@ public void onClick(View v) {
// 发阶段,线上开启调试模式有安全风险,可以使用BuildConfig.DEBUG
// 来区分环境
ARouter.openDebug();
// Add external dex( push your patch dex here)
LogisticsCenter.setExtraDexList(collectExtraDexPath("/data/local/tmp/arouter/external"));
ARouter.init(getApplication());
break;
case R.id.normalNavigation:
Expand Down Expand Up @@ -152,6 +158,15 @@ public void onInterrupt(Postcard postcard) {
.withObject("map", map)
.navigation();
break;

case R.id.loadExternalDexRoute:
// Before using outer route , you must make sure load your outer dex impl ...

ARouter.getInstance()
.navigation(ExternalDexProvider.class)
.doSomethings();
break;

case R.id.navByName:
((HelloService) ARouter.getInstance().build("/yourservicegroupname/hello").navigation()).sayHello("mike");
break;
Expand Down Expand Up @@ -214,6 +229,24 @@ public void onInterrupt(Postcard postcard) {
}
}

private static List<String> collectExtraDexPath(String... extraDexDir) {
List<String> extraDexPaths = Collections.emptyList();
if (extraDexDir != null) {
extraDexPaths = new ArrayList<>();

for (String dir : extraDexDir) {
File dexDir = new File(dir);
if (dexDir.exists() && dexDir.isDirectory()) {
File[] listFiles = dexDir.listFiles();
for (File dexFile : listFiles) {
extraDexPaths.add(dexFile.getAbsolutePath());
}
}
}
}
return extraDexPaths;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.alibaba.android.arouter.demo.extrasdex;

import com.alibaba.android.arouter.facade.template.IProvider;

/**
* This is external dex
*/
public interface ExternalDexProvider extends IProvider {
void doSomethings();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.android.arouter.demo.extrasdex;


import android.content.Context;
import android.widget.Toast;

import com.alibaba.android.arouter.demo.extrasdex.ExternalDexProvider;
import com.alibaba.android.arouter.facade.annotation.Route;

@Route(path = "/extras/external/patch/route")
public class ExternalDexProviderImpl implements ExternalDexProvider {

private Context mContext;

@Override
public void init(Context context) {
mContext = context;
}

@Override
public void doSomethings() {
Toast.makeText(mContext, " Here will be used by hot swap code.", Toast.LENGTH_SHORT).show();
}
}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="依赖注入(参照代码)" />


<Button
android:id="@+id/loadExternalDexRoute"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="load External dex route" />
</LinearLayout>

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.android.arouter.utils.TextUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -53,10 +54,12 @@ public class LogisticsCenter {
private static Context mContext;
static ThreadPoolExecutor executor;
private static boolean registerByPlugin;
private static List<String> extraDexList;

/**
* arouter-auto-register plugin will generate code inside this method
* call this method to register all Routers, Interceptors and Providers
*
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
* @since 2017-12-06
*/
Expand All @@ -72,8 +75,9 @@ private static void loadRouterMap() {
* register by class name
* Sacrificing a bit of efficiency to solve
* the problem that the main dex file size is too large
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
*
* @param className class name
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
*/
private static void register(String className) {
if (!TextUtils.isEmpty(className)) {
Expand All @@ -91,13 +95,14 @@ private static void register(String className) {
+ " should implements one of IRouteRoot/IProviderGroup/IInterceptorGroup.");
}
} catch (Exception e) {
logger.error(TAG,"register class error:" + className);
logger.error(TAG, "register class error:" + className);
}
}
}

/**
* method for arouter-auto-register plugin to register Routers
*
* @param routeRoot IRouteRoot implementation class in the package: com.alibaba.android.arouter.core.routers
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
* @since 2017-12-06
Expand All @@ -111,6 +116,7 @@ private static void registerRouteRoot(IRouteRoot routeRoot) {

/**
* method for arouter-auto-register plugin to register Interceptors
*
* @param interceptorGroup IInterceptorGroup implementation class in the package: com.alibaba.android.arouter.core.routers
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
* @since 2017-12-06
Expand All @@ -124,6 +130,7 @@ private static void registerInterceptor(IInterceptorGroup interceptorGroup) {

/**
* method for arouter-auto-register plugin to register Providers
*
* @param providerGroup IProviderGroup implementation class in the package: com.alibaba.android.arouter.core.routers
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
* @since 2017-12-06
Expand All @@ -137,6 +144,7 @@ private static void registerProvider(IProviderGroup providerGroup) {

/**
* mark already registered by arouter-auto-register plugin
*
* @author billy.qi <a href="mailto:[email protected]">Contact me.</a>
* @since 2017-12-06
*/
Expand All @@ -146,6 +154,10 @@ private static void markRegisteredByPlugin() {
}
}

public static void setExtraDexList(List<String> extraDexList) {
LogisticsCenter.extraDexList = extraDexList;
}

/**
* LogisticsCenter init, load all metas in memory. Demand initialization
*/
Expand All @@ -167,7 +179,7 @@ public synchronized static void init(Context context, ThreadPoolExecutor tpe) th
if (ARouter.debuggable() || PackageUtils.isNewVersion(context)) {
logger.info(TAG, "Run with debug mode or new install, rebuild router map.");
// These class was generated by arouter-compiler.
routerMap = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE);
routerMap = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE, extraDexList);
if (!routerMap.isEmpty()) {
context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).edit().putStringSet(AROUTER_SP_KEY_MAP, routerMap).apply();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -57,10 +58,14 @@ private static SharedPreferences getMultiDexPreferences(Context context) {
* @param packageName 包名
* @return 所有class的集合
*/
public static Set<String> getFileNameByPackageName(Context context, final String packageName) throws PackageManager.NameNotFoundException, IOException, InterruptedException {
public static Set<String> getFileNameByPackageName(Context context, final String packageName, List<String> extraDex) throws PackageManager.NameNotFoundException, IOException, InterruptedException {
final Set<String> classNames = new HashSet<>();

List<String> paths = getSourcePaths(context);
if (extraDex != null) {
paths.addAll(extraDex);
}

final CountDownLatch parserCtl = new CountDownLatch(paths.size());

for (final String path : paths) {
Expand Down