天天看點

Android最佳實踐

導言:因為新工作使用内網代理的關系,涉及到資訊安全,外網基本被屏蔽掉了,是以部門内部還是有相當一部分人在使用eclipse,不過我還是打算申請外網,尼瑪還要錢,兼用android studio 和eclipse 畢竟前者基本上淪為主流開發工具,github上很多優秀的開源項目都遷到studio上了,本來是打算在eclipse中搭建gradle環境,網上也是存在相應的解決方案,不過我還是覺得eclipse+ant, studio+gradle已是行業标準,瞎配亂搭,不倫不類,辣眼睛不習慣。

1.看下兩者的差別與建議:

If your project uses the old Ant & Eclipse ADT project structure, consider it legacy and start porting it to the new Gradle & Android Studio project structure. 建議把eclipse上的項目移植到Studio。

Android最佳實踐
Android最佳實踐

主要的差別在于Studio結構把Src源碼集一分為二,Main源碼目錄和AndroidTest測試代碼目錄,add source sets ‘paid’ and ‘free’ into src which will have source code for the paid and free flavours of your app.同一套代碼友善地建構不同類型的版本。Having a top-level app is useful to distinguish your app from other library projects (e.g., library-foobar) that will be referenced in your app. The settings.gradle then keeps references to these library projects, which app/build.gradle can reference to。 Gradle相比ant地優勢除了上面提到的幾點,還包括依賴地自動下載下傳,定制化簽名等。

2.Gradle遵循如下幾點規則:

  • 2.1. Put passwords and sensitive data in gradle.properties 在正式簽名的時候,盡量避免在signingConfigs中顯示的添加,應該添加到屬性檔案gradle.properties中。防止密碼洩露,出現在版本控制系統中,而Instead, make a gradle.properties file which should not be added to the version control system:
    Android最佳實踐
  • 2.2. Prefer Maven dependency resolution instead of importing jar files 使用maven依賴庫,If you explicitly include jar files in your project, they will be of some specific frozen version, such as 2.1.1. Downloading jars and handling updates is cumbersome, 你要不嫌煩,也可以。
  • 2.3. Avoid Maven dynamic dependency resolution 在添加第三方庫時,避免使用such as 2.1.+ 這種動态版本形式,相比較于the use of static versions such as 2.1.1 helps create a more stable, predictable and repeatable development environment. 版本固定,穩定性。
  • 2.4. Use different package name for non-release builds 對于測試版本建議使用不同的包名,Use applicationIdSuffix for debug build type to be able to install both debug and release apk on the same device (do this also for custom build types, if you need any). 在編譯腳本中通過對applicationIdSuffix字段的聲明可以使同一台機器同時安裝多個不同版本的同一應用,This will be especially valuable later on in the app’s lifecycle, after it has been published to the store. 如果想進一步在圖示和名字上進行差別,使用如下添加方法:simply put debug icon in app/src/debug/res and release icon in app/src/release/res. You could also change app name per build type, as well as versionName。
    Android最佳實踐