组件化是建立在模块化思想上的一次演进,一个变种。组件化本来就是模块化的概念。核心是模块角色的可转化换性,在打包时,是library;调试时,是Application。
组件化的单位是组件,这里跟模块化的一个最大的区别就是组件之间是可以相互进行跳转的,其实现就是通过路由,这个在之后会手动来实现这个路由的功能。它是一个编译时的行为。
apply plugin: 'com.Android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
1234567891011121314151617181920212223242526272829303132333435
package com.example.componentlib2;
public class AppConfig {
public static final String[] COMPONENTS = {
"com.example.logincomponent.LoginApplication",
"com.example.minecomponent.MineApplication"
};
}
123456789
package com.example.componentlib2;
import android.app.Application;
public interface IAppComponent {
void initalliza(Application app);
}
12345678
package com.example.componentlib2;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
public interface ILoginService {
// 跳转 从主 app 跳转到 login app
void launch(Context context, String targetClass);
//获取数据主app从login app 获取返回数据并显示
Fragment newUserInfoFragment(FragmentManager fragmentManager, int viewId, Bundle bundle);
}
1234567891011121314151617
package com.example.componentlib2;
import android.content.Context;
public interface IMineService {
void launch(Context context,int userId);
}
12345678
package com.example.componentlib2;
public class ServiceFactory {
private static final ServiceFactory instance = new ServiceFactory();
public static ServiceFactory getInstance() {
return instance;
}
private ServiceFactory() {
}
private ILoginService mLoginService;
private IMineService mMineService;
public ILoginService getLoginService() {
return mLoginService;
}
public void setLoginService(ILoginService mLoginService) {
this.mLoginService = mLoginService;
}
public IMineService getMineService() {
return mMineService;
}
public void setMineService(IMineService mMineService) {
this.mMineService = mMineService;
}
}
12345678910111213141516171819202122232425262728293031323334
package com.example.logincomponent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class LoginActivity extends AppCompatActivity {
public static String EXTRA_TARGET_CLASS = "extra_target_class";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_mAIn);
}
}
123456789101112131415
package com.example.logincomponent;
import android.app.Application;
import com.example.componentlib2.IAppComponent;
import com.example.componentlib2.ServiceFactory;
public class LoginApplication extends Application implements IAppComponent {
private static Application application;
public static Application getApplication() {
return application;
}
// 当 login 是 application 的时候 调用 initalliza 方法时,上下文指向的就是 LoginApplication
@Override
public void onCreate() {
super.onCreate();
initalliza(this);
}
//当 login是 lib 的时候 被主 App 调用被赋值
@Override
public void initalliza(Application app) {
application = app;
ServiceFactory.getInstance().setLoginService(new LoginService());
}
}
123456789101112131415161718192021222324252627282930
package com.example.logincomponent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import com.example.componentlib2.ILoginService;
public class LoginService implements ILoginService {
@Override
public void launch(Context context, String targetClass) {
Intent intent = new Intent(context, LoginActivity.class);
intent.putExtra(LoginActivity.EXTRA_TARGET_CLASS, targetClass);
context.startActivity(intent);
}
@Override
public Fragment newUserInfoFragment(FragmentManager fragmentManager, int viewId, Bundle bundle) {
UserInfoFragment fragment = new UserInfoFragment();
fragment.setArguments(bundle);
fragmentManager.beginTransaction().add(viewId, fragment).commit();
return fragment;
}
}
12345678910111213141516171819202122232425262728
package com.example.logincomponent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class UserInfoFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.userinfo_fragment, null);
}
}
12345678910111213141516171819
package com.example.minecomponent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MineActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mineactivity_main);
}
}
1234567891011121314
package com.example.minecomponent;
import android.app.Application;
import com.example.componentlib2.IAppComponent;
import com.example.componentlib2.ServiceFactory;
public class MineApplication extends Application implements IAppComponent {
private static Application application;
public static Application getApplication() {
return application;
}
@Override
public void onCreate() {
super.onCreate();
initalliza(this);
}
@Override
public void initalliza(Application app) {
application = app;
ServiceFactory.getInstance().setMineService(new MineService());
}
}
12345678910111213141516171819202122232425262728
package com.example.minecomponent;
import android.content.Context;
import android.content.Intent;
import com.example.componentlib2.IMineService;
public class MineService implements IMineService {
@Override
public void launch(Context context, int userId) {
Intent intent = new Intent(context, MineActivity.class);
intent.putExtra("ID", userId);
context.startActivity(intent);
}
}
文章介绍了组件化,以及如何搭建好一个组件化。本文可能写的有点粗糙,有关组件化、热修复等进入架构师的框架搭建,有一套比较系统的Android架构核心技术学习进阶资料,需要的可以私信我发送“核心笔记”或“手册”即可前往获取全套资料!