使用scala+scaloid+IntelliJ IDEA 12开发android应用

(http://cxfksword.me/2013/06/30/develop-android-with-intellij-idea-12-and-scaloid/)

软件环境

继续下面的示例前,需要保证安装了下面的软件:

  • JDK 1.6
  • Android SDK
  • IntelliJ IDEA 12
  • Scala 2.10.2
  • Maven 3

    IntelliJ IDEA 12正常也可以换为Android Studio,不过因为Android Studio是基于IntelliJ IDEA 13 EAP开发的,当前scala plugin还不支持13 EAP,所以还是建议使用IntelliJ IDEA 12,等scala plugin支持最新版后,再换为Android Studio进行开发。

示例项目

hello-scaloid-maven是一个完全使用maven编译的示例项目,借助android maven plugin插件可以把代码编译为android的apk文件。

1、下载hello scaloid maven项目到本地

1
git clone https://github.com/pocorall/hello-scaloid-maven.git hello-scaloid-maven

2、打开POM.xml文件,在中加上Android SDK的本地路径

1
2
3
4
    <properties>
      <scala.version>2.10.2</scala.version>
      <android.sdk.path>/Applications/android sdk/</android.sdk.path>
  </properties>

3、执行maven命令下载依赖软件

1
mvn clean package

4、当提示build success后,用IntelliJ IDEA的“File->Open Project”打开POM.xml文件。这里不建议直接使用mvn idea:idea命令生成的IntelliJ项目文件,因为IntelliJ一直在更新,而mvn生成的项目文件可能格式比较旧了。

5、因为android不包括scala类库,所以编译时需要把scala和scaloid类库也编译到apk中,但一个apk是有方法数量限制的,当不使用Proguard进行裁减优化时,使用IntelliJ IDEA编译项目经常会提示错误:

1
2
3
4
5
Android Dex: [hello-scaloid-maven] trouble writing output: Too many methods: 112662; max is 65536. By package:
Android Dex: [hello-scaloid-maven] 2 android.annotation
Android Dex: [hello-scaloid-maven] 32 android.gesture
Android Dex: [hello-scaloid-maven] 48101 org.scaloid.common
...

6、IntelliJ IDEA默认不打开Proguard,下面我们修改配置打开Proguard。打开“File->Prject Structure->Facets->android->compiler”,勾选上Enable ProGuard项。并把Config file指向项目根目录的proguard.cfg文件。

注意:MAC下android sdk目录假如有空格的话,编译时会提示错误:

Unable to access jarfile /Applications/Android

这时需要改下Proguard脚本。打开“$(android sdk path)/tools/proguard/bin/proguard.sh”文件,把“$PROGUARD_HOME/lib/proguard.jar”路径用双引号括起来。详细可参考:http://fiahfy.blogspot.com/2013/06/unable-to-access-jarfile.html

7、设置好Proguard后,再使用IntelliJ IDEA编译应该就能成功了。Have fun!!

Tip:假如你觉得android模拟器太慢,可以参考这篇文章做优化,速度提高10倍!!https://www.virag.si/2012/10/speeding-up-android-emulator/

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据