Akka on Android multiple reference.conf

2020-02-29 04:09发布

问题:

I'm trying to add akka and spray to my android app (written in Scala with Scaloid lib). Everything should work fine, my ide is not throwing any errors. I found some links addressing the same problem, but nothing helped me so far. When I build my project with:

sbt ~android:install

When I have the following line in my build.sbt

apkbuildExcludes in Android += "reference.conf"

I get the error

08-31 08:02:45.598    7884-7884/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: at.itn.android, PID: 7884
    com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'

without it

[info] Generating dex, incremental=true
com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK reference.conf
    File 1: /home/markus/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.3.5.jar
    File 2: /home/markus/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.3.5.jar

I don't have any reference.conf (I also tried the reference.conf https://github.com/akka/akka/blob/master/akka-actor/src/main/resources/reference.conf )

here's my build.sbt

android.Plugin.androidBuild

name := "myApp"
scalaVersion := "2.11.0"

proguardCache in Android ++= Seq(
  ProguardCache("org.scaloid") % "org.scaloid"
)

proguardOptions in Android ++= Seq(
  "-dontobfuscate",
  "-dontoptimize",
  "-dontwarn scala.collection.mutable.**",
  "-ignorewarnings",
  "-keep class scala.Dynamic"
)

apkbuildExcludes in Android += "reference.conf"

libraryDependencies ++= {
  Seq(
    "org.scaloid" %% "scaloid" % scaloidV,
    "com.typesafe.akka" % "akka-actor_2.11" % akkaV
    // ....
  )
}

Can someone send me in the right direction on how I can get akka running on android?

回答1:

I have a working example of using akka and spray on android. Check it out. https://github.com/anjeikatkov/android-akka-spray-example

Hope, it helps.



回答2:

I have described a solution on the scala-on-android mailing list a while ago. Here’s the link: https://groups.google.com/d/msg/scala-on-android/JwMQCtC2zCs/rHHg4BU55W0J.

TL;DR: The main issue seems to be the wrong classloader (you should see a warning in logcat). I preloaded the config in the Application:

class MyApplication extends Application {
  ...
  val config = ConfigFactory.load()
  ...
}

And also pointed ActorSystem to the right classloader explicitly:

val app = getApplication.asInstanceOf[MyApplication] 
val actorSystem = ActorSystem("MyActorSystem", app.config, app.getClassLoader)

Hope that helps



回答3:

You should place the "reference.conf" file from akka repostiory into /src/main/resources directory of your project.



回答4:

Only to have an complete answer:

There seems to be three kinds of directory layout that are accepted by android-sdk-plugin:

  1. Ant
  2. Gradle
  3. Wrapped

All answers that I find says that I should put my reference.conf file on $BASE/src/main/resources/. But looking at ProjectLayout.scala we can see that this advice only really works in Gradle layouts.

On my project, the plugin is using the Ant layout, so it should be put on $BASE/resources/