[Android]使用Kotlin+Anko開發Android(一)

來源:互聯網
上載者:User

標籤:

以下內容為原創,歡迎轉載,轉載請註明

來自天天部落格:http://www.cnblogs.com/tiantianbyconan/p/4800656.html 

 

Kotlin是由JetBrains開發並且開源的靜態類型JVM語言。比Java語言文法簡潔,支援很多Java中不支援的文法特性,如高階函數、內聯函數、null安全、靈活擴充、操作符重載等等。而且它還完全相容Java,與Scala類似,但是Scala的宗旨是“儘可能自己實現,不得已才使用Java”,而Kotlin卻相反:“儘可能複用Java的實現,不得已才自己實現”。所以相比之下Kotlin更簡潔輕量,非常適合移動端的開發。另外JetBrains針對Android開發提供了一個由Kotlin實現的“anko”開源庫,可以讓你使用DSL的方式直接用代碼編寫UI,讓你從繁瑣的xml中解脫出來,而且避免了xml解析過程所帶來的效能問題。

 

這篇先講怎麼去使用idea(Android Studio使用者也一樣)搭建Kotlin的Android開發環境。

 

一、下載以下相關idea外掛程式:

1. Kotlin

2. Kotlin Extensions For Android

3. Anko DSL Preview

其中Anko DSL Preview外掛程式用於預覽使用DSL編寫的UI代碼,就像以前使用xml編寫UI檔案時可以動態在“Preview”視窗預覽效果一樣。

 

二、建立Android項目

在src/main目錄下,建立kotlin目錄(用於放置kotlin代碼),配置Gradle如下:

 1 buildscript { 2     ext.kotlin_version = ‘0.12.1230‘ 3     repositories { 4         mavenCentral() 5     } 6     dependencies { 7         classpath ‘com.android.tools.build:gradle:1.1.1‘ 8         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9         classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"10     }11 }12 apply plugin: ‘com.android.application‘13 apply plugin: ‘kotlin-android‘14 15 repositories {16     mavenCentral()17 }18 19 android {20     compileSdkVersion 2221     buildToolsVersion "22.0.1"22 23     defaultConfig {24         applicationId "com.wangjie.androidwithkotlin"25         minSdkVersion 926         targetSdkVersion 2227         versionCode 128         versionName "1.0"29     }30 31     sourceSets {32         main.java.srcDirs += ‘src/main/kotlin‘33     }34 35     buildTypes {36         release {37             minifyEnabled false38             proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘39         }40     }41 }42 43 dependencies {44     compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])45     compile ‘com.android.support:appcompat-v7:22.2.0‘46     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"47     compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"48     compile ‘org.jetbrains.anko:anko:0.6.3-15‘49 }

然後sync & build。

 

三、配置Kotlin

調用“Configuring Kotlin in the project”這個Action

 

四、把Java代碼一鍵轉成kotlin代碼

開啟要轉換的Java檔案,調用“Convert Java File to Kotlin File”這個Action即可。

轉換之前的Java代碼:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

轉換之後的Kotlin代碼:

public class MainActivity : ActionBarActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)    }    override fun onCreateOptionsMenu(menu: Menu?): Boolean {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu)        return true    }    override fun onOptionsItemSelected(item: MenuItem?): Boolean {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        val id = item!!.getItemId()        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true        }        return super.onOptionsItemSelected(item)    }}

 

[Android]使用Kotlin+Anko開發Android(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.