1.点击商城进入ShoppingModule
2.点击登录进入LoginModule
3.点击账单红色区域展示账单列表(其他Module中的Fragment)
(shareModule为公共模块)
根据业务需求创建如下:
在App的gradle.properties文件下添加,用于控制module是否独立运行。
#配置某个组件是否可以独立运行
isShoppingRunAlone = true
isLoginRunALone = true
然后配置App build.gradle。
apply plugin: ‘com.android.application’
apply plugin: ‘kotlin-android’
apply plugin: ‘kotlin-android-extensions’
android {
compileSdkVersion 30
buildToolsVersion “29.0.3”
defaultConfig {
applicationId “com.example.moduledemo”
minSdkVersion 16
targetSdkVersion 30
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 “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”
implementation ‘androidx.core:core-ktx:1.3.0’
implementation ‘androidx.appcompat:appcompat:1.1.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 project(‘:ShareModule’)
// 根据gradle中的配置来决定是否引用module
if (!isLoginRunALone.toBoolean()){
implementation project(‘:LoginModule’)
}
if (!isShoppingRunAlone.toBoolean()){
implementation project(‘:ShoppingModule’)
}
}
继续配置其他Module的build.gradle文件。
if (isShoppingRunAlone.toBoolean()){
apply plugin: ‘com.android.application’
}else {
apply plugin: ‘com.android.library’
}
apply plugin: ‘kotlin-android’
apply plugin: ‘kotlin-android-extensions’
android {
compileSdkVersion 30
buildToolsVersion “29.0.3”
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName “1.0”
testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner”
consumerProguardFiles “consumer-rules.pro”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}
}
sourceSets{
main{
// 在独立运行或者作为Libarary调试时,使用不同的AndroidManifest.xml文件
if (isShoppingRunAlone.toBoolean()){
manifest.srcFile ‘src/main/manifest/AndroidManifest.xml’
}else {
manifest.srcFile ‘src/main/AndroidManifest.xml’
}
}
}
}
dependencies {
implementation fileTree(dir: “libs”, include: [“*.jar”])
implementation “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”
implementation ‘androidx.core:core-ktx:1.3.0’
implementation ‘androidx.appcompat:appcompat:1.1.0’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘androidx.test.ext:junit:1.1.1’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.2.0’
//导入公共模块
implementation project(‘:ShareModule’)
}
在不同运行模式下使用不同的Manifest文件。
需要在对应module的Main目录下新建manifest文件夹(不然单独运行会找不到Manifest文件)。
单独运行的manifest文件设置如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.example.loginmodule”>
<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”>
并入主Module运行时manifest文件设置如下:
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.example.shoppingmodule”>
全部配置完成之后,可以在gradle.properties中修改变量的值,编译查看配置是否正确,manifest文件是否替换。运行查看是否正常。
接下来开始配置路由。
好多人心中有疑惑,在引用Module之后是可以直接获取到子Module的Activity的为什么还要使用路由跳转。是因为组件化开发是为了使单独Module可以独自编译,运行如果主Module引用子Module的类名,当子Module单独运行时主Module会编译异常。
我们要知道一个项目不可能只有一个子Module,当我们其他子Module要进行相互跳转时如何使用路由呢?所以我们要在ShareModule进行路由的配置,在之前的配置中我们将ShareModule导入了每个Module。
第一步
我们创建对应Module的跳转模板
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
public interface ILoginService {
void launch(Context ctx, String targetClass);
}
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
public interface IShoppingService {
void launch(Context ctx, String string);
Fragment newBillFragment(FragmentManager fragmentManager, int viewId, Bundle bundle);
}
第二步
在对应的moudle中实现跳转逻辑及传值操作
package com.example.loginmodule;
import android.content.Context;
import android.content.Intent;
import com.example.sharemodule.ILoginService;
public class LoginService implements ILoginService {
@Override
public void launch(Context ctx, String targetClass) {
Intent intent = new Intent(ctx, LoginActivity.class);
ctx.startActivity(intent);
}
}
package com.example.shoppingmodule;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.example.sharemodule.IShoppingService;
public class ShoppingService implements IShoppingService {
@Override
public void launch(Context ctx, String string) {
Intent intent = new Intent(ctx, ShoppingActivity.class);
ctx.startActivity(intent);
}
@Override
public Fragment newBillFragment(FragmentManager fragmentManager, int viewId, Bundle bundle) {
BillFragment fragment = new BillFragment();
fragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(viewId, fragment).commit();
return fragment;
}
}
第三步
接下来我们创建一个ServiceFactory,为我们提供跳转实例,并且处理单独运行时可能会出现的异常
package com.example.sharemodule;
public class ServiceFactory {
private static final ServiceFactory instance = new ServiceFactory();
private ILoginService mLoginService;
private IShoppingService mShoppingService;
private ServiceFactory(){}
public static ServiceFactory getInstance() {
return instance;
}
public ILoginService getLoginService() {
if (mLoginService == null){
mLoginService = new EmptyLoginService();
}
return mLoginService;
}
public void setLoginService(ILoginService mLoginService) {
this.mLoginService = mLoginService;
}
public IShoppingService getSignService() {
if (mShoppingService == null){
mShoppingService = new EmptyShoppingService();
}
return mShoppingService;
}
public void setSignService(IShoppingService mSignService) {
this.mShoppingService = mSignService;
}
}
package com.example.mylibrarySharedLibrary;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
public class EmptyLoginService implements ILoginService {
@Override
public void launch(Context ctx, String targetClass) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
总结
这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
总结
这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-2JNPyqz2-1712426410496)]
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…