android lint工作机制,Android架构
MVC
mvc model view controller 模式視圖控制器
M: 業務邏輯處理
V:處理數據顯示的部分
C:Activity處理用戶交互的問題,中間橋梁的作用,解耦的作用。
特點:
耦合性低
擴展性好,利于維護
模塊職責劃分明確
Model層:
//狀態的回調
public interface LoginResultListener {
void result(boolean status);
}
//接口
public interface UserModel {
public void login(String name, String password, LoginResultListener listener);
}
//實現類,進行具體的業務操作
public class UserModelImp implements UserModel {
@Override
public void login(String name, String password, LoginResultListener listener) {
//todo進行相關的操作
}
}
view層:
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="rao.com.mvc.MvcDemoActivity">
android:id="@+id/til_username"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:hint="用戶名"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/til_password"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/til_username">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼"/>
android:id="@+id/bt_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/til_password"/>
controller:
public class MvcDemoActivity extends AppCompatActivity {
private TextInputLayout mTilUserName;
private TextInputLayout mTilPassword;
private Button mBtLogin;
private UserModelImp mUserModelImp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvc_demo);
mTilUserName = findViewById(R.id.til_username);
mTilPassword = findViewById(R.id.til_password);
mBtLogin = findViewById(R.id.bt_login);
mUserModelImp = new UserModelImp();
mBtLogin.setOnClickListener(view -> {
mUserModelImp.login(mTilUserName.getEditText().getText().toString(), mTilPassword.getEditText().getText().toString(), new LoginResultListener() {
@Override
public void result(boolean status) {
}
});
});
}
}
在實際的使用過程中xml布局起的作用是十分的有限的,同時Activty需要操作ui和業務邏輯,造成冗余。
MVP
M:依然是業務邏輯和實體類型模型
V:對應于Activity,負責View的繪制以及與用戶交互
P:負責完成View于Model間的交互。
mvp和mvc.png
定義model層:
//接口回調
public interface OnLoginListener {
void onLoginResult(int status, User user);
}
//定義業務
public interface IUserBiz {
void login(String username, String password, OnLoginListener listener);
}
//具體的實現類
public class UserBiz implements IUserBiz {
@Override
public void login(String username, String password, OnLoginListener listener) {
// TODO: 2018/3/18 ,進行登錄,網絡數據等操作
boolean status = true;
if (status) {
listener.onLoginResult(1, new User(username, password));
} else {
listener.onLoginResult(0, null);
}
}
}
view層:
//view相關
public interface IUserLoginView {
String getUsername();
String getPassword();
void showLoading();
void dismissLoading();
void loginStatus(int status);
}
//activity
public class MVPActivity extends AppCompatActivity implements IUserLoginView {
private TextInputLayout mTilUserName;
private TextInputLayout mTilPassword;
private Button mBtLogin;
private ProgressDialog mProgressDialog;
private UserLoginPresenter mUserLoginPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvp);
mTilUserName = findViewById(R.id.til_username);
mTilPassword = findViewById(R.id.til_password);
mBtLogin = findViewById(R.id.bt_login);
mProgressDialog = new ProgressDialog(this);
mUserLoginPresenter = new UserLoginPresenter(this);
mBtLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUserLoginPresenter.login();
}
});
}
@Override
public String getUsername() {
return mTilUserName.getEditText().getText().toString();
}
@Override
public String getPassword() {
return mTilPassword.getEditText().getText().toString();
}
@Override
public void showLoading() {
mProgressDialog.show();
}
@Override
public void dismissLoading() {
mProgressDialog.dismiss();
}
@Override
public void loginStatus(int status) {
//todo,根據操作,去進行響應的跳轉
}
}
presenter層:
public class UserLoginPresenter {
private IUserBiz mIUserBiz;
private IUserLoginView mIUserLoginView;
public UserLoginPresenter(IUserLoginView IUserLoginView) {
mIUserLoginView = IUserLoginView;
mIUserBiz = new UserBiz();
}
public void login() {
mIUserLoginView.showLoading();
mIUserBiz.login(mIUserLoginView.getUsername(), mIUserLoginView.getPassword(), new OnLoginListener() {
@Override
public void onLoginResult(int status, User user) {
if (status == 1) {
mIUserLoginView.loginStatus(1);
} else {
mIUserLoginView.loginStatus(0);
}
}
});
}
}
MVVM
View: 對應于Activity和xml,負責View的繪制,以及用戶交互。
Model:實體模型,數據存取
ViewModel:負責完成View與Model間的交互,負責業務邏輯。
一把配套使用DataBinding使用,Model與ViewModel雙向通信,一般使用數據驅動的方式來實現。view只負責UI操作。
mvvm.png
Android插件化
當業務量大的時候,android的65536方法數的限制。
apk大的時候,動態加載apk:一個宿主的apk,到sd卡中動態的加載apk,
資源加載:通過AssertManagerr類
代碼加載 :java中類加載機制
動態加載APk:類加載器,就是將java的字節碼加載到虛擬機當中android中有兩個重要的
DexClassLoader: 可以從dex文件加載字節碼文件,用于動態加載和熱更新等。
PathClassLoader:只能加載文件目錄下的apk
資源加載:AssertManager反射來加載的
代碼加載:反射綁定到activity的生命期,之后在加載
Android熱更新
線上檢測到嚴重的 crash
拉出bugfix分支并在分支上修復問題
jenkins構建和補丁生成
app推送或主動拉取文件
將bugfix代碼合并到master上
熱更新框架:
Dexposed :阿里,基于aop的思想,無需重啟,即可修復,通過hook技術,不僅可以修改java方法,還能hook修改android方法,日志記錄,性能統計,安全控制,事物處理。(hook基本的方法就是通過hook“接觸”到需要修改的api函數入口點,改變它的地址指向新的自定義的函數)
AndFix:阿里,更純粹的熱修護技術,性能較好,工具較完善。
Nuwa:基于dex分包的技術,將dex文件拆分多個dex,編上號,依次加載。
原理:
Android類加載機制
PathClassLoader 加載系統的類,應用類
DexClassLoader 加載Dex文件,apk包
熱修復機制
dexElements 會創建dex數組
ClassLoader 會遍歷這個數據,根據線上的carch定位到這個dex,將修復好的dex的位置靠前,優先加載這個dex文件。
進程保活
使用場景:定位,推送等等
進程優先級:
前臺進程
可見進程(沒有前臺組件,但是會影響前臺界面的進程)
服務進程
后臺進程
空進程(緩存)
回收策略:
low memory killer:通過一系列的評分機制,定義進程進行打分,將分數搞的進程判定為bad進程,殺死并釋放緩存。(定時檢查)
OOM_ODJ:這個閥值,判斷進程的優先級,越大進程優先級越低。
保活方案:
系統廣播拉活,開機,網絡數據變化,不可控制(容易被系統軟件禁用)
系統Service機制拉活 :利用的是當系統內存不足而殺掉該Service,可以拉活。殺死Service后,第一5秒拉活,二次10秒后拉活,三次20秒拉活,當Serice被系統拉活三次之后,就不會在被系統進行拉活了。當被安全軟件或系統軟件靜止之后,是不能拉活的。
利用native進程拉活:利用linux的fork機制創建一個進程,監控主進程的存活,這是可以立即對主進程進行拉活。(失效了)
JobScheduler機制拉活,跟native類是,android5.0之后提供的。
帳號同步機制拉活 (不再生效)
UIL
Universe Image Loader圖片加載框架
Lint檢查
Android Lint是一個靜態的代碼分析工具,它能夠對android項目潛在的bug,可優化的代碼,安全性,性能,可用性,可訪問性國際化等進行檢查。
Lint工作流程:通過配置lint.xml配置,運用lintTool工具,進行相關的額檢查
在Src文件目錄下,創建lint.xml文件
Lint.png
如果確定java代碼寫的沒有問題,你可以以下的方式使用
//在java代碼中的使用,忽略這警告,檢查,這是在lint.xml中配置了屬性的
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView(savedInstanceState);
}
//在xml文件中的使用,當代碼執行到這里的時候就會忽略未使用的資源的問題,不會對編譯盡心檢查
tools:ingnore="UnusedResources"
自定義lint
使用默認的lint檢查不能滿足需求,在自定義庫的使用,某些屬性沒有配置 BuiltinlssueRegistry
Kotlin
Kotlin一種基于JVM的編程語言
是對java的一種擴展
Kotlin支持函數式編程
Kotlin類與Java類互相調用
定位carch定位的堆棧的問題
安裝Kotlin 插件 在android studio安裝
總結
以上是生活随笔為你收集整理的android lint工作机制,Android架构的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【1】Python 视频文字识别提取 -
- 下一篇: php 添加样式,添加样式到php ht
