복붙노트

[SCALA] 이클립스, 안드로이드, 스칼라 간편한 있지만 작동하지 않는

SCALA

이클립스, 안드로이드, 스칼라 간편한 있지만 작동하지 않는

나는 최근에 코드와 Proguard와 또는 Treeshake를 사용하지 않고 컴파일 시간을 단축 안드로이드 사용하여 스칼라 이클립스, 프로그래밍의 방법을 따랐다.

이 문서에 따라, 나는 수 사용을 마지막 이클립스 빌드 (3.7), 스칼라의 거의 마지막 버전 에뮬레이터 버전 10에 업데이트 (2.8.1), 제공되는 플러그인과 이클립스 버전 2.8.3을해야합니다.

제시된 방법은 우리가 크게 에뮬레이터에 업로드 코드의 크기를 축소 스칼라 라이브러리를 업로드 할 수있는 특정 램 디스크 이미지 버전을 제공하는 것이다.

나는 단계를 따라 헬로 세상을 창조, 스칼라 자연을 추가 더미 스칼라 클래스를 추가, 안드로이드 패키지 설치 전에 스칼라 빌더를 이동, 모든 것을 완벽하게 구축,하지만 난 이클립스, 응용 프로그램이 충돌과에서 에뮬레이터에 APK를 시작할 때 나는처럼 보이는 다음과 같은 오류를 얻을 같은 (문서의 끝 부분에) 여기에 제시된 같은 :

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

내가 활동 파일의 스칼라 참조를 제거하면 잘 실행됩니다.

여기에 TestSinceInstallation.java 파일은 다음과 같습니다

    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 파일입니다

    package upg.TestSinceInstallation

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

내가이 일을하기 위해 어떻게해야 어떻게 생각하십니까? 나는 목표에 가까운 느낌.

해결법

  1. ==============================

    1.여기에 솔루션은 이클립스 3.7 및 스칼라 아무 문제없이 3.0.0로 안드로이드를 사용하는 것이다.

    여기에 솔루션은 이클립스 3.7 및 스칼라 아무 문제없이 3.0.0로 안드로이드를 사용하는 것이다.

    자, 스칼라 프로젝트를 만듭니다,

    당신은 완료됩니다.

    이제 좋은 일들이 일어난다. 첫째, 당신은 어떤 활동을 scalafy 수 있으며, 같은 스칼라 고유의 기능에 대한 액세스를 얻을 것이다 :

    여기에 그들 중 일부의 예입니다.

    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))
    }
    

    이제 이전의 모든 보일러 후에는 간결 활동을 쓰기 위해 매우 유용합니다. 그들이보기 것처럼 우리가 직접 ID를에 작동 할 수있는 방법을합니다. .requestFocus () 메소드는 다양한 구조에서 유추 할 수있는 경우 : 동음은 (텍스트 뷰 도면)으로서 필요하다.

    // 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해야합니다.

    둘째, 서로 다른 자원을 가진 애플리케이션을위한 기지로이다 사용하려면 라이브러리 프로젝트와 같은 스칼라 프로젝트를 설정 라이브러리 프로젝트를 설정하는 일반적인 방법을 따르십시오. 당신이 기본 라이브러리 프로젝트, 속성, 안드로이드로 사용할 스칼라 프로젝트를 마우스 오른쪽 단추로 클릭 한면 IsLibrary을 확인합니다. 이 라이브러리를 사용하여 유래 된 프로젝트를 만들려면하고있는 당신은 APK를 생성 할 수 있습니다, 새로운 안드로이드 프로젝트를 생성하고, 어떤 스칼라 또는 androidproguardscala 성격, 단지 마우스 오른쪽 버튼으로 클릭, 속성, 안드로이드를 추가하지 않고, 및 라이브러리로 이전 스칼라 프로젝트를 추가 할 수 있습니다.

    안드로이드 플러그인의 새 버전으로 업데이트, 당신은 프로젝트 속성> 경로> 주문 및 내보내기 빌드로 이동 안드로이드 개인 라이브러리를 확인해야합니다. 이것은 주요 프로젝트가 스칼라가 할당되지 않은 경우에도, 라이브러리 프로젝트와 주요 프로젝트에 모두 스칼라 라이브러리를 내보낼 수 있습니다.

    플러그인 Robolectric를 사용하여 테스트하는 것은 쉽게 안드로이드의 스칼라 프로젝트를 테스트 할 수 있습니다. 다만, 테스트 프로젝트를 생성하기위한 단계를 수행 그것에 스칼라 자연을 추가 할 수 있습니다. 당신은 새로운 스칼라 디버거를 사용할 수 있으며, scalatest 라이브러리를 추가하여, 당신은 정합해야 사용할 수있는 많은 다른 스칼라가 있습니다.

  2. from https://stackoverflow.com/questions/9924015/eclipse-android-scala-made-easy-but-still-does-not-work by cc-by-sa and MIT license