手动组件化+ARouter

第一步先创建一个Module 使其组件化
在这里插入图片描述
在main文件夹下创建两个文件夹(debug,release)分别存放两份清单文件

在这里插入图片描述
release文件下的清单文件要把activity的入口去掉 当作为依赖的时候 是没有入口的
debug文件下的不需要 可以用来当做Module运行代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bw.test3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".test3Activity">
        </activity>
    </application>

</manifest>

在项目工程下的gradle.properties中添加一个常量

在这里插入图片描述
Module下的build.gradle添加一下代码

if (istest3Module.toBoolean())
apply plugin: 'com.android.application'
else apply plugin: 'com.android.library'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    sourceSets{
        main{
            if (istest3Module.toBoolean())
                manifest.srcFile'src/main/debug/AndroidManifest.xml'
            else manifest.srcFile'src/main/debug/AndroidManifest.xml'
        }
    }

    defaultConfig {
        if (istest3Module.toBoolean())
        applicationId "com.bw.test3"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

在app的
在这里插入图片描述
添加依赖

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation project(path: ':test1')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation ('com.alibaba:arouter-api:1.4.1')
    annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
    if (!istest1Module.toBoolean())
        implementation project(':test1')
    if (!istest2Module.toBoolean())
        implementation project(':test2')
    if (!istest3Module.toBoolean())
        implementation project(':test3')
}

组件化结束

开始跳转

先继承依赖
在自己创建的module和app中都要添加以下依赖直到依赖结束

    defaultConfig {
        if (istest3Module.toBoolean())
        applicationId "com.bw.test3"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        //在defaultConfig下添加如下五行代码
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }
        //到此结束
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

依赖添加

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//添加的依赖以下两句代码
    implementation ('com.alibaba:arouter-api:1.4.1')
    annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
}

依赖结束

在app手动创建Java类继承Application 。初始化ARouter

public class Mainapp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //开启控制台信息
        ARouter.openLog();
        //开启调试模式
        ARouter.openDebug();
        //初始化
        ARouter.init(this);
    }
}

在自己创建module下的mainActivity(主页面)java代码添加注解下图所示

//这里的路径需要注意的是至少需要有两级,/xx/xx一般是本类名+上一级文件夹名
@Route(path = "/test3/test3Activity")
public class test3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3);
        //使用类进行注册
        ARouter.getInstance().inject(this);
    }
}

app目录下清单文件加上name

     <application
        android:name=".Mainapp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

app布局添加点击事件开始进行跳转

public class MainActivity extends AppCompatActivity {
    private TextView test;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test = (TextView) findViewById(R.id.test);
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
				//可进行传值
                 ARouter.getInstance().build("/test3/test3Activity").withString("123","魏铭池").navigation();
            }
        });
    }
}

运行项目检测是否报错
结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值