如何将Scala集成到Android核心平台?

发布于 2024-09-12 21:32:06 字数 2415 浏览 12 评论 0原文

我有兴趣将 Scala(或其他一些非 Java JVM 语言)集成到 android 平台中。我并不是指用 Scala 编写 Android 应用程序(我很早就这样做了),而是实际上挂钩了构建 Android 平台源代码树的构建过程。我想这将是一个挂钩到 makefile 等的问题。有人对此有见解吗?

到目前为止我所拥有的: 来自 git://android.git.kernel.org/platform/manifest.git 的平台源代码树以其原始形式构建,以“[下载并构建 Google Android][1]”

  • build/core/combo/scalac 为指导。 mk # 配置 scala 编译器相关变量,包含在 config.mk
  • 中 在 build/core/definitions.mk 中添加了 all-subdir-scala-files 和 all-scala-files-under 的定义
  • 在Definitions.mk 中添加了定义来构建 scala文件,以便它们包含在包中

剩下的:

  • 包含 scala-library.jar
  • 确保对 -bootclasspath 的更改没有破坏任何内容
  • 了解如何处理 scala 类依赖于 java 类的情况,反之亦然
  • 代码主要清理
  • 测试!
  • 弄清楚我所做的更改应该做什么(而不只是将它们发布在这里)

看起来我几乎就在那里!!!

过去的一些笔记

最新:我找到了编译Java源文件的地方!在Definitions.mk中,请参阅“定义transform-java-to-classes.jar”。最新的想法是编写一个将scala转换为类的定义,然后将这些类直接存储在打包的文件中。我将在transform-java-to-classes.jar 中的这一步之前调用transform-scala-to-class。现在将放弃对 eclipse 和 cygwin 的支持,因为它会使代码与变通办法变得混乱,从而增加了我失败的机会。

构建过程由运行 build/core/main.mk 的根 Makefile 开始 build/core/main.mk 包含 build/core/config.mk,其中包含 build/core/combo/javac.mk,其中设置 HOST_JAVAC、TARGET_JAVAC 和 COMMON_JAVAC。 COMMON_JAVAC 是“具有公共参数的 Java 编译器命令”,从它的外观来看,其他两个变量默认情况下会获取这些值,除非在特殊环境(openjdk 或 eclipse)中。 COMMON_JAVAC 不在该文件之外使用。另外两个仅在 build/core/definitions.mk 中使用。

build/core/java_library.mk (包含在 config.mk 中)似乎只与构建 jar 相关。这不在我们关心的范围之内。与 jar 的任何交互都以类文件为先决条件,而类文件则假定我们已经成功构建了 scala 文件。

main.mk 中有关于 java 版本的检查。我们将忽略这些并假设我们的 scala 版本是兼容的。现在(在combo/scalac.mk中)我使用与javac.mk中使用的相同的--target arg。这也许应该存储在一个变量中。

main.mk 还包括 build/core/definitions.mk ,它又定义了一些有用的函数。我们这里关心的是 all-java-files-under 和 all-subdir-java-files。后者用于在Android.mk文件中查找java文件。前者用于后者的实现。我将编写它们的 Scala 等价物。

为了弄清楚构建过程是如何工作的,我现在使用 -n 和其他参数运行 make。我从 stackoverflow 文章“[调试 makefiles 的工具][2]”中得到了这个想法。我也在研究重制的调试。

build/core/{config.mk,Definitions.mk} 让我们了解哪些 make 文件/命令用于做什么。

作为基于每个项目的支持的一种可能的方式,附加代码很可能被添加到项目的 Android.mk 文件中。从 platform/build/core/build-system.html 中,我们读到“Android.mk 是控制给定模块构建的 makefile 片段的标准名称。只有顶层目录应该有一个名为“Makefile”的文件。”您可以创建一个新目标,例如“scala-build”,并在最终 make 之前运行它(make PackageName scala-build)。也许还可以将其偷偷地隐藏在变量赋值中,从而减少显式调用目标的需要。

另一种方法(更加黑客)是劫持用于 javac 的命令。这是在 build/core/combo/javac.mk 中设置的。您项目的 Android.mk 必须在 LOCAL_SRC_FILES 中包含 *.scala 文件以及 *.java 文件。

I am interested in integrating Scala (or some other non-Java JVM-language) into the android platform. I am not referring to writing an android application with Scala, that I did early early on, but actually hooking into the build process that builds the android platform source tree. I imagine this will be a matter of hooking into the makefiles and such. Does anyone have insight into this?

What I have so far:
The platform source treefrom git://android.git.kernel.org/platform/manifest.git built in its virgin form, guided by "[Download and build the Google Android][1]"

  • build/core/combo/scalac.mk # Configures scala compiler related variables, included by config.mk
  • Added definitions in build/core/definitions.mk for an all-subdir-scala-files and an all-scala-files-under
  • Added definition in definitions.mk to build scala files such that they are included in the package

What's left:

  • Include scala-library.jar
  • Ensure changes to -bootclasspath has not broken anything
  • Figure out how to handle case where scala classes depend on java classes and visa versa
  • Major cleanup of code
  • Testing!
  • Figure out what to do (other than just posting them here) with the changes I've made

Looks like I'm almost there!!!

Some notes from the past

Latest: I have found where the Java source files are compiled! In definitions.mk, see 'define transform-java-to-classes.jar'. The latest idea is to write a transform-scala-to-classes definition and then have it store those classes in the directly that gets packaged. I will call transform-scala-to-class right before this step in transform-java-to-classes.jar. Support for eclipse and cygwin will for now be dropped as it clutters up the code with workarounds and therefore increases my chances of failure.

The build process starts out by the root Makefile running build/core/main.mk
build/core/main.mk includes build/core/config.mk which includes build/core/combo/javac.mk which sets HOST_JAVAC, TARGET_JAVAC, and COMMON_JAVAC. COMMON_JAVAC is the "Java compiler command with common arguments," by the look of it the other two variables get these values by default, unless in a special environment (openjdk or eclipse). COMMON_JAVAC is not used outside this file. The other two are only used in build/core/definitions.mk.

build/core/java_library.mk (included by config.mk) seems to only be concerned with building jars. This is out of the scope of us caring. Any interaction with jars presupposes class files which presuppose that we were already successful in building our scala files.

There are checks in main.mk regarding the version of java. We will ignore these and assume that our version of scala is compatible. Right now (in combo/scalac.mk) I am using the same --target arg used in javac.mk. This should perhaps be stored in a variable.

main.mk also includes build/core/definitions.mk which in turns defines some useful functions. The one we care about here is all-java-files-under and all-subdir-java-files. The latter is used in Android.mk files to find java files. The former is used in the implementation of the latter. I will write Scala equivalents of them.

To figure out how the build process works, I am now running make with -n and others. I got this idea from the stackoverflow article "[Tool for debugging makefiles][2]". I am also investigating debugging with remake.

build/core/{config.mk, definitions.mk} gives us light as to which make files/commands are used to do what.

As a possible way of hacking in support on a per project bases, additional code could most likely be added to the project's Android.mk file. From platform/build/core/build-system.html we read "Android.mk is the standard name for the makefile fragments that control the building of a given module. Only the top directory should have a file named "Makefile"." You could create a new target like "scala-build" and run that (make PackageName scala-build) before the final make. One could perhaps also hide it sneakily in a variable assignment, mitigating the need for a target to be called explicitly.

Another way (far far more hackish) is to hijack the command being used for javac. This is set in build/core/combo/javac.mk. Your project's Android.mk will have to include *.scala files in LOCAL_SRC_FILES along with the *.java files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2024-09-19 21:32:06

reddit 上的人说,有一个关于使用 ant 将 Scala 集成到 Android 的教程 这里

Guys on reddit say, there's a tutorial on integration Scala into Android with ant here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文