Android開發實踐小結

DaveGandy 8年前發布 | 6K 次閱讀 安卓開發 Android開發 移動開發

作為一名搬運工,應該懂得避免重復創建輪子。

配置keystore密碼信息

通常在 app/build.gradle 中我們會使用以下方式配置:

signingConfigs { 
  release {  
    storeFile file("myapp.keystore")  
    storePassword "mystorepassword"    
    keyAlias "mykeyAlias"        
    keyPassword "mykeypassword"   
  }
} 

但這種方法不是特別好,因為如果你把代碼分享到 github ,你的密碼就泄露了。

推薦的做法應該是在Androd項目中 gradle.properties (如果沒有則手動創建一個)文件中創建以下變量,這個文件是不會被版本控制系統提交的,所以不用擔心密碼泄露。

KEYSTORE_PASSWORD=mystorepassword
KEY_PASSWORD=mykeypassword

那么在 app/build.gradle 中的配置就應該是

signingConfigs {
    release {
        try {
            storeFile file("myapp.keystore")
            storePassword KEYSTORE_PASSWORD
            keyAlias "mykeyAlias"
            keyPassword KEY_PASSWORD
        }
        catch (ex) {
            throw new InvalidUserDataException("You should define KEYSTORE_PASSWORD and KEY_PASSWORD in gradle.properties.")
        }
    }
} 

這里在打包的時候,如果沒有在 gradle.properties 配置相關變量,那么就會在messages窗口中提示

Error:(16, 0) You should define KEYSTORE_PASSWORD and KEY_PASSWORD in gradle.properties.

包引用盡可能使用dependency而不是直接導入jar包

dependencies {
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
}

以下方式也是不推薦使用的

dependencies {
    compile 'com.squareup.retrofit2:retrofit:2.1.0+'
}

這種方式每次編譯時都會去聯網檢測最新的包,影響性能,還可能會下載跟你不符合需求的jar包。

為release版本和debug版本指定不同的包名

同一個應用而不同的包名可以同時安裝在同一個手機里面。

可以參考以下配置

android {
    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-DEBUG'
        }
?
        release {
            // ...
        }
    }
}

分別為包名和版本號加上 debugDEBUG 后綴。

如果想要在版本號添加時間信息,有利于區分,可以這樣處理:

1、首先在 app/build.gralde 中定義一個 buildTime() 函數

//定義build 時間
def buildTime() {
    Date date = new Date()
    String build = date.format("yyMMddHHmm", TimeZone.getDefault())
    return build
} 

2、然后再修改 versionNameSuffix 參數 

buildTypes {
        debug {
            //配置這包名后綴下可以同時安裝release包和debug包
            applicationIdSuffix ".debug"
            //配置versionName后綴
            versionNameSuffix '_' + (buildTime()) + '-DEBUG'
            ...
        }
        release {
            ...
        }
    }

在debug包中定義了版本號

開發調試工具

Stetho

Stetho是非死book開源的Android調試工具,可以使用Chrome開發工具來對Android應用進行調試、抓包、查看Sqlite數據庫等功能。可以在 debug 版本中集成 Stetho ,方便開發調試。

集成Stetho也是非常簡單,只需要在 app/build.gradle 中配置

dependencies { 
    compile 'com.非死book.stetho:stetho:1.4.1' 
  } 

然后在 Application 里面初始化Stetho

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initializeWithDefaults(this);
  }
}

這樣就配置好了,AS連接手機跑起來后。打開Chrome,在地址欄輸入

chrome://inspect/#devices 

這時候就看到手機調試的信息

查看設備

查看sqlite數據庫

View Hierarchy

調試網絡

目前Steho支持okhttp網絡庫,同樣的在gradle里面配置

dependencies { 
    compile 'com.非死book.stetho:stetho:1.4.1' 
    compile 'com.非死book.stetho:stetho-okhttp3:1.4.1' 
    compile 'com.非死book.stetho:stetho-urlconnection:1.4.1'
} 

對OkHttp2.x版本

OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());

對OkHttp3.x版本

new OkHttpClient.Builder()
    .addNetworkInterceptor(new StethoInterceptor())
    .build(); 

對于其他網絡需要修改Stetho現在還沒有支持,如果是 HttpURLConnection ,可以使用 StethoURLConnectionManager 來集成,詳情可以參考 Steho 官網。

網絡抓包

LeakCanary

LeakCanary可以在應用運行時檢測應用是否有OOM風險的一個工具庫。同樣的可只在 debug 版本中集成。

在app/build.gradle中

dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
   testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
 }

在Application中

public class ExampleApplication extends Application {
?
  @Override public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {
      // This process is dedicated to LeakCanary for heap analysis.
      // You should not init your app in this process.
      return;
    }
    LeakCanary.install(this);
    // Normal app init code...
  }
} 

這樣就配置好了。連接手機AS跑完后就會在手機桌面上生成一個 LeakCanary 圖標,點擊進入可以方便查看變量在內存中的引用鏈。如果 LeakCanary 檢測到有內存泄露,也會發送一個通知欄消息來提醒。

AS常用插件

很多App都會使用UI注解框架來初始化UI控件其中最有名的估計就是 ButterKnife 了。

class ExampleActivity extends Activity {
  @BindView(R.id.user) EditText username;
  @BindView(R.id.pass) EditText password;
?
  @BindString(R.string.login_error) String loginErrorMessage;
?
  @OnClick(R.id.submit) void submit() {
    // TODO call server...
  }
?
  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...
  }
}

但是經常寫 @BindView 也是一件令人枯燥和煩惱的事情。

Android Butterknife Zelezny

這個插件可以極大的解放程序猿的雙手,提高搬磚效率。

安裝好插件后,把光標定位到 layout 文件的引用處,例如 setContentView(R.layout.simple_activity); 的 R.layout.simple_activity 末尾處,按下快捷鍵command+N(windows是Altt+Insert)將彈出以下對話框。

 

 

 

來自:http://www.cnblogs.com/angrycode/p/6044351.html

 

 本文由用戶 DaveGandy 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!