組件化定義
組件化是建立在模塊化思想上的一次演進,一個變種。組件化本來就是模塊化的概念。核心是模塊角色的可轉化換性,在打包時,是library;調試時,是Application。
組件化的單位是組件,這里跟模塊化的一個最大的區別就是組件之間是可以相互進行跳轉的,其實現就是通過路由,這個在之后會手動來實現這個路由的功能。它是一個編譯時的行為。
組件化優點:
- 可重用
- 代碼簡潔
- 易測試
為什么使用組件化
一直使用單工程操作,項目越來越大導致出現了不少的問題:
- 查找問題慢:定位問題,需要在多個代碼混合的模塊中尋找和跳轉。
- 開發維護成本增加:避免代碼的改動影響其它業務的功能,導致開發和維護成本不斷增加。
- 編譯時間長:項目工程越大,編譯完整代碼所花費的時間越長。
- 開發效率低:多人協作開發時,開發風格不一,又很難將業務完全分割,大家互相影響,導致開發效率低下。
- 代碼復用性差:寫過的代碼很難抽離出來再次利用。
組件化項目整體結構
COMPONENTLIB2 LIBRARY項目結構
BUILD.GRADLE
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
APPCONFIG
package com.example.componentlib2;
public class AppConfig {
public static final String[] COMPONENTS = {
"com.example.logincomponent.LoginApplication",
"com.example.minecomponent.MineApplication"
};
}
123456789
IAPPCOMPONENT
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
IMINESERVICE
package com.example.componentlib2;
import android.content.Context;
public interface IMineService {
void launch(Context context,int userId);
}
12345678
SERVICEFACTORY
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
LOGINCOMPONENT 組件
LOGINACTIVITY
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
LOGINAPPLICATION
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
LOGINSERVICE
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
USERINFOFRAGMENT
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
MINECOMPONENT 組件
MINEACTIVITY
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
MINEAPPLICATION
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
MINESERVICE
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架構核心技術學習進階資料,需要的可以私信我發送“核心筆記”或“手冊”即可前往獲取全套資料!