ARouter系列3:继续学习(手写一个Arouter框架)(1)

4.1、module:apt-annotation


该module只新建了一个注解类。

// 声明注解的作用域

@Target(ElementType.TYPE)

// 声明注解的生命周期

@Retention(RetentionPolicy.CLASS)

public @interface BindPath {

String path();

}

4.2、module:apt-processor


apply plugin: ‘java-library’

dependencies {

implementation fileTree(dir: ‘libs’, include: [‘*.jar’])

implementation project(path: ‘:apt-annotation’)

annotationProcessor ‘com.google.auto.service:auto-service:1.0-rc4’

compileOnly ‘com.google.auto.service:auto-service:1.0-rc3’

}

sourceCompatibility = “1.7”

targetCompatibility = “1.7”

@AutoService(Processor.class)

public class Test1Processor extends AbstractProcessor {

Filer filer;

@Override

public synchronized void init(ProcessingEnvironment processingEnv) {

super.init(processingEnv);

filer = processingEnv.getFiler();

}

@Override

public SourceVersion getSupportedSourceVersion() {

return processingEnv.getSourceVersion();

}

@Override

public Set getSupportedAnnotationTypes() {

Set types=new HashSet<>();

types.add(BindPath.class.getCanonicalName());

return types;

}

@Override

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

// 获取到当前模块中用到了BindPath注解的activity的类对象(类节点) (有几个模块中依赖了apt-processor,该方法就会执行几次)

// 类节点 TypeElement 方法节点 ExecutableElement 方法节点 VariableElement

Set<? extends Element> elementAnnotationWith = roundEnv.getElementsAnnotatedWith(BindPath.class);

Map<String, String> map = new HashMap<>();

// 遍历整个模块中用到了BindPath注解的节点

for (Element element : elementAnnotationWith) {

TypeElement typeElement = (TypeElement) element;

// 获取到activity上面的 BindPath 的注解

BindPath annotation = typeElement.getAnnotation(BindPath.class);

// 获取到注解里面带的值 中间容器 map 的activity所对应的 key

String key = annotation.path();

// 获取到包名和类名

Name activityName = typeElement.getQualifiedName();

map.put(key, activityName + “.class”);

}

// 写文件

if (map.size() > 0) {

Writer writer = null;

// 需要生成的文件名 让类名不重复

String activityName = “ActivityUtil” + System.currentTimeMillis();

try {

// 生成一个Java文件

JavaFileObject sourceFile = filer.createSourceFile(“com.gs.util.” + activityName);

// 从生成的这个文件开始写

writer = sourceFile.openWriter();

StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append(“package com.gs.util;\n”);

stringBuffer.append(“import com.gs.arouter.ARouterTest;\n” +

“import com.gs.arouter.IRouterTest;\n” +

“\n” +

“public class " + activityName + " implements IRouterTest{\n” +

" @Override\n" +

" public void putActivity(){\n");

Iterator iterator = map.keySet().iterator();

while (iterator.hasNext()) {

String key = iterator.next();

String className = map.get(key);

stringBuffer.append(" ARouterTest.getInstance().addActivity(“” + key + “”, " +

className + “);”);

}

stringBuffer.append(“\n }\n}”);

writer.write(stringBuffer.toString());

} catch (IOException e) {

e.printStackTrace();

} finally {

if (writer != null) {

try {

writer.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

return false;

}

}

这个很重要,不要搞错了。

4.3、module:arouter


4.3.1、IRouterTest.java

public interface IRouterTest {

void putActivity();

}

4.3.2、ARouterConstant.java

public class ARouterConstant {

public static final String AROUTER_PARAM_LOGIN_ACTIVITY = “login/LoginActivity”;

public static final String AROUTER_PARAM_UCMAIN_ACTIVITY = “uc/UCMainActivity”;

}

4.3.3、 ARouterTest.java

public class ARouterTest {

public volatile static ARouterTest aRouterTest;

private Context context;

private Map<String, Class<? extends Activity>> map;

private ARouterTest() {

map = new HashMap<>();

}

public static ARouterTest getInstance() {

if (aRouterTest == null) {

synchronized (ARouterTest.class) {

if (aRouterTest == null) {

aRouterTest = new ARouterTest();

}

}

}

return aRouterTest;

}

public void init(Context context) {

this.context = context;

List classNames = getClassName(“com.gs.util”);

for (String className : classNames) {

try {

Class<?> clazz = Class.forName(className);

if (IRouterTest.class.isAssignableFrom(clazz)) {

IRouterTest iRouterTest = (IRouterTest) clazz.newInstance();

iRouterTest.putActivity();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

public void addActivity(String key, Class<? extends Activity> clazz) {

if (key != null && clazz != null && !map.containsKey(key)) {

map.put(key, clazz);

}

}

public void jumpActivity(String key, Bundle bundle) {

Class<? extends Activity> classActivity = map.get(key);

if (classActivity != null) {

Intent intent = new Intent(context, classActivity);

if (bundle != null) {

intent.putExtras(bundle);

}

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);

}

}

/**

  • 通过包名获取这个包下面的所有类名

  • @param packageName 包名

  • @return a list of class

*/

private List getClassName(String packageName) {

List classList = new ArrayList<>();

String path = null;

try {

path = context.getPackageManager()

.getApplicationInfo(context.getPackageName(), 0)

.sourceDir;

DexFile dexFile = new DexFile(path);

Enumeration entries = dexFile.entries();

while (entries.hasMoreElements()) {

String name = (String) entries.nextElement();

if (name.contains(packageName)) {

classList.add(name);

}

}

} catch (Exception e) {

e.printStackTrace();

}

return classList;

}

}

4.4、使用


4.4.1、module:app

App.java

public class App extends Application {

@Override

public void onCreate() {

super.onCreate();

ARouterTest.getInstance().init(this);

}

}

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

ARouterTest.getInstance().jumpActivity(ARouterConstant.AROUTER_PARAM_LOGIN_ACTIVITY, null);

}

});

}

}

activity_main

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”>

<Button

android:id=“@+id/btn1”

style=“@style/BtnCommon”

android:text=“1、跳转到LoginActivity” />

4.4.2、module:login

LoginActivity.java

@BindPath(path = ARouterConstant.AROUTER_PARAM_LOGIN_ACTIVITY)

public class LoginActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

ARouterTest.getInstance()

.jumpActivity(ARouterConstant.AROUTER_PARAM_UCMAIN_ACTIVITY, null);

}

});

}

}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“This is LoginActivity.”

android:textSize=“20sp”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

<Button

android:id=“@+id/btn1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“跳转到 UCMainActivity”

android:textAllCaps=“false”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintVertical_bias=“0.418” />

</androidx.constraintlayout.widget.ConstraintLayout>

4.4.3、module:usercenter

UCMainActivity.java

@BindPath(path = ARouterConstant.AROUTER_PARAM_UCMAIN_ACTIVITY)

public class UCMainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_u_c_main);

}

}

activity_u_c_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.UCMainActivity”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“This is UCMainActivity.”

android:textSize=“20sp”

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
=“wrap_content”

android:layout_height=“wrap_content”

android:text=“This is UCMainActivity.”

android:textSize=“20sp”

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-SC8j4gmg-1715501082542)]

[外链图片转存中…(img-ZAS35j5I-1715501082544)]

[外链图片转存中…(img-W7AOCYMw-1715501082545)]

[外链图片转存中…(img-mkmvaXRz-1715501082546)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值