@@ -55,7 +55,7 @@ Development inside Xcode with the Oracle JVM is a little more complicated. It se
55
55
that JNI_CreateJavaVM generates a SIGSEGV internally as part of normal operation
56
56
which is trapped using a signal handler so it can proceed on the command line.
57
57
Unfortunately, this is caught by Xcode debugger lldb and it suspends and will not
58
- continue until you enter" pr h -s false SIGSEGV" into the debug console each time
58
+ continue until you enter` pr h -s false SIGSEGV ` into the debug console each time
59
59
you run the program. The alternative is to not use the debugger at all in your scheme.
60
60
61
61
Perversely, with AWT and Swing on macOS the JVM needs to be created on the main thread
@@ -78,44 +78,36 @@ the jre. Use the examples. directory to build using the following commands:
78
78
swift build -Xlinker -L$JVM_LIBRARY_PATH -Xlinker -rpath -Xlinker$JVM_LIBRARY_PATH -Xlinker -ljvm
79
79
```
80
80
81
- Builds on Linux need to be made with the latest preview 6. The swing source in
81
+ The swing source in
82
82
"examples/Sources" shows how to receive events and subclass a Java class to have certain
83
83
methods such as java.awt.Canvas.paint() be implemented in Swift. More on this later.
84
84
85
- ###Android
85
+ ###Android Development
86
86
87
87
For Android, consult the modified versions of the swift-android-samples and the associated
88
- gradle build system pluginswift-android-gradle from the[ original Androidport ] ( https://github.com/SwiftAndroid ) .
89
- Thisrequires a Ubuntu 15 system orVM, a Lollipop (api 21) or better device and a Swift-3.0
90
- toolchain built with Android support. Instructions for this process are available in the
88
+ gradle build system plugin from the[ AndroidToochain ] ( https://github.com/SwiftJava/android_toolchain ) .
89
+ Thisrun on macOS orUbuntu 16.04, and requires a Lollipop (api 21) or better device.
90
+ More detaills and context are available in the
91
91
[ Swift README for Android] ( https://github.com/apple/swift/blob/master/docs/Android.md )
92
92
and[ this comprehensive tutorial] ( https://medium.com/@ephemer/how-we-put-an-app-in-the-android-play-store-using-swift-67bd99573e3c )
93
93
but hopefully the scripts in the modified gradle plugin take most of the pain out of it.
94
94
95
- Once you have a toolchain you should be able to type the following commands:
95
+ Once you have a toolchainand run its setup.sh script you should be able to type the following commands:
96
96
97
97
``` Shell
98
- cd swift-android-gradle
99
- ./gradlew install
100
- ```
101
-
102
- This install of gradle plugin will tell you which environment variables need to be set up.
103
- Now, connect the Android phone and type:
104
-
105
- ``` Shell
106
- cd ../swift-android-samples/swifthello
98
+ cd swift-android-samples/swifthello
107
99
./gradlew installDebug
108
100
```
109
101
110
- For a new application define two Java interfaces, one for messaging from Java to Swift
102
+ For a new application, define two Java interfaces, one for messaging from Java to Swift
111
103
with it's name ending in "Listener" and one for messaging back into Java from Swift.
112
104
You then use ./genswift.sh from this project to generate the Swift binding code:
113
105
114
106
``` Shell
115
107
./genswift.sh your.package your.jar
116
108
```
117
109
118
- This generates Swift classes and a third Java source src/org/genie /your_package/YourAppProxy.java
110
+ This generates Swift classes and a third Java source src/org/swiftjava /your_package/YourAppProxy.java
119
111
that also needs to be included in your project. Consult the script genhello.sh and project
120
112
"swift-android-samples/swifthello" for details. The source "swift-android-samples/swifthello/src/main/swift/Sources/main.swift"
121
113
shows how to set this up with a native method called from the main activity.
@@ -164,7 +156,7 @@ provide a Swift implementation of the "run()" method callable from Java.
164
156
This approach is also taken for processing events and all interfaces with names
165
157
ending in "Listener", "Manager" or "Handler" also have "Base" classes generated
166
158
for subclassing along with Java Proxy classes. On macOS and Linux these classes
167
- are compiled into a jar file~ /genie .jar using the genjar.sh script for this to work.
159
+ are compiled into a jar file~ /swiftjava .jar using the genjar.sh script for this to work.
168
160
169
161
``` Swift
170
162
class MyActionListener :ActionListenerBase {
@@ -176,6 +168,11 @@ are compiled into a jar file ~/genie.jar using the genjar.sh script for this to
176
168
quitButton.addActionListener (MyActionListener ());
177
169
```
178
170
171
+ Any interface/protocol from a Java interface can be added to a class to enable it to
172
+ be passed to Java provide it's name ends in` Listener ` . The implementation is a little
173
+ complex but does not have an appreciable overhead. structs can also be made accesable
174
+ to Java in this way but the object will no be live i.e. a coy of the struct will be taken.
175
+
179
176
Some event processing is also done by subclassing concrete classes that have names
180
177
ending in "Adapter". Slightly modified Swift "Base" classes and Java Proxies are also
181
178
generated for this. Other classes have methods that are intended to be responsibility
@@ -190,17 +187,19 @@ javaObject to your classes' javaObject. Due to the use of generics you'll
190
187
also be prompted to provide a null implementation of the "required" initialiser.
191
188
192
189
``` Swift
190
+ class MyCanvas :CanvasBase {
191
+
193
192
init (imageProducer :ImageProducer) {
194
193
super .init (javaObject :nil )
195
- CanvasBase ().withJavaObject {
196
- self .javaObject = $0
197
- }
194
+ inherit (CanvasBase ())
198
195
image= createImage (imageProducer)
199
196
}
200
197
201
198
required init (javaObject : jobject? ) {
202
199
fatalError (" init(javaObject:) has not been implemented" )
203
200
}
201
+
202
+ ...
204
203
```
205
204
206
205
Consult the Swing examples codefor further details.
@@ -225,5 +224,5 @@ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TOR
225
224
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
226
225
227
226
This License does not apply to the code generated from the Apple distribution of the Java VM
228
- which are provided under the provisions of "Fair Use"but your use is ultimately subject
227
+ which are provided under the provisions of" Fair Use" and your useis ultimately subject
229
228
to the original License Agreement.