Skip to content

Commit

Permalink
Merge pull request #1 from bboyfeiyu/master
Browse files Browse the repository at this point in the history
在Android调试模式中使用Stetho
  • Loading branch information
BillionWang committed Apr 3, 2015
2 parents b5e4087 + 74d9a40 commit 5e2045d
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 10 deletions.
142 changes: 142 additions & 0 deletions androidweekly/Kotlin for Android (II)创建一个工程/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
Kotlin for Android (II)创建一个工程
---

>
* 原文标题 : Kotlin for Android (II): Create a new project
* 原文链接 : [Kotlin for Android (II): Create a new project](http://antonioleiva.com/kotlin-android-create-project/)
* 译者 : [Lollypo](https://github.com/Lollypo)
* 校对者: [chaossss](https://github.com/chaossss)
* 状态 : 完成


当我从[what Kotlin is and what it can do for us](http://antonioleiva.com/kotlin-for-android-introduction/)获得一些启发之后,觉得是时候配置下 Android Studio来帮助我们使用Kotlin开发Android应用程序了. 其中有些步骤只需要在初次使用时完成一次, 但是其他一些Gradle配置需要为每一个新项目做一遍.

对于本系列文章, 我将创建一个我早些时候创建的[Bandhook](https://play.google.com/store/apps/details?id=com.limecreativelabs.bandhook)的简化版本, 它基本上就是连接到一个基于RESTful的音乐API然后接收一些乐队的信息. 链接到 [Bandhook Kotlin on Github](https://github.com/antoniolg/Bandhook-Kotlin) 查看源代码.


###创建一个新项目然后下载Kotlin插件###

就像你平常做的那样,我们只需要用Android Studio创建一个带Activity的基本Android项目。

一旦完成,我们需要做的第一件事就是去下载Kotlin插件. 去到Android Studio的系统设置中然后查找plugins.之后,再次使用搜索找到Kotlin插件,安装并重启IDE。

![kotlin-plugin](http://7xi8kj.com1.z0.glb.clouddn.com/kotlin-plugin-e1424632570741.png)

###添加Kotlin插件的依赖到的应用程序的build.gradle中###

该项目的build.gradle需要添加一个新的依赖,这个依赖将会被Kotlin插件要求以在主Module中使用:
```gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.11.91'
}
}
```



###配置Module的build.grade###

首先, 应用Kotlin插件:
```gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
```
接着, 添加Kotlin库到你的依赖:
```gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.jetbrains.kotlin:kotlin-stdlib:0.11.91'
}
```
最后, 你需要添加我们在下一个步骤创建的Kotlin文件夹:
```gradle
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
...
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
```
或者,你可以跳过这一步,当做完下一个步骤时,使用这个Android Studio的操作:

![configure-kotlin-project](http://7xi8kj.com1.z0.glb.clouddn.com/configure-kotlin-project.png)

我更倾向于手动去做以保持我的Gradle文件有整洁有序, 但第二个选项可能较为容易些。



###创建Kotlin文件夹###

如果你将项目的视图从‘Android’转到‘Project’,那将会非常容易。依次选择‘app->src->main’ 然后创建一个名为 ‘kotlin'的文件夹:

![kotlin-folder](http://7xi8kj.com1.z0.glb.clouddn.com/kotlin-folder.png)



###将Java activity转换成Kotlin文件###

Kotlin插件能将Java转换为Kotlin类. 我们可以轻松的通过‘Code’菜单中的‘Convert Java File to Kotlin File'选项转换当前的Activity到Kotlin类 :

![convert-java-to-kotlin](http://7xi8kj.com1.z0.glb.clouddn.com/convert-java-to-kotlin-e1424633562637.png)

IDE将建议你移动新文件到Kotlin文件夹,点击‘Move File’(或者手动完成,假如你没看到这个选项).
```java
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)
}
}
```



###主要区别###

看一看之前的代码, 我们可以看到一些明显的差异。 其中很大一部分我们将会在下一篇文章讲解到:

- 使用冒号,而不是'extends'。
- 显式使用‘override': 在Java中, 我们可以使用一个注释使我们的代码更清晰,但它不是必要条件. Kotlin将迫使我们使用它.
- 函数则使用‘fun’关键字: Kotlin是一个面向对象的函数式语言, 因此可能会与其他语言类似,例如Scala. Java方法被函数的形式表示。
- 函数参数命名规则不同: 类型和名称都写在相反的位置,并用冒号隔开。
- 分号可选: 我们不需要在行的结尾处加上分号。如果我们想要也可以加上, 但如果我们不这样做,它就可以节省大量的时间,并使我们的代码整洁。
- 其他小细节: 在简介一文中, 我已经说到了 ‘?’ 的意义. 这表明参数可以为空。NULL的处理方式不同于Java。



###总结###

也许我们会认为使用一门新语言将会非常困难, Kotlin被JetBrains团队开发出来的,要成为最容易和可交互的语言用来覆盖那些Java的不足之处。由于Android Studio也是基于JetBrains的产品,这将让集成到这个IDE中并且开始工作非常简单。

下一篇文章将介绍一些让我们在使用Kotlin开发Android应用程序时,能让开发过程更简单的奇巧淫技。
11 changes: 1 addition & 10 deletions androidweekly/Square 开源库Flow和Mortar的介绍/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Flow 将一个应用分成一个逻辑上的 Screen组合,Screen不是任何
我们应用中的每一个Activity将会成为一个 Flow 对象,Flow对象在返回栈中保存了 Screen 的记录,和 Activity 或者 FragmentManager 的返回栈有些类似,通过这样的设计允许我们在 Screen 之间通过简单地实例化就可以轻松的切换,而不需要在应用中包含很多Activity。这里有一小部分 Activity(最好是一个)来持有这些 Screen。他们之间的关系下图类似:
![screen](http://www.bignerdranch.com/img/blog/2015/02/screen.png)

我们我们想切换到一个新的 Screen,我们只需简单地实例化这个 Screen,并且告诉我们 Flow 对象帮助我们切换为这个 Screen。除此以外,正如我们所期待的,Flow 被实例化后也会实现 goBack() 和 goUp() 方法。然而,许多开发者都把 Java 中的 goto 语句看作洪水猛兽,但事实上 Java 中的 goto 语句并没有它听起来那么恐怖。
如果我们想切换到一个新的 Screen,我们只需简单地实例化这个 Screen,并且告诉我们 Flow 对象帮助我们切换为这个 Screen。除此以外,正如我们所期待的,Flow 被实例化后也会实现 goBack() 和 goUp() 方法。然而,许多开发者都把 Java 中的 goto 语句看作洪水猛兽,但事实上 Java 中的 goto 语句并没有它听起来那么恐怖。
![flow](http://www.bignerdranch.com/img/blog/2015/02/flow.png)

从本质上看,Flow 的作用仅仅是在 App 中告诉我们将要切换到哪一个 Screen。而这样设计的好处在于,Flow 通过这样的设计让我们能够方便地在我们定义的各种不同的自定义 View 中切换,并使我们免受在 Activity 或 Fragment 需要考虑的种种麻烦,让我们把注意力都集中在处理 View上。Flow 为我们创造了一个简单,方便,以 View 为中心的应用架构。
Expand All @@ -84,13 +84,6 @@ Presenter 是一个拥有简单生命周期和伴随其生命周期的 Bundle

完全没有 Fragment 那样复杂的生命周期,这可不是我吹的!

There are a lot of moving parts and new terms and classes and all sorts of room for confusion. So in sum, we have the following pieces of the puzzle:

- Screen: A particular location in the application’s navigation hierarchy
- Blueprint: A section of an application with its own Dagger module
- Presenter: A View-controller object
- Custom Views: Views defined by Java and usually some XML

文章写到这里,你会发现在 Flow 和 Mortar 中有许多发生改变的部分,新的术语和类,还有新的使用规范,这难免会让人一头雾水。所以为了方便大家的理解,总的来说,我们需要重视的是下面几个部分:

- Screen: 在应用导航层次结构中的一个特殊存在,用来代表我们视图的对象
Expand All @@ -104,8 +97,6 @@ Here’s what our final Mortar and Flow architecture looks like:

![](https://www.bignerdranch.com/img/blog/2015/02/mortar-and-flow.png)

Instead of sticking with Model View Controller, the architecture has morphed into more of a Model View Presenter style. The big difference concerns the handling of runtime configuration changes like rotation. In MVC, our Controller (Activities and Fragments) will be destroyed alongside our Views, whereas in MVP, only our View will be destroyed and recreated. Nifty.

抛弃了对 MVC 模式的执念,这个架构在完成之后变得更像 MVP 模式。这样巨大的转变使得新的架构需要关注如何处理应用在运行时配置信息改变的问题,例如:旋转。在 MVC 模式中,我们的控制器(Activity 和 Fragment)会随着我们的 View 一起被杀死。然而,在 MVP 模式中,我们只有 View
被杀死,又在需要它的时候重现它。挺有趣的对吧?

Expand Down
Loading

0 comments on commit 5e2045d

Please # to comment.