Android 技術選型(持續更新中...)

水牛發表於2017-04-18

github主頁: https://github.com/hss01248

重中之重:

不管用什麼第三方庫,都要自己再封裝一層api 不要直接用第三方庫的api 不要直接用第三方庫的api 不要直接用第三方庫的api

log列印

一個log庫的最佳實踐為:

  • 既能列印到logcat,又能列印到檔案
  • 能列印一切物件,並且列印得便於檢視:陣列,集合,json,xml,html,java object以及android系統的特有物件
  • 不用傳入tag,能自動獲取
  • 堆疊資訊,且點選能直接跳到對應的類
  • 執行緒資訊
  • 物件繼承和屬性
  • 能支援string.format拼接字串(其實不支援也沒事,我們封裝頂層api時自己也可以寫一下)

參賽選手

  • Logger: log界元老和先驅,7600+star,不能列印集合和任意物件,有很多改進庫,比如LogDelegate
  • Timber 就一個檔案,是上層api封裝,底層僅提供列印到logcat實現,列印到檔案的話需要自己實現介面. 可以參考這個來設計自己app中的頂層api
  • logback-android logback在android上的實現,xml各種配置.沒具體用過
  • XLog 1300star,所有期望的功能都有,而且0依賴
  • LogUtils 600多star,所有期望的功能都有,依賴okio
  • KLog: 1400+star,功能不如xlog豐富

日誌列印的demo對比見: https://github.com/hss01248/TestTools

最終選擇

綜上,獲獎選手為LogDelegate,同時,timber可作為應用內部api再次封裝的一個參考.或者你就簡單包一層.

網路請求

我希望達到的目標是,對著介面文件,api鏈式呼叫著配置一下,一個請求就完成了. 懶得去記具體框架裡的類啊註解啊什麼的, 甚至,http協議的一些複雜內容我也不想涉及,比如快取控制->把常用的那些快取模式變成幾個int值給我選就行了,我才懶得用的時候考慮請求頭響應頭什麼的. 一句話,傻瓜式使用. 所以,從這個角度看,retrofit不是好的選擇.

okhttp + chuck 對okhttp的封裝: https://github.com/hss01248/HttpUtilForAndroid :我花最多精力維護的一個庫,深度結合android平臺特點,使用時爽得飛起.

https://github.com/jeasonlzy/okhttp-OkGo 這個很多人用,也是傻瓜式使用.

https://github.com/jgilfelt/chuck 通過攔截器的方式拿到請求和響應資訊,然後顯示在一個單獨的activity中算是應用內抓包

工具

  • 模擬請求: postman (需要翻牆到chrome商店內下載安裝)或者fiddler
  • 手機/電腦抓包: fiddler.
  • apk應用內抓包: chuck
  • https抓包: fiddler最新版+ fiddler證照生成外掛
  • 弱網路模擬: fiddler可模擬adsl低網速,但無法模擬tcp丟包情況
  • 網路日誌列印: LoggingInterceptor

圖片載入

包括普通圖片載入和大圖載入,以及大圖輪播,以及圖片的一些特殊效果,比如圓角,圓形,高斯模糊等等. https://github.com/hss01248/ImageLoader 此庫基於glide和fresco封裝,實現了圖片的常見特殊效果. 優化了sumsamplingImageView的大圖輪播,使輪播時記憶體佔用也一直維持在20M的水平. 配置全部鏈式api呼叫

本地圖片選擇,裁剪,壓縮

系統intent被國產rom搞得不能用,各種相容性bug,必須用框架 https://github.com/hss01248/PhotoOut 用鏈式api串起幾個優秀的開源庫,從而串起了整個流程.

https://github.com/crazycodeboy/TakePhoto/ 這個star很多,也是將整個流程串起來了.

提示性UI

toast:

Toasty : https://github.com/hss01248/Toasty 帶有狀態的toast,有succes.error,warn,info,normal 五種,以及debug一種(只在debug模式下彈出).

dialog :

DialogUtil : https://github.com/hss01248/DialogUtil 囊括所有常見的通用型dialog,既有ios風格又有android風格.不需要activity引用即可彈出. https://github.com/saiwu-bigkoo/Android-AlertView ios風格的dialog

notification

NotifyUtil: https://github.com/hss01248/NotifyUtil api鏈式呼叫,擺脫原生的難用的api.封裝了常用的通知模式.

必用的UI小元件

選擇器:

https://github.com/Bigkoo/Android-PickerView

內建時間日期選擇器,UI自定義配置很豐富 還有三級,二級,一級聯動選擇器

https://github.com/gzu-liyujiang/AndroidPicker

各種豐富的內建選擇器: 包括日期選擇器、時間選擇器、單項選擇器、城市地址選擇器、車牌號選擇器、數字選擇器、星座選擇器、生肖選擇器、顏色選擇器、檔案選擇器、目錄選擇器等

https://github.com/yangxu4536/MeiTuanLocateCity 仿美團城市選擇介面,可直接用在實際專案中

https://github.com/Bigkoo/EasySideBar 一款按字母排序庫,已封裝好城市資料,風格仿美團,可定製化強。

多條件篩選選單

https://github.com/dongjunkun/DropDownMenu 類似美團,愛奇藝電影票下拉選單

優惠券效果

https://github.com/dongjunkun/CouponView

輪播圖

https://github.com/glassLake/AndroidImageSliderByFresco 第一次進入有快速滾動兩張的bug

https://github.com/saiwu-bigkoo/Android-ConvenientBanner

https://github.com/youth5201314/banner 多種模式 Android廣告圖片輪播控制元件,支援無限迴圈和多種主題,可以靈活設定輪播樣式、動畫、輪播和切換時間、位置、圖片載入框架等!

快速分組側邊欄

https://github.com/saiwu-bigkoo/Android-QuickSideBar https://github.com/CaMnter/EasyRecyclerViewSidebar https://github.com/gjiazhe/WaveSideBar

給圖片打標籤

https://github.com/saiwu-bigkoo/Android-PictureTagView

滾動刻度尺

https://github.com/LichFaker/ScaleView

加減號控制數字增減

https://github.com/saiwu-bigkoo/Android-SnappingStepper

廣告彈窗

https://github.com/yipianfengye/android-adDialog 功能比較豐富

狀態列變色/透明狀態列/沉浸式

檔案下載

https://github.com/lingochamp/FileDownloader 專注於檔案下載,各種高階特性.適用於對檔案下載業務要求較高的專案

資料庫

greendao太難用,還是ormlite好. https://github.com/j256/ormlite-android https://github.com/Raizlabs/DBFlow 這個api設計更優雅

資料庫除錯方式:
無需root.在瀏覽器端通過http請求的方式直接檢視和運算元據庫,方便快捷 https://github.com/amitshekhariitbhu/Android-Debug-Database

頁面狀態管理

https://github.com/hss01248/PageStateManager 我自己用的,基於鴻洋的庫改進 https://github.com/arieridwan8/pageloader https://github.com/weavey/LoadingLayoutDemo

工具類庫

這個也沒有疑問,就是這個高大全的庫了: https://github.com/Blankj/AndroidUtilCode

Rx全家桶

為什麼沒有retrofit? 因為它的api太難用了啊.設計很優美,但是使用起來坑太多,又繁瑣.

RxAndroid

讓你隨意地切換執行緒

RxBinding

從此擺脫頻繁點選導致的重複操作的煩惱

RxLifecycle

生命週期管理,減少記憶體洩漏

RxPermissions

6.0執行時許可權

Rx Preferences

通過 RxJava 的方式來訪問 SharedPreferences

onactivityResult

https://github.com/VictorAlbertos/RxActivityResult 將開啟activity和接收result的程式碼放在一起,內聚感很強,而且可以轉化為observable流.

鍵盤監聽

鍵盤監聽是android一大坑. 防止自動彈出鍵盤: activity 標籤里加android:windowSoftInputMode="stateHidden|stateAlwaysHidden"

https://github.com/yshrsmz/KeyboardVisibilityEvent

網路變更事件監聽

https://github.com/pwittchen/NetworkEvents

螢幕適配

https://github.com/hongyangAndroid/AndroidAutoLayout

UI特效

陰影效果

https://github.com/wangjiegulu/ShadowViewHelper 1.0.4效果不穩定,建議用1.0.2.

第三方庫相容性總還是存在一定問題,最靠譜的還是用drawable: 看這個: shadow.xml

水波紋效果

https://github.com/traex/RippleEffect 點選後產生水波紋,相容到api9 補充: 發現在華為平板上會崩...

動畫

https://github.com/florent37/ViewAnimator A fluent Android animation library api封裝簡化使用

https://github.com/airbnb/lottie-android parses Adobe After Effects animations exported as json with Bodymovin and renders them natively on mobile

https://github.com/lgvalle/Material-Animations

loading動畫

https://github.com/race604/WaveLoading : provides a wave loading animation as a Drawable. 也就是說,可以設定給任意一個view當背景

換皮膚主題

https://github.com/hongyangAndroid/ChangeSkin 一鍵換膚

https://github.com/garretyoder/Colorful 任何時候都能程式碼動態修改 主題色,擺脫了xml的限制

把textview 玩出花來

https://github.com/thoughtbot/stencil 漸漸出現的動畫

檔案和快取相關

https://github.com/vincentbrison/dualcache This android library provide a cache with 2 layers, one in RAM in top of one disk.

https://github.com/baoyongzhang/Treasure Very easy to use wrapper library for Android SharePreferences

content-provider

https://github.com/EverythingMe/easy-content-providers

原生元件的擴充套件

edittext

https://github.com/rengwuxian/MaterialEditText https://github.com/bufferapp/BufferTextInputLayout 對design包裡的TextInputLayout的一個擴充套件

原生EditText在不同android版本上效果不一樣,絕對不能用.

  • 可以用appcompat包裡的AppCompatEditText,
  • 或者用design包裡的TextInputEditText.
  • 如果UI是自己搞,那麼直接用TextInputLayout+TextInputEditText,各種提示UI,顯示隱藏密碼,基本的校驗都整合了. 使用很簡單,看這裡:TextInput詳解 · Material Design Part 1

button

寫那麼多的selector煩死人了,用這個吧: https://github.com/niniloveyou/StateButton

viewpager

https://github.com/Devlight/InfiniteCycleViewPager

禁止左右滑動

@Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isCanScroll && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isCanScroll && super.onInterceptTouchEvent(event);
    }
複製程式碼

當各page型別一致時,內部迴圈利用四個page

https://github.com/AlexMofer/RecyclePagerAdapter

shape 和selector

寫xml太麻煩,直接用上java程式碼:通過程式碼定義shape/selector

listview和recycleview

https://github.com/CymChad/BaseRecyclerViewAdapterHelper 對多型別的支援一般般? https://github.com/EvilBT/SherlockAdapter

https://github.com/WuXiaolong/PullLoadMoreRecyclerView 少定製,快速上手 https://github.com/hongyangAndroid/baseAdapter 鴻洋大神出品 關於Android RecyclerView的那些開源LayoutManager

動態的linearlayout或者scrollview裡巢狀listview

下拉重新整理

看這個彙總就好了: https://github.com/android-cjj/BeautifulRefreshLayout

第三方分享和登入,統計

使用友盟的sdk,然後再封裝一層 參見第三方登入/分享最佳實踐 自己封裝的一個: https://github.com/hss01248/UmengUtil 對umeng分享,第三方登入以及統計的api的封裝,避免再出現api大幅改動而到處改原始碼.真正的一行程式碼完成分享和登入 注:上面最佳實踐的一些好的思路還沒有整合到這個庫中,目前只整合了qq,weixin,sina三家.

第三方推送

整合第三方推送最佳實踐 一句話來,小米rom就用小米推送,其他用友盟推送,更細緻一點,還可以華為rom用華為推送 開源庫: OnePush

許可權申請

6.0以下的懸浮窗許可權申請,以及判斷: https://github.com/hss01248/FloatWindowPermission 6.0以上,執行時許可權,還包括懸浮窗許可權,以及讀寫sd卡許可權(非執行時,但華為手機和平板要求): RxPermissions: https://github.com/tbruyelle/RxPermissions

二維碼掃描

基本上都是基於z-xing封裝

BGAQRCode-Android 我用的是這個 barcodescanner 這個也很多人推薦

音訊錄製和播放

音訊錄製

當然是錄製成跨平臺的mp3格式:

https://github.com/hss01248/AndroidMP3Recorder

基於start最多的AndroidMP3Recorder專案改進,經受了一個實際線上專案兩年的考驗 特點: 邊錄邊轉碼,暫停可播,方法安全,回撥豐富,6.0以下許可權也適配了

音訊播放

mediaplayer 難用得你想死,有木有?路徑設定很多rom互相不承認.操作方法不安全,各種異常.都給你封裝好了:

https://github.com/hss01248/SafeMediaPlayer

視訊相關

短視訊錄製和播放:

第三方的趣拍已經收費了,一年5萬,略貴.下面這個是開源中比較好的,不過沒有特效.

https://github.com/mabeijianxi/small-video-record

有特效的要收費,比如這個,錄製過程中有濾鏡,錄製後貼圖,加音樂.

https://github.com/rdsdk/rdVideoEditSDK-for-Android

視訊播放

  • Vitamio 支援格式較多,穩定性高,效能較高,功能強大,開發難度低。個人使用免費,商業使用收費.但是,已經停止更新了. https://github.com/yixia/VitamioBundle
  • ijkplayer 目前國內最火的,bilibili開源的. https://github.com/Bilibili/ijkplayer 基於ijk的豐富的功能封裝庫: https://github.com/CarGuo/GSYVideoPlayer 基於ijk的介面封裝:(不如上一個豐富) https://github.com/jjdxmashl/jjdxm_ijkplayer

藍芽

FastBle https://github.com/Jasonchenlijian/FastBle

相機拍照和拍視訊的相容

Android相機相容性問題非常大,如果只是拍照,儘量呼叫系統相機,如果需要預覽和拿視訊流,那麼可以基於這個開源庫開發:

  • https://github.com/google/cameraview 久不更新
  • https://github.com/wonderkiln/CameraKit-Android 反應遲鈍,相容性不佳
  • https://github.com/Fotoapparat/Fotoapparat 一開啟就crash,一身的bug
  • https://github.com/natario1/CameraView 相容性不錯,反應靈敏
  • https://github.com/florent37/CameraFragment 好好的view不用,用什麼fragment,差評
  • https://github.com/hujiaweibujidao/cameraview 基於google/cameraview修改,對應blog

最終建議使用: https://github.com/natario1/CameraView

android上建輕量級http伺服器

本質上是提供http通訊渠道 下面的庫,是用java原生的serversocket來接收資訊,然後用org.apache.http包來解析http相關資訊,然後自己像寫severlet一樣處理.

https://github.com/yanzhenjie/AndServer

程式保活

對比了github的幾個star數較多的庫,最終選擇HelloDaemon,這個庫是在其他兩個庫基礎上的改進.

相關理論文章: Android程式保活的研究與實踐 Android 程式常駐(2)----細數利用android系統機制的保活手段

程式碼優化

findbugs 外掛+ PMD外掛+ 配置CheckStyles(規範程式碼格式) + alibaba 程式碼檢查外掛 參見 https://juejin.im/post/58d4e35261ff4b00605326d9

渠道打包

  • 360加固寶 : 神速,還會加上360自己的一些統計功能
  • packer-ng-plugin : 號稱下一代Android打包工具,100個渠道包只需要10秒鐘.但沒有具體用過

crash日誌統計

想避免崩潰 用 https://github.com/jenly1314/NeverCrash ,但程式碼後續的一些邏輯要考慮清楚 日誌統計: 騰訊的bugly,或者友盟統計.360加固時也可選日誌統計功能,實時傳送而不是下一次傳送,比友盟的要全一些

apk更新和熱修復

自己搞

可以自己搭建伺服器,寫後臺管理apk版本,並提供檢測新版本和下載新版本的api, app端核對版本號,下載apk,校驗md5,安裝. android端基本邏輯封裝: https://github.com/hss01248/ApkUpdater

熱更新: tinker 配置稍顯繁瑣

利用bugly的更新和熱修復功能

註冊賬號,整合sdk,按文件配置好就ok,比較簡單.推薦使用. 其熱修復是通過tinker sdk +管理後臺 文件: https://bugly.qq.com/docs/user-guide/instruction-manual-android-hotfix/

雲端儲存

  • 七牛雲: 雲端圖片處理功能真是牛逼上了天.之前還根據他們的api封裝了個工具類:QiniuUtils
  • 阿里雲: 看api文件,該有的雲端處理功能也有,但沒有實際用過.有用過的嗎?說說看

除錯工具篇

程式碼侵入型:

stetho web上直接除錯網路和資料庫 需要在Application的oncreate處init,網路的話需要給okhttp加一個攔截器.

網路抓包

https://github.com/jgilfelt/chuck 通過新增okhttp攔截器的方式拿到請求和響應資訊,然後顯示在一個本手機上單獨的activity中,不需要與電腦聯網,json顯示已格式化,很清晰

資料庫除錯

Android-Debug-Database 類似stetho的資料庫檢視功能,也是web端檢視,需要與電腦聯網.

非侵入型

網路抓包

  • 手機端抓包工具-packet capture 在手機上自建vpn,並匯入證照.可以抓所有手機http和https流量.但顯示介面上json沒有格式化顯示.

  • 電腦端抓包工具-fiddler 需要手機和電腦在同一個區域網(比較難達成),手機上設定代理,由電腦代理上網. 注意,https抓包需要安裝一個證照製作外掛,內建製作工具有bug,製作的證照不被系統承認.

檢視頂層activity

https://github.com/hss01248/DemoCollections/tree/master/ViewDebugHelper 手機全域性檢視頂層activity.

一些牛逼的小技巧

awesome-android-tips Android 系統中,那些能大幅提高工作效率的 API 彙總 Android開發中,那些讓你相見恨晚的方法、類或介面 android-tips-tricks-cn corelink

提升開發效率工具彙總

Android攻城獅—全套必備神級工具(開發,外掛,效率)

以下不算技術選型,僅做備忘

幾個全套型快速開發框架 -學習coding skill 用

https://github.com/meikoz/Basic https://github.com/minggo620/Pluto-Android

別人總結的開源專案大禮包

https://github.com/Tim9Liu9/TimLiu-Android https://github.com/Lafree317/ShareAndroidResource https://github.com/limedroid/XDroid/wiki https://github.com/aritraroy/UltimateAndroidReference

save instance自動化

android-state 600+ star SaveState 100 + star

系統intent去開啟一些資源

android-intents

相關文章