南寧網(wǎng)站建設(shè)代理婚戀網(wǎng)站排名前十名
?了解如何使現(xiàn)有的 Android 應(yīng)用程序跨平臺(tái),以便它在 Android 和 iOS 上都能運(yùn)行。您將能夠在一個(gè)位置編寫代碼并針對(duì) Android 和 iOS 進(jìn)行測(cè)試一次。
本教程使用一個(gè)示例 Android 應(yīng)用程序,其中包含用于輸入用戶名和密碼的單個(gè)屏幕。憑證經(jīng)過驗(yàn)證并保存到“內(nèi)存”數(shù)據(jù)庫。
如果您不熟悉 Kotlin Multiplatform,請(qǐng)先了解如何設(shè)置環(huán)境并從頭開始創(chuàng)建跨平臺(tái)應(yīng)用程序。
Window開發(fā)環(huán)境
編譯Ios環(huán)境需要mac xcode,如果您是window
需要安裝虛擬機(jī),虛擬機(jī)有許多種,這里僅介紹
VMware安裝macOS虛擬機(jī)詳細(xì)教程https://www.overwall.info/168.html
vmware虛擬機(jī)安裝macOS視頻教程https://www.bilibili.com/video/BV1PtynYHE13/?spm_id_from=333.337.search-card.all.click&vd_source=36df3adcf294146e4c45aa1b10354537
準(zhǔn)備開發(fā)環(huán)境
-
安裝所有必要的工具并將它們更新到最新版本
https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-setup.html
您需要一臺(tái)裝有 macOS 的 Mac 才能完成本教程中的某些步驟,其中包括編寫特定于 iOS 的代碼和運(yùn)行 iOS 應(yīng)用程序。這些步驟不能在其他操作系統(tǒng)上執(zhí)行,例如 Microsoft Windows。這是由于 Apple 的要求。?
讓你的跨平臺(tái)應(yīng)用在 iOS 上運(yùn)行
一旦你使安卓應(yīng)用具備跨平臺(tái)特性,就可以創(chuàng)建一個(gè) iOS 應(yīng)用,并復(fù)用其中共享的業(yè)務(wù)邏輯。
1.在 Xcode 中創(chuàng)建一個(gè) iOS 項(xiàng)目。
2.將框架連接到你的 iOS 項(xiàng)目。
3.從 Swift 使用共享模塊。
在 Xcode 中創(chuàng)建 iOS 項(xiàng)目
1.在 Xcode 中,點(diǎn)擊 “文件”|“新建”|“項(xiàng)目”。
2.選擇一個(gè) iOS 應(yīng)用模板,然后點(diǎn)擊 “下一步”。
3.將產(chǎn)品名稱設(shè)定為 “simpleLoginIOS” ,然后點(diǎn)擊 “下一步”。
4.對(duì)于項(xiàng)目的存儲(chǔ)位置,選擇存放你跨平臺(tái)應(yīng)用的目錄,比如 “kmp - integration - sample”。
在安卓開發(fā)工具(Android Studio)中,你會(huì)得到以下結(jié)構(gòu):
為了與跨平臺(tái)項(xiàng)目的其他頂級(jí)目錄保持一致,你可以將 “simpleLoginIOS” 目錄重命名為 “iosApp”。要完成這一操作,需先關(guān)閉 Xcode,然后把 “simpleLoginIOS” 目錄重命名為 “iosApp”。要是在 Xcode 打開的狀態(tài)下重命名該文件夾,你將會(huì)收到警告,而且還有可能損壞項(xiàng)目。
將框架連接到你的 iOS 項(xiàng)目
一旦你有了框架,就可以手動(dòng)將其連接到你的 iOS 項(xiàng)目中。
另一種方法是通過 CocoaPods,來配置集成,但這種集成方式不在本教程的講解范圍內(nèi)。
手動(dòng)將你的框架連接到 iOS 項(xiàng)目:
1.在 Xcode 中,雙擊項(xiàng)目名稱打開 iOS 項(xiàng)目設(shè)置。
2.在項(xiàng)目設(shè)置的 Build Phases “構(gòu)建階段” 選項(xiàng)卡上,點(diǎn)擊 “+” 并添加 New Run Script Phase “新的運(yùn)行腳本階段“。
3.添加以下腳本:
4.將運(yùn)行腳本階段移動(dòng)到編譯源代碼階段之前
5.在“構(gòu)建設(shè)置”選項(xiàng)卡中,在“構(gòu)建選項(xiàng)”下禁用“用戶腳本沙盒”。
這可能需要重新啟動(dòng)你的 Gradle 守護(hù)進(jìn)程,如果你在未先禁用沙盒功能的情況下構(gòu)建了 iOS 項(xiàng)目。請(qǐng)停止可能已被置于沙盒中的 Gradle 守護(hù)進(jìn)程。
./gradlew --stop
6.在 Xcode 中構(gòu)建項(xiàng)目。如果一切設(shè)置正確,項(xiàng)目將成功構(gòu)建。
如果你有與默認(rèn)的 “調(diào)試(Debug)” 或 “發(fā)布(Release)” 不同的自定義構(gòu)建配置,在 “構(gòu)建設(shè)置” 選項(xiàng)卡中,在 “用戶自定義” 下添加 “KOTLIN_FRAMEWORK_BUILD_TYPE” 設(shè)置,并將其設(shè)為 “調(diào)試(Debug)” 或 “發(fā)布(Release)”。
使用 Swift 中的共享模塊
1.在 Xcode 中,打開 ContentView.swift 文件并導(dǎo)入共享模塊:
import shared
2.為檢查連接是否正常,使用跨平臺(tái)應(yīng)用共享模塊中的?greet()
?函數(shù)。
import SwiftUI
import sharedstruct ContentView: View {var body: some View {Text(Greeting().greet()).padding()}
}
3.從 Xcode 運(yùn)行該應(yīng)用程序以查看結(jié)果:
4.在 ContentView.swift 文件中,編寫使用共享模塊中的數(shù)據(jù)并渲染應(yīng)用程序用戶界面的代碼
import SwiftUI
import sharedstruct ContentView: View {@State private var username: String = ""@State private var password: String = ""@ObservedObject var viewModel: ContentView.ViewModelvar body: some View {VStack(spacing: 15.0) {ValidatedTextField(titleKey: "Username", secured: false, text: $username, errorMessage: viewModel.formState.usernameError, onChange: {viewModel.loginDataChanged(username: username, password: password)})ValidatedTextField(titleKey: "Password", secured: true, text: $password, errorMessage: viewModel.formState.passwordError, onChange: {viewModel.loginDataChanged(username: username, password: password)})Button("Login") {viewModel.login(username: username, password: password)}.disabled(!viewModel.formState.isDataValid || (username.isEmpty && password.isEmpty))}.padding(.all)}
}struct ValidatedTextField: View {let titleKey: Stringlet secured: Bool@Binding var text: Stringlet errorMessage: String?let onChange: () -> ()@ViewBuilder var textField: some View {if secured {SecureField(titleKey, text: $text)} else {TextField(titleKey, text: $text)}}var body: some View {ZStack {textField.textFieldStyle(RoundedBorderTextFieldStyle()).autocapitalization(.none).onChange(of: text) { _ inonChange()}if let errorMessage = errorMessage {HStack {Spacer()FieldTextErrorHint(error: errorMessage)}.padding(.horizontal, 5)}}}
}struct FieldTextErrorHint: View {let error: String@State private var showingAlert = falsevar body: some View {Button(action: { self.showingAlert = true }) {Image(systemName: "exclamationmark.triangle.fill").foregroundColor(.red)}.alert(isPresented: $showingAlert) {Alert(title: Text("Error"), message: Text(error), dismissButton: .default(Text("Got it!")))}}
}extension ContentView {struct LoginFormState {let usernameError: String?let passwordError: String?var isDataValid: Bool {get { return usernameError == nil && passwordError == nil }}}class ViewModel: ObservableObject {@Published var formState = LoginFormState(usernameError: nil, passwordError: nil)let loginValidator: LoginDataValidatorlet loginRepository: LoginRepositoryinit(loginRepository: LoginRepository, loginValidator: LoginDataValidator) {self.loginRepository = loginRepositoryself.loginValidator = loginValidator}func login(username: String, password: String) {if let result = loginRepository.login(username: username, password: password) as? ResultSuccess {print("Successful login. Welcome, \(result.data.displayName)")} else {print("Error while logging in")}}func loginDataChanged(username: String, password: String) {formState = LoginFormState(usernameError: (loginValidator.checkUsername(username: username) as? LoginDataValidator.ResultError)?.message,passwordError: (loginValidator.checkPassword(password: password) as? LoginDataValidator.ResultError)?.message)}}
}
5.在 simpleLoginIOSApp.swift 文件中,導(dǎo)入共享模塊并為 ContentView () 函數(shù)指定參數(shù)。
import SwiftUI
import shared@main
struct SimpleLoginIOSApp: App {var body: some Scene {WindowGroup {ContentView(viewModel: .init(loginRepository: LoginRepository(dataSource: LoginDataSource()), loginValidator: LoginDataValidator()))}}
}
6.運(yùn)行 Xcode 項(xiàng)目,你會(huì)看到 iOS 應(yīng)用顯示出登錄表單。在用戶名處輸入 “Jane”,密碼處輸入 “password”。應(yīng)用會(huì)使用共享代碼對(duì)輸入內(nèi)容進(jìn)行驗(yàn)證。
享受成果吧 —— 只需更新一次邏輯。
現(xiàn)在,你的應(yīng)用程序?qū)崿F(xiàn)了跨平臺(tái)。你只需在一處更新業(yè)務(wù)邏輯,就能在安卓和 iOS 系統(tǒng)上看到相應(yīng)的變化。
1.在 Android Studio 中,更改用戶密碼的驗(yàn)證邏輯:“password” 不應(yīng)該是一個(gè)有效的選項(xiàng)。為此,更新 LoginDataValidator 類的 checkPassword () 函數(shù):
package com.jetbrains.simplelogin.shared.dataclass LoginDataValidator {
//...fun checkPassword(password: String): Result {return when {password.length < 5 -> Result.Error("Password must be >5 characters")password.lowercase() == "password" -> Result.Error("Password shouldn't be \"password\"")else -> Result.Success}}
//...
}
2.在 Android Studio 中,為 iOS 應(yīng)用添加運(yùn)行配置:
在主菜單中選擇 “運(yùn)行(Run)| 編輯配置(Edit configurations)”。
要添加新配置,點(diǎn)擊加號(hào),然后選擇 “iOS 應(yīng)用程序(iOS Application)”。
將該配置命名為 “SimpleLoginIOS”。
在 “Xcode 項(xiàng)目(Xcode project)” 文件字段中,選擇 simpleLoginIOS.xcodeproj 文件的位置。
在 “執(zhí)行目標(biāo)(Execution target)” 列表中選擇一個(gè)模擬環(huán)境,然后點(diǎn)擊 “確定(OK)”
3.從 Android Studio 運(yùn)行 iOS 和安卓應(yīng)用,查看相應(yīng)變化。
你可以查看本教程的最終代碼。最終代碼https://github.com/Kotlin/kmp-integration-sample/tree/final.
還有什么別的可以分享呢?
你已經(jīng)共享了應(yīng)用程序的業(yè)務(wù)邏輯,但你也可以決定共享應(yīng)用程序的其他層。例如,ViewModel 類的代碼在安卓和 iOS 應(yīng)用中幾乎相同,如果你的移動(dòng)應(yīng)用需要有相同的表示層,那么你可以共享這部分代碼。
接下來做什么?
一旦你讓安卓應(yīng)用實(shí)現(xiàn)跨平臺(tái),就可以繼續(xù)進(jìn)行以下操作:
添加對(duì)多平臺(tái)庫的依賴
添加安卓依賴
添加 iOS 依賴
你還可以查看社區(qū)資源:
視頻:如何將安卓項(xiàng)目遷移至 Kotlin 多平臺(tái)
-
Add dependencies on multiplatform libraries
-
Add Android dependencies
-
Add iOS dependencies
視頻:讓 Kotlin JVM 代碼適配 Kotlin 多平臺(tái)的三種方法
如何在Android應(yīng)用中添加對(duì)多平臺(tái)庫的依賴?
有哪些常見的Kotlin多平臺(tái)庫可以添加到Android應(yīng)用中?
社區(qū)資源中還有哪些關(guān)于Kotlin多平臺(tái)的內(nèi)容值得參考?
-
Video: How to migrate an Android project to Kotlin Multiplatform
-
Video: 3 ways to get your Kotlin JVM code ready for Kotlin Multiplatform