Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from vgorloff/develop
Browse files Browse the repository at this point in the history
RC 1.0.5
  • Loading branch information
vgorloff committed Aug 10, 2019
2 parents ac4944e + 1f8c1d9 commit 85e7da9
Show file tree
Hide file tree
Showing 84 changed files with 1,111 additions and 588 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
local.properties.yml
/*.code-workspace
/.build
xcuserdata/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ task spm(type:Exec) {
// project.logger.lifecycle("Building Swift sources...")
workingDir "$rootDir"
executable = '/usr/bin/env'
args = ["ruby", "-r", "./Builder.rb", "-e", "Builder.perform('')"]
args = ["ruby", "-r", "../Scripts/Builder.rb", "-e", "Builder.perform('')"]
}

afterEvaluate {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package="com.home.helloNDK">

<application
android:extractNativeLibs="false"
android:allowBackup="true"
android:extractNativeLibs="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand All @@ -18,5 +18,7 @@
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.system.ErrnoException;
import android.system.Os;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
Expand All @@ -11,6 +13,11 @@ public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
try {
Os.setenv("CFFIXED_USER_HOME", this.getFilesDir().getPath(), true);
} catch (ErrnoException e) {
e.printStackTrace();
}
super.onCreate(savedInstanceState);
final Button myButton = new Button(this);
myButton.setText("Press me");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class SwiftLib {

static {
System.loadLibrary("HelloMessages");
System.loadLibrary("HelloJNICore");
}

public native int sayHello();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.4] - 2019-08-04
## [1.0.5] - 2019-08-09
### Changed
- Sample projects moved to Toolchain repository.
###Added
- Sample iOS Project which uses same Swift Package.

## [1.0.4] - 2019-08-08
### Changed
* Demo application builder now uses Swift Package Manager.

Expand Down
2 changes: 2 additions & 0 deletions Package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
/.swiftpm
17 changes: 17 additions & 0 deletions Package/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// swift-tools-version:5.0

import PackageDescription

let package = Package(
name: "HelloJNI",
products: [
// See: https://theswiftdev.com/2019/01/14/all-about-the-swift-package-manager-and-the-swift-toolchain/
.library(name: "HelloJNICore", type: .dynamic, targets: ["HelloJNICore"]),
.library(name: "NDKLog", targets: ["NDKLog"])
],
targets: [
.target(name: "HelloJNICore", dependencies: ["NDKLog"]),
.target(name: "NDKLog", dependencies: ["sysNDKLog"]),
.systemLibrary(name: "sysNDKLog")
]
)
106 changes: 106 additions & 0 deletions Package/Sources/HelloJNICore/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Foundation
import NDKLog
#if os(Android)
import FoundationNetworking
#endif

let tester = URLTester()

@_cdecl("Java_com_home_helloNDK_SwiftLib_sayHello")
public func sayHello() -> Int {
// fatalError()
AndroidLogger.info("SA - SwiftCore: Works!")
probeDispatch()
probeOperation()
probeSerialization()
tester.test()
return Int(Date().timeIntervalSince1970)
}

func probeDispatch() {
let sema = DispatchSemaphore(value: 0)

let queue = DispatchQueue(label: "queueName")
queue.async {
AndroidLogger.info("SA - DispatchQueue: Works!")
sema.signal()
}

if sema.wait(timeout: .now() + 10) == .timedOut {
AndroidLogger.info("SA - DispatchQueue: Timeout.")
}
}

private let opQueue = OperationQueue()

func probeOperation() {
let op = BlockOperation {
AndroidLogger.info("SA - BlockOperation: Works!")
}
opQueue.addOperations([op], waitUntilFinished: true)
}


func probeSerialization() {
let json = ["name": "SA - JSONSerialization/JSONDecoder: Works!"]
do {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
struct Person: Decodable {
let name: String
}
let person = try JSONDecoder().decode(Person.self, from: data)
AndroidLogger.info(person.name)
} catch {
AndroidLogger.info(String(describing: error))
}
}



class URLTester: NSObject {


let config = URLSessionConfiguration.default
lazy var session = URLSession(configuration: config)

override init() {
super.init()
// session.delegate = self
}

func test() {
AndroidLogger.info("SA - URLSession: Seems not Working yet.")
if let url = URL(string: "https://www.google.com") {
let sema2 = DispatchSemaphore(value: 0)
let task = session.dataTask(with: url) { data, response, error in
if let response = response {
AndroidLogger.info("Response: " + String(describing: response))
}
if let error = error {
let nsError = error as NSError
AndroidLogger.info("Error: " + String(describing: nsError))
AndroidLogger.info("Error UserInfo: " + String(describing: nsError.userInfo))
}
if let data = data {
AndroidLogger.info("Data: " + String(describing: data))
}
sema2.signal()
}
task.resume()
if sema2.wait(timeout: .now() + 10) == .timedOut {
AndroidLogger.info("Timeout")
}
AndroidLogger.info("URL Task Completed")
} else {
AndroidLogger.info("bad url")
}
}
}

//extension URLTester: URLSessionDelegate {
//
// func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge,
// completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
// AndroidLogger.info("Challenge: " + String(describing: challenge))
// }
//}
16 changes: 16 additions & 0 deletions Package/Sources/NDKLog/NDKLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ndk.log

public struct AndroidLogger {

@discardableResult
public static func info(_ message: String) -> Int32 {
#if os(Android)
return "ANDROID: \(message)".withCString {
__android_log_print_1($0)
}
#else
print("NOT AN ANDROID: " + message)
return 0
#endif
}
}
9 changes: 9 additions & 0 deletions Package/Sources/sysNDKLog/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ndk [system] {

module log {
header "sysNDKLog.h"
header "/usr/local/ndk/sysroot/usr/include/android/log.h"
export *
link "log"
}
}
5 changes: 5 additions & 0 deletions Package/Sources/sysNDKLog/sysNDKLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "/usr/local/ndk/sysroot/usr/include/android/log.h"

static inline int __android_log_print_1(const char *str) {
return __android_log_print(ANDROID_LOG_INFO, "SwiftAndroid", "%s", str);
}
22 changes: 19 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# Requirements

- Xcode 10.2.1
- Xcode 11
- Android Studio 3.4
- Android NDK 20 (Comes with Android Studio as downloadable package).
- Ruby 2.5 (Comes with macOS)

## Using Sample projects
## Usage

1. Make sure that you have [Swift Android Toolchain](https://github.com/vgorloff/swift-everywhere-toolchain). You can either build it or download [pre-build](https://github.com/vgorloff/swift-everywhere-toolchain/releases) version.

2. Navigate to certain project subfolder and look on Readme.md file in that subfolder.
2. Make sure that file `Android/local.properties` has proper paths (usually paths already set by `Android Studio`):

- sdk.dir - Path to Android SDK (by default similar to: /Users/user/Library/Android/sdk)
- ndk.dir - Path to Android NDK (by default similar to: /Users/user/Library/Android/sdk/ndk-bundle)

3. Copy file `local.properties.yml.template` to `local.properties.yml`. Update file `local.properties.yml` with proper paths:

- swiftToolchain.dir - Path to Swift Toolchain (by default similar to: /Users/user/git/swift-everywhere-toolchain/ToolChain/swift-android-toolchain)

4. Open `iOS/HelloJNI.xcodeproj` in Xcode 11 and run it on Device or iOS Simulator.

5. Open folder `Android` in Android Studio and run it on Device or Android Simulator.

## Links

- Android NDK: Using C/C++ Native Libraries to Write Android Apps: http://bit.ly/2HjXYJk
- Modern Android NDK Tutorial – Jordan Réjaud – Medium: http://bit.ly/2HvrmLO
45 changes: 0 additions & 45 deletions Scripts/ADB.rb

This file was deleted.

Loading

0 comments on commit 85e7da9

Please sign in to comment.