http://stormzhang.com/devtools/2014/12/18/android-studio-tutorial4/
https://github.com/stormzhang/9GAG
Android Studio
主題
字體
**快捷鍵**
Action Mac OSX Win/Linux
註釋代碼(//) Cmd + / Ctrl + /
註釋代碼(/**/) Cmd + Option + / Ctrl + Alt + /
格式化代碼 Cmd + Option + L Ctrl + Alt + L
清除無效包引用 Option + Control + O Alt + Ctrl + O
查找 Cmd + F Ctrl + F
查找+替換 Cmd + R Ctrl + R
上下移動代碼 Option + Shift + Up/Down Alt + Shift + Up/Down
刪除行 Cmd + Delete Ctrl + Y
擴大縮小選中範圍 Option + Up/Down Ctrl + W/Ctrl + Shift + W
快捷生成結構體 Cmd + Option + T Ctrl + Alt + T
快捷覆寫方法 Ctrl + O Ctrl + O
快捷定位到行首/尾 Cmd + Left/Right Ctrl + Left/Right
摺疊展開代碼塊 Cmd + Plus,Minus Ctrl + Plus/Minus
摺疊展開全部代碼塊Cmd + Shift + Plus,Minus Ctrl + Shift + Plus,Minus
文件方法結構 Cmd + F12 Ctrl + F12
查找調用的位置 Ctrl + Option + H Ctrl + Alt + H
大小寫轉換 Cmd + Shift + U Ctrl + Shift + U
自動導包
GRADLE
9GAG/app/build.gradle
// 聲明是Android程序
apply plugin: 'com.android.application'
android {
// 編譯SDK的版本
compileSdkVersion 21
// build tools的版本
buildToolsVersion "21.1.1"
defaultConfig {
// 應用的包名
applicationId "me.storm.ninegag"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0.0"
}
// java版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
debug {
// debug模式
}
release {
// 是否進行混淆
minifyEnabled false
// 混淆文件的位置
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
// 移除lint檢查的error
lintOptions {
abortOnError false
}
}
dependencies {
// 編譯libs目錄下的所有jar包
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.2'
compile 'com.etsy.android.grid:library:1.0.5'
compile 'com.alexvasilkov:foldable-layout:1.0.1'
// 編譯extras目錄下的ShimmerAndroid模塊
compile project(':extras:ShimmerAndroid')
}
- 文件開頭apply plugin是最新gradle版本的寫法,以前的寫法是apply plugin: ‘android’, 如果還是以前的寫法,請改正過來。
- buildToolsVersion這個需要你本地安裝該版本才行,很多人導入新的第三方庫,失敗的原因之一是build version的版本不對,這個可以手動更改成你本地已有的版本或者打開 SDK Manager 去下載對應版本。
- applicationId代表應用的包名,也是最新的寫法,這裏就不在多說了。
- android 5.0開始默認安裝jdk1.7才能編譯,但是由於mac系統自帶jdk的版本是1.6,所以需要手動下載jdk1.7並配置下,具體可以見我這篇博客Mac下安裝和管理Java
- minifyEnabled也是最新的語法,很早之前是runProguard,這個也需要更新下。
- proguardFiles這部分有兩段,前一部分代表系統默認的android程序的混淆文件,該文件已經包含了基本的混淆聲明,免去了我們很多事,這個文件的目錄在 /tools/proguard/proguard-android.txt , 後一部分是我們項目裏的自定義的混淆文件,目錄就在 app/proguard-rules.txt , 如果你用Studio 1.0創建的新項目默認生成的文件名是 proguard-rules.pro , 這個名字沒關係,在這個文件裏你可以聲明一些第三方依賴的一些混淆規則,由於是開源項目,9GAG裏並未進行混淆,具體混淆的語法也不是本篇博客討論的範圍。最終混淆的結果是這兩部分文件共同作用的。
- compile project(‘:extras:ShimmerAndroid’)這一行是因爲9GAG中存在其他Module,不知道Module的概念可以看下這篇博客Android Studio系列教程二–基本設置與運行, 總之你可以理解成Android Library,由於Gradle的普及以及遠程倉庫的完善,這種依賴漸漸的會變得非常不常見,但是你需要知道有這種依賴的。
- 以上文件裏的內容只是基本配置,其實還有很多自定義部分,如自動打包debug,release,beta等環境,簽名,多渠道打包等,後續會單獨拿出來講解。
9GAG/gradle
#Thu Dec 18 16:02:24 CST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-2.2.1-all.zip
可以看到裏面聲明瞭gradle的目錄與下載路徑以及當前項目使用的gradle版本,這些默認的路徑我們一般不會更改的,這個文件裏指明的gradle版本不對也是很多導包不成功的原因之一。
9GAG/extras/ShimmerAndroid/build.gradle
每一個Module都需要有一個gradle配置文件,語法都是一樣,唯一不同的是開頭聲明的是 apply plugin: ‘com.android.library’
9GAG/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
內容主要包含了兩個方面:一個是聲明倉庫的源,這裏可以看到是指明的jcenter(), 之前版本則是mavenCentral(), jcenter可以理解成是一個新的中央遠程倉庫,兼容maven中心倉庫,而且性能更優。另一個是聲明瞭android gradle plugin的版本,android studio 1.0正式版必須要求支持gradle plugin 1.0的版本。
9GAG/settings.gradle
這個文件是全局的項目配置文件,裏面主要聲明一些需要加入gradle的module,我們來看看9GAG該文件的內容:
include ':app', ':extras:ShimmerAndroid'
文件中的 app, extras:ShimmerAndroid 都是module,如果還有其他module都需要按照如上格式加進去。
友盟多渠道打包
在AndroidManifest.xml裏面會有這麼一段:
<meta-data
android:name="UMENG_CHANNEL"
android:value="Channel_ID" />
裏面的Channel_ID就是渠道標示。我們的目標就是在編譯的時候這個值能夠自動變化。
第一步 在AndroidManifest.xml裏配置PlaceHolder
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
第二步 在build.gradle設置productFlavors
android {
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
_360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}
}
或者批量修改
android {
productFlavors {
xiaomi {}
_360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
很簡單清晰有沒有?直接執行 gradle assembleRelease , 然後就可以靜靜的喝杯咖啡等待打包完成吧。
assemble: 允許直接構建一個Variant版本,例如assembleFlavor1Debug。
assemble: 允許構建指定Build Type的所有APK,例如assembleDebug將會構建Flavor1Debug和Flavor2Debug兩個Variant版本。
assemble: 允許構建指定flavor的所有APK,例如assembleFlavor1將會構建Flavor1Debug和Flavor1Release兩個Variant版本。
項目中使用的完整的gradle文件配置
apply plugin: 'com.android.application'
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
applicationId "com.boohee.*"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
// dex突破65535的限制
multiDexEnabled true
// 默認是umeng的渠道
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
lintOptions {
abortOnError false
}
signingConfigs {
debug {
// No debug config
}
release {
storeFile file("../yourapp.keystore")
storePassword "your password"
keyAlias "your alias"
keyPassword "your password"
}
}
buildTypes {
debug {
// 顯示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不顯示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled true
zipAlignEnabled true
// 移除無用的resource文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
// 友盟多渠道打包
productFlavors {
wandoujia {}
_360 {}
baidu {}
xiaomi {}
tencent {}
taobao {}
...
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.jakewharton:butterknife:6.0.0'
...
}
開發筆記
ctrl+alt f 全局變量
ctrl+j live + templates 模板
SystemClcok.sleep()==Thread.sleep();
@ViewInject(R.id.xxx) View.inject(context);=findeviewbyid(R.id.xxx);//註解
模擬器快捷鍵:
Esc 後退 F2 菜單
文本加密 text encrypt
templates 模板 Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) 內存卡是否存在 ProgressDialog 進度條dialog
創建桌面小部件
1.需要在清單文件裏面配置元數據
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/" />
</receiver>
2.需要配置當前元數據裏面用到的xml res/xml
example_appwidget_info.xml
3.需要配置一個廣播接收者
ExampleAppWidgetProvider
4.實現一個桌面小部件的xml(根據需求:桌面小控件長什麼樣子就實現什麼樣子)
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/preview"
android:initialLayout="@layout/example_appwidget"
android:configure="com.example.android.ExampleAppWidgetConfigure"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard"
android:initialKeyguardLayout="@layout/example_keyguard">
</appwidget-provider>
桌面小部件管理 AppWidgetManager 遠程view 設置文本view.setTextViewText(R.id.xxx,"xxxx"); 設置onclick()爲 view.setonclickpengdingIntent(xxx,xxx);
按照比例變化:
ScaleAnimation sa=new ScaleAnimation(0.5f, 1f, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
旋轉:
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 設置動畫的時間
rotateAnimation.setDuration(5000);
// 設置動畫無限循環
rotateAnimation.setRepeatCount(Animation.INFINITE);
隱藏鍵盤 editortext.setinputtype(Inputtype.type.null);