Eclipse中的Andr​​oid,斯卡拉取得容易,但仍然无法正常工作 [英] Eclipse, Android, Scala made easy but still does not work

查看:188
本文介绍了Eclipse中的Andr​​oid,斯卡拉取得容易,但仍然无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我跟着编程的方式为Android使用Scala和Eclipse ,从而降低了code和编译时不使用Proguard的或Treeshake。

I recently followed a way of programming for Android using Scala and Eclipse, which reduces the code and the compile time without using Proguard or Treeshake.

以下这篇文章,我应该能够利用最后的Eclipse版本(3.7),斯卡拉几乎是最后一个版本(2.8.1)更新模拟器版本10,2.8.3版本在Eclipse的插件提供研究。

Following this article, I should be able use the last Eclipse build (3.7), almost the last version of Scala (2.8.1) updated on an emulator version 10, the version 2.8.3 within Eclipse with the provided plug-in.

在presented方式是提供一个具体的ramdisk映像的版本,在这里我们可以上传斯卡拉库,从而大大缩小的code尺寸上传到模拟器。

The presented way is to provide a specific ramdisk image version, where we can upload scala libraries, which drastically shrinks the size of the code to upload to the emulator.

我遵循的步骤,创建一个Hello World,添加斯卡拉自然,增加了一个虚拟的Scala类,移动之前的Andr​​oid包安装斯卡拉建设者,一切都建立完美的,但是当我启动的apk上的模拟器在Eclipse中,应用程序崩溃,我得到了下面的错误,它看起来像 一样在这里psented (在文件的结尾)$ P $

I followed the steps, created a hello world, added scala nature, added a dummy scala class, moved the Scala builder before the Android Package Installer, everything builds perfectly, but when I launch the apk on a emulator from Eclipse, the application crashes and I get the following error, which looks like the same as presented here (at the end of the document) :

    03-29 10:29:38.505: E/AndroidRuntime(839): java.lang.NoClassDefFoundError: upg.TestSinceInstallation.ComputeSum

如果我删除了活动文件中的斯卡拉参考,运行良好。

If I remove the scala reference in the activity file, it runs well.

下面是TestSinceInstallation.java文件:

Here is the TestSinceInstallation.java file:

    package upg.TestSinceInstallation;

    import android.app.Activity;
    import android.os.Bundle;
    import upg.TestSinceInstallation.ComputeSum;

    public class TestSinceInstallationActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            int a = 1;
            int b = 5;
            ComputeSum cs = new ComputeSum(a, b);
            if(cs.getResut() == 6) {
              setContentView(R.layout.main);
            }
        }
    }

和这里的ComputeSum.scala文件

and here is the ComputeSum.scala file

    package upg.TestSinceInstallation

    class ComputeSum(a: Int, b: Int) {
      def getResut() : Int = a + b
    }

你觉得我应该做的,使这项工作?我觉得自己如此接近的目标。

What do you think I should do to make this work ? I feel so close to the goal.

推荐答案

下面是解决方案,使用Android使用的的Eclipse 3.7 ,并使用Scala 3.0.0没有任何问题。

Here is the solution, to use Android with Eclipse 3.7 and with Scala 3.0.0 without any problems.

  • 在安装的Eclipse 3.7 (对我来说3.7.2)和< A HREF =htt​​p://developer.android.com/sdk/index.html#ExistingIDE> Android SDK中 - 加的Java SDK 7v22 如果您的系统没有。 注意:特殊Android的ADT包不允许安装斯卡拉(截至2013年6月,LINUX站)
  • 在安装Android ADT插件版本22 Eclipse的指向Eclipse来这个网站:
  • Install Eclipse 3.7 (for me 3.7.2) and the Android SDK - plus the Java SDK 7v22 if not already in your system. Caution: The special Android ADT Bundle does not allow to install scala (as of June 2013, linux station).
  • Install Android ADT plug-in version 22 for Eclipse by pointing Eclipse to this website :

https://dl-ssl.google.com/android/eclipse/

  • Install Scala IDE version 3.0.0 by pointing Eclipse to this website :
  • <一个href="http://download.scala-ide.org/sdk/e37/scala210/stable/site">http://download.scala-ide.org/sdk/e37/scala210/stable/site

    • Install the AndroidProguardScala plug-in v47 by pointing Eclipse to this website :
    • <一个href="https://androidproguardscala.s3.amazonaws.com/UpdateSiteForAndroidProguardScala">https://androidproguardscala.s3.amazonaws.com/UpdateSiteForAndroidProguardScala

      现在,创建一个斯卡拉项目,

      Now, to create a scala project,

      1. 创建一个Android项目照常
      2. 在项目上单击右键,配置添加斯卡拉自然
      3. 在项目上单击右键,添加AndroidProguardScala自然
      1. Create an Android project as usual
      2. Right-click on the project, Configure, Add Scala nature
      3. Right-click on the project, Add AndroidProguardScala nature

      您就大功告成了。

      现在美好的事情发生。 首先,你可以scalafy任何活动,你将获得斯卡拉独特的功能,如:

      Now good things happen. First, you can scalafy any activity, and you will get access to scala unique features, such as:

      • 懒惰的评价定义类体内的意见
      • 隐式转换功能与您的code自定义视图的交互
      • 否分号和Scala的所有语法糖。
      • 使用演员的活动 从处理线程区分UI线程。
      • Lazy evaluations to define views inside the class body
      • Implicit conversion functions to customize the interaction of view with your code
      • No semicolons and all the syntactic sugar of scala.
      • Use of actors for activities to distinguish the UI thread from the processing thread.

      下面是其中一些的例子。

      Here is an example of some of them.

      package com.example.testing;
      import android.app.Activity
      import android.os.Bundle
      import scala.collection.mutable.Map
      import android.view.View
      import android.widget.SeekBar
      import android.widget.ImageButton
      import android.graphics.drawable.Drawable
      import android.widget.TextView
      
      trait ActivityUtil extends Activity {
        implicit def func2OnClickListener(func: (View) => Unit):View.OnClickListener = {
          new View.OnClickListener() { override def onClick(v: View) = func(v) }
        }
        implicit def func2OnClickListener(code: () => Unit):View.OnClickListener = {
          new View.OnClickListener() { override def onClick(v: View) = code() }
        }
        private var customOnPause: () => Unit = null
        override def onPause() = {
          super.onPause()
          if(customOnPause != null) customOnPause()
        }
        def onPause(f: =>Unit) = {
          customOnPause = {() => f}
        }
        private var customOnCreate: Bundle => Unit = null
        override def onCreate(savedInstanceState: Bundle) {
          super.onCreate(savedInstanceState)
          if(customOnCreate != null) customOnCreate(savedInstanceState)
        }
        def onCreate(f: Bundle => Unit) = {
          customOnCreate = f
        }
        // Keep references alive when fetched for the first time.
        private implicit val vMap = Map[Int, View]()
        private implicit val ibMap = Map[Int, ImageButton]()
        private implicit val sbMap = Map[Int, SeekBar]()
        private implicit val tvMap = Map[Int, TextView]()
        private implicit val dMap = Map[Int, Drawable]()
      
        def findView[A <: View](id: Int)(implicit v: Map[Int, A]): A = v.getOrElseUpdate(id, findViewById(id).asInstanceOf[A])
        def findDrawable[A <: Drawable](id: Int)(implicit v: Map[Int, A]): A = v.getOrElseUpdate(id, getResources().getDrawable(id).asInstanceOf[A])
      
        implicit class RichView(b: View) { // Scala 2.10 !
          def onClicked(f: =>Unit) = b.setOnClickListener{ () => f }
        }
        // Implicit functions to operate directly on integers generated by android.
        implicit def findViewImageButton(id: Int): ImageButton = findView[ImageButton](id)
        implicit def findViewSeekBar(id: Int): SeekBar = findView[SeekBar](id)
        implicit def findViewTextView(id: Int): TextView = findView[TextView](id)
        implicit def findDrawable(id: Int): Drawable = findDrawable[Drawable](id)
        implicit def findRichView(id: Int): RichView = toRichView(findView[View](id))
      }
      

      现在毕竟是previous样板,这是非常有用的编写简明的活动。请注意如何我们可以直接IDS操作的,如果他们的意见。消歧需要为(查看:TextView中).requestFocus()如果这些方法可以从不同的结构来推断

      Now after all the previous boilerplate, it is very useful to write concise activities. Note how we can directly operate on ids as if they were views. Disambiguation is needed as (view: TextView).requestFocus() if the methods can be inferred from various structures.

      // Now after all the boilerplate, it is very useful to write consise activities
      class MyActivity extends Activity with ActivityUtil {
        import R.id._ // Contains R.id.button, R.id.button2, R.id.button3, R.id.mytextview
        lazy val my_button: ImageButton = button   //In reality, R.id.button
        lazy val his_button: ImageButton = button2
      
        onCreate { savedInstanceState =>       // The type is automatically inferred.
          setContentView(R.layout.main)
          my_button.setOnClickListener(myCustomReactClick _)
          his_button.setOnClickListener { () =>
             //.... Scala code called after clicking his_button
          }
          button3.onClicked {
            // Awesome way of setting a method. Thanks Scala.
          }
          mytextview.setText("My text")  // Whoaaa ! setText on an integer.
          (mytextview: TextView).requestFocus() // Disambiguation because the method is ambiguous
          // Note that because of the use of maps, the textview is not recomputed.
        }
      
        def myCustomReactClick(v: View) = {
          // .... Scala code called after clicking my_button
        }
        onPause{
          // ... Custom code for onPause
        }
      }
      

      请确保斯卡拉文件的名称匹配包含的主要活动,在这种情况下,它应该是 MyActivity.scala

      Make sure that the name of the scala file matches the main activity contained in it, in this case it should be MyActivity.scala.

      第二,要建立一个Scala的项目作为库项目,使用的是具有不同的资源应用的基础上,按照<一href="http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject">regular建立一个库项目方式。在要作为一个基础库项目,属性安卓 Scala的项目用鼠标右键单击,并检查 isLibrary
      要使用此库创建推导项目,可以为它生成APK,创建一个新的Andr​​oid项目,不添加任何阶或androidproguardscala性质,只需右键单击,属性安卓,并添加previous斯卡拉项目作为一个库。

      Second, to set up a scala project as a library project, to use is as a base for applications having different resources, follow the regular way of setting up a library project. Right-click on the scala project that you want as a base library project, Properties, Android, and check isLibrary.
      To create derivated project using this library and for which you can generate an APK, create a new android project, and without adding any scala or androidproguardscala nature, just right-click, Properties, Android, and add the previous scala project as a library.

      更新随着Android插件的新版本,你应该去项目性状&gt;构建路径&GT;订单和出口和检查 Android的私人图书馆。这将允许出口的Scala库,无论是在库项目和主要项目,即使主体工程未分配斯卡拉。

      UPDATE With the new version of the Android Plug-in, you should go to Project Properties > Build Päth > Order and Export and check Android Private Libraries. This will allow to export the scala library, both in the library project and the main project, even if the main project is not assigned Scala.

      测试使用插件 Robolectric 可以很容易地测试你的android斯卡拉项目。只要按照步骤创建一个测试项目,添加斯卡拉自然吧。您甚至可以使用新的斯卡拉调试器,并加入 scalatest 库,你可以使用应该匹配器等多项功能Scala有。

      TESTING Using the plug-in Robolectric makes it easy to test your android scala project. Just follow the steps for creating a test project, add Scala nature to it. You can even use the new scala debugger, and by adding the scalatest library, you can use should matchers and many other features Scala has.

      这篇关于Eclipse中的Andr​​oid,斯卡拉取得容易,但仍然无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆