標籤:
上周Android的官方部落格發表聲明:Google將在年底結束對Eclipse+ADT的開發以及停止支援,把重心完全轉移到Android Studio。對於很多使用Eclipse的Android開發人員而言,他們必須接受一個全新的IDE。按照Google的說法,Android Studio非常的智能和強大。這裡分享下Android Studio的一些基本體驗。
參考原文:Time to Migrate Android Projects to Android Studio
Xiao Ling
翻譯:yushulx
Android Studio下載安裝
這個大家都懂的,用VPN搞定。完整安裝包可以用迅雷離線,會比較快。
Eclipse ADT工程匯入Android Studio
步驟很簡單:
File->New->New Projects
設定好代碼路徑
最後確定
這個時候編譯還不能完全通過,會有一些錯誤。比如output路徑需要設定。Android Studio會有提示,全部fix之後就可以了
Android Studio中好用的快速鍵
Android Studio開啟的時候會自動跳出一些小技巧,可以學習下捷徑:
Ctrl+ALT+Shift+N: 全域搜尋任何符號
Ctrl+N: 全域搜尋任何類
Ctrl+Shift+A: 尋找功能表命令和工具列功能
Alt+F7: 尋找變數的使用
Ctrl+ALT+Shift+S: 開啟工程屬性
Ctrl+Shift+Enter: 輸入圓括弧後可以自動添加大括弧
Alt+Enter: 自動修複錯誤
Ctrl+Alt+T: 自動添加一些代碼模板,比如try/catch
Shift+Shift: 全域搜尋各種檔案,類,變數,等等
建立一個簡單的工程
看一下Android Studio和Eclipse的一些差異
工程建立嚮導
File->New->New Project:
Android Studio會幫你選擇目標裝置
有各種Activity的模板可以選擇。
如果要學習sample,可以從GitHub匯入。
通過Gradle構建Android應用
Android Studio使用Gradle來構建Android工程。
AndroidManifest.xml和Eclipse中是不一樣的。SDK版本都被轉移到了build.gradle檔案中:
apply plugin: ‘com.android.application‘ android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "dynamsoft.com.demo" minSdkVersion 19 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ } }} dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) compile ‘com.android.support:appcompat-v7:22.2.0‘}
Android UI編輯器
Android Studio的UI編輯器非常方便,可以即時預覽:
建立自訂View
在Eclipse中建立自訂View是很麻煩的,很容易出錯。Android Studio中是完全自動建立的。只需要點擊菜單New -> UI Component -> Custom View就可以了。
對應的檔案會自動產生。
如何產生簽名APK
要發布app的人需要給apk簽名。Android Studio裡也是很簡單的。
Android Studio中如何添加依賴
在Android工程中我們經常需要使用第三方的庫或者原始碼。在Android Studio中提供了3種添加依賴的方式。使用Ctrl+ALT+Shift+S開啟工程屬性,選擇dependencies,可以看到:
Library dependency是從maven的倉庫中添加依賴
File dependency是添加代碼檔案或者jar檔案
Module dependency是在工程中建立一個module(比如Zxing的源碼),並添加進來
使用Zxing建立一個簡單的QR code掃描應用
先從GitHub擷取源碼:
git clone https://github.com/zxing/zxing
建立一個module:
匯入Zxing源碼:
添加module dependency:
建立一個簡單的layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/editText" android:text="Generate QRCode" android:id="@+id/barcode_button" /> <ImageView android:layout_width="400dp" android:layout_height="400dp" android:layout_below="@id/barcode_button" android:id="@+id/barcode" android:background="@color/abc_search_url_text_normal"/></RelativeLayout>
使用QRCodeWriter產生QR二維碼:
public void onClick(View v) { String content = mEditText.getText().toString(); QRCodeWriter write = new QRCodeWriter(); try { int width = mImageView.getWidth(); int height = mImageView.getHeight(); BitMatrix bitMatrix = write.encode(content, BarcodeFormat.QR_CODE, width, height); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { bitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK: Color.WHITE); } } mImageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); }}
運行效果:
Android Studio比較Eclipse
和Eclipse比,Android Studio是全新的體驗。Android Studio在使用的時候偶爾會出現卡住,不過功能真的如Google說的非常強大,包含了各種有用的捷徑,代碼智能提示,程式碼分析等。在熟悉了操作之後,完全不會再想回到Eclipse了。
Google將專註於Android Studio,放棄Eclipse+ADT