Swift で書いたコードを Java にコンパイルして Android で動作させられる Silver を試してみた。
環境
- Mac OS X 10.11.5 (英語)
- RemObjects Fire 8.3.93.1987
- Oracle JDK 1.8.0_91
Silver の主張
- 2016 年後半に Swift 3.0 をサポート予定
- Elements 8.3.91 は Swift 2.2 の大部分をサポートしてる
- Elements 8.2 は Swift 2.1 をサポートしてる
手順
-
Fire with Elemnts for Mac をダウンロード
- Fire は RemObjects 社の IDE
- Elements は RemObjects 社のコンパイラ
- Swift は Free だが C# と独自言語 Oxygene は有料
- Windows では Fire ではなく Visual Studio を使う
-
Start a new Project で Platform を .NET から Java へ変更、テンプレートはまず Console Application を選んでみる
-
生成されたファイル構成はシンプル
- 下 3 つの sln は Visual Studio 用で、実質 2 個
$ find SilverConsoleApp
SilverConsoleApp
SilverConsoleApp/Program.swift
SilverConsoleApp/SilverConsoleApp.elements
SilverConsoleApp/SilverConsoleApp.sln
SilverConsoleApp/SilverConsoleApp.sln.fire.cache
SilverConsoleApp/SilverConsoleApp.sln.fire.user
1. プロジェクトファイルの構造も綺麗な感じ
```
$ cat SilverConsoleApp/SilverConsoleApp.elements
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
<PropertyGroup>
<ProductVersion>3.5</ProductVersion>
<ProjectGuid>D5543A07-76CC-45B6-9AB2-684CC4594AA4</ProjectGuid>
<OutputType>exe</OutputType>
<Configuration Condition="'$(Configuration)' == ''">Release</Configuration>
<AllowLegacyCreate>False</AllowLegacyCreate>
<AllowLegacyOutParams>False</AllowLegacyOutParams>
<MinFrameworkVersionRequired>4.0</MinFrameworkVersionRequired>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<Optimize>false</Optimize>
<OutputPath>.\bin\Debug</OutputPath>
<DefineConstants>DEBUG;TRACE;</DefineConstants>
<GenerateDebugInfo>True</GenerateDebugInfo>
<EnableAsserts>True</EnableAsserts>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<CaptureConsoleOutput>False</CaptureConsoleOutput>
<StartMode>Project</StartMode>
<RegisterForComInterop>False</RegisterForComInterop>
<CpuType>anycpu</CpuType>
<RuntimeVersion>v25</RuntimeVersion>
<XmlDoc>False</XmlDoc>
<XmlDocWarningLevel>WarningOnPublicMembers</XmlDocWarningLevel>
<WarnOnCaseMismatch>True</WarnOnCaseMismatch>
<EnableUnmanagedDebugging>False</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<Optimize>true</Optimize>
<OutputPath>.\bin\Release</OutputPath>
<GenerateDebugInfo>False</GenerateDebugInfo>
<EnableAsserts>False</EnableAsserts>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<CaptureConsoleOutput>False</CaptureConsoleOutput>
<StartMode>Project</StartMode>
<RegisterForComInterop>False</RegisterForComInterop>
<CpuType>anycpu</CpuType>
<RuntimeVersion>v25</RuntimeVersion>
<XmlDoc>False</XmlDoc>
<XmlDocWarningLevel>WarningOnPublicMembers</XmlDocWarningLevel>
<WarnOnCaseMismatch>True</WarnOnCaseMismatch>
<EnableUnmanagedDebugging>False</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Reference Include="rt"/>
<Reference Include="swift">
<Private>True</Private>
</Reference>
<Reference Include="cooper">
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.swift"/>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Elements\RemObjects.Elements.Cooper.targets"/>
-
しかし、エディタで日本語入力ができない!
-
Run してみたら java.security.PrivilegedActionException が起きて動かない
-
jar が出来ているので、直接実行してみるとちゃんと動くので IDE の問題っぽい
-
$ find SilverConsoleApp/bin
SilverConsoleApp/bin
SilverConsoleApp/bin/Debug
SilverConsoleApp/bin/Debug/cooper.jar
SilverConsoleApp/bin/Debug/SilverConsoleApp.jar
SilverConsoleApp/bin/Debug/swift.jar
$ java -jar SilverConsoleApp/bin/Debug/SilverConsoleApp.jar
The 魔法 happens here.
1. お供の swift.jar は 109KB
2. お供の cooper.jar は 83KB
1. jar には class ファイルが 1 個含まれている
```
$ mkdir jar
$ cd jar
$ tar xzvf ../SilverConsoleApp/bin/Debug/SilverConsoleApp.jar
x silverconsoleapp/
x silverconsoleapp/__Global.class
x DEBUG_INFO/
x DEBUG_INFO/silverconsoleapp.__Global.di
x METADATA/
x METADATA/.metadata.fx
x META-INF/
x META-INF/MANIFEST.MF
1. swift.jar と cooper.jar に依存してる
```
$ more META-INF/MANIFEST.MF
Manifest-Version: 1.0
Implementation-Vendor: RemObjects Software
Class-Path: swift.jar cooper.jar
Main-Class: silverconsoleapp.__Global
Name: silverconsoleapp/__Global.class
1. jd-gui-1.4.0.jar でデコンパイル
```
package silverconsoleapp;
public abstract class __Global {
public static void main(String[] __args) {
swift.__Global.$$setArgV((String[])__args);swift.__Global.print__separator__terminator("The 魔法 happens here.", " ", null);
}
}
1. swift API を Java で実装してて、それを呼び出してる感じ
-
そのまま Run すると generic Android device では deploy 出来ないので CrossBox で device を選択しろと出てしまう
-
と思ったら、タイトルバー真下のプルダウンで USB 接続済みのデバイスを選択できた
- 何回かデバイス側でデバッグ許可をし直したら動いた
参考リンク