This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from vgorloff/develop
RC 1.0.5
- Loading branch information
Showing
84 changed files
with
1,111 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
/.swiftpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.