简体   繁体   中英

How to add unmanaged jars to build.sbt generated by a g8 template?

I am working on libgdx/android project in Scala. I have build.sbt generated by libgdx-sbt-project.g8 template. I am trying to add a jar file. Could somebody help me out? Here is the code:

lazy val android = common ++ Tasks.natives ++ Seq(
versionCode := 0,
keyalias := "change-me",
platformName := "android-19",
mainAssetsPath in Compile := file("common/assets"),
unmanagedJars in Compile <+= (libraryJarPath) (p => Attributed.blank(p)) map( x=> x),
libraryDependencies ++= Seq(
  "com.badlogicgames.gdx" % "gdx-backend-android" % "0.9.9",
  "com.badlogicgames.gdx" % "gdx-platform" % "0.9.9" % "natives" classifier "natives-armeabi",
  "com.badlogicgames.gdx" % "gdx-platform" % "0.9.9" % "natives" classifier "natives-armeabi-v7a"
),
nativeExtractions <<= (baseDirectory) { base => Seq(
  ("natives-armeabi.jar", new ExactFilter("libgdx.so"), base / "lib" / "armeabi"),
  ("natives-armeabi-v7a.jar", new ExactFilter("libgdx.so"), base / "lib" / "armeabi-v7a")
)})

I am new with Scala and SBT. I don't quite understand this syntax:

unmanagedJars in Compile <+= (libraryJarPath) (p => Attributed.blank(p)) map( x=> x)

Save the jar(s) to the directory pointed by libraryJarPath , reload the project and you should have them on Compile classpath.

To query for the value of the libraryJarPath setting execute show libraryJarPath in sbt shell.

It'd be quite helpful if you shared the project on GitHub so it'd be easier to help out.

Trying out libgdx-sbt-project.g8 myself

Provided you work with ajhager/libgdx-sbt-project.g8 project template with the defaults:

~/sandbox/so
▶ g8 ajhager/libgdx-sbt-project
package [my.game.pkg]:
name [My Game]:
scala_version [2.10.3]:
api_level [19]:
libgdx_version [0.9.9]:

Template applied in ./my-game

~/sandbox/so
▶ cd my-game

I would first update sbt used in the project:

sandbox/so/my-game
▶ cat project/build.properties
sbt.version=0.12.4

0.12.4 is quite old since 0.13.2 is out and 0.13.5 around the corner. They brought many build definition syntax changes that are supposed to ease working with builds.

I'm working with 0.13.6-SNAPSHOT launcher:

sandbox/so/my-game
▶ xsbt --version
sbt launcher version 0.13.6-SNAPSHOT

Starting up sbt:

sandbox/so/my-game                                                                                                                                                             
▶ xsbt
Getting org.scala-sbt sbt 0.12.4 ...
...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/jacek/sandbox/so/my-game/project/target/scala-2.9.2/sbt-0.12/classes...
[error] Android SDK not found. You might need to set ANDROID_SDK_HOME or ANDROID_SDK_ROOT or ANDROID_HOME
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? q

sandbox/so/my-game
▶ ANDROID_SDK_HOME=~/apps/android-sdk-mac_x86 xsbt
[info] Loading project definition from /Users/jacek/sandbox/so/my-game/project
[info] Set current project to all-platforms (in build file:/Users/jacek/sandbox/so/my-game/)
>

inspect the unmanagedJars in Compile gives:

> show compile:unmanaged-jars
[info] common/compile:unmanaged-jars
[info]  ArrayBuffer()
[info] desktop/compile:unmanaged-jars
[info]  ArrayBuffer()
[info] android/compile:unmanaged-jars
[info]  ArrayBuffer(Attributed(/Users/jacek/apps/android-sdk-mac_x86/platforms/android-19/android.jar))
[info] ios/compile:unmanaged-jars
[info]  ArrayBuffer()
[info] all-platforms/compile:unmanaged-jars
[info]  ArrayBuffer()
[success] Total time: 0 s, completed May 27, 2014 11:45:47 PM

Place the jar you want to have included in classpath:

sandbox/so/my-game
▶ mkdir -p android/lib/

sandbox/so/my-game
▶ touch android/lib/my-shiny-android-library.jar

sandbox/so/my-game
▶ ANDROID_SDK_HOME=~/apps/android-sdk-mac_x86 xsbt
[info] Loading project definition from /Users/jacek/sandbox/so/my-game/project
[info] Set current project to all-platforms (in build file:/Users/jacek/sandbox/so/my-game/)
> show unmanaged-jars
[info] common/compile:unmanaged-jars
[info]  ArrayBuffer()
[info] desktop/compile:unmanaged-jars
[info]  ArrayBuffer()
[info] android/compile:unmanaged-jars
[info]  ArrayBuffer(Attributed(/Users/jacek/sandbox/so/my-game/android/lib/my-shiny-android-library.jar), Attributed(/Users/jacek/apps/android-sdk-mac_x86/platforms/android-19/android.jar))
[info] ios/compile:unmanaged-jars
[info]  ArrayBuffer()
[info] all-platforms/compile:unmanaged-jars
[info]  ArrayBuffer()
[success] Total time: 0 s, completed May 27, 2014 11:54:40 PM

What's interesting is that the setting libraryJarPath is available as library-jary-path (note the y in the name!):

val libraryJarPath = SettingKey[File]("library-jary-path")

To query for the value you should use the library-jary-path setting name in sbt shell:

> show library-jary-path
[info] /Users/jacek/apps/android-sdk-mac_x86/platforms/android-19/android.jar

To see what the following syntax is doing...

unmanagedJars in Compile <+= (libraryJarPath) (p => Attributed.blank(p)) map(x => x)

...you may want to use consoleProject that lets you work with the build definition on command line:

> console-project
[info] Starting scala interpreter...
[info]
import sbt._
import Process._
import Keys._
import _root_.sbtandroid.AndroidPlugin._
import _root_.sbtrobovm.RobovmPlugin._
import LibgdxBuild._
import currentState._
import extracted._
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_55).
Type in expressions to have them evaluated.
Type :help for more information.

scala> libraryJarPath
res0: sbt.SettingKey[sbt.package.File] = sbt.SettingKey$$anon$4@4db9c8c3

scala> libraryJarPath(p => Attributed.blank(p))
res1: sbt.Project.Initialize[sbt.Attributed[sbt.package.File]] = sbt.Init$GetValue@2ed40452

scala> libraryJarPath.apply(p => Attributed.blank(p))
res2: sbt.Project.Initialize[sbt.Attributed[sbt.package.File]] = sbt.Init$GetValue@14e6f49b

scala> libraryJarPath.apply(p => Attributed.blank(p)) map (x => x)
res3: sbt.Project.Initialize[sbt.Task[sbt.Attributed[sbt.package.File]]] = sbt.Init$GetValue@48a4f61e

scala> libraryJarPath.apply(p => Attributed.blank(p)) map identity
res4: sbt.Project.Initialize[sbt.Task[sbt.Attributed[sbt.package.File]]] = sbt.Init$GetValue@526c8b39

The transformed value, after apply and map , is appended, <+= , to the unmanagedJars setting in Compile configuration scope.

Read Console Project for more details on how to work with the different settings and get their values.

scala> get(libraryJarPath in Compile in android)
res5: sbt.package.File = /Users/jacek/apps/android-sdk-mac_x86/platforms/android-19/android.jar

ps I wish I knew how to get the value of the transformed setting res4 above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM