微信沒有提供官方的Flutter插件,所以找了個比較多人用的微信插件:fluwx,項目只需要微信授權(quán)登錄,還有其他功能大家可以試試。
首先需要在 微信開放平臺 注冊成為開發(fā)者并創(chuàng)建應(yīng)用,審核通過后得到AppId。
引入
在pubspec.yaml里添加:
dependencies:
fluwx: ^1.0.4
添加完成后 flutter pub get
初始化
import 'package:fluwx/fluwx.dart' as fluwx;
fluwx.register(appId:"wxd930ea5d5a258f4f");
fluwx.sendAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test");
fluwx.responseFromAuth.listen((response) {
//監(jiān)聽授權(quán)登錄回調(diào)
print("code: " + response.code);
});
-
appId:微信開放平臺申請的appid -
scope:為固定值 -
state:任意自定義字符串
獲取code后可以傳給后臺獲取access_token。
遇到的問題
fluwx 用到 kotlin 和 Luban,編譯的時候遇到一些問題:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not download kotlin-compiler-embeddable.jar (org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.40)
> Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.3.40/kotlin-compiler-embeddable-1.3.40.jar'.
> Could not GET 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.3.40/kotlin-compiler-embeddable-1.3.40.jar'.
> Read timed out
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
- kotlin-compiler-embeddable-1.3.40.jar 一直下載不成功,最后還是手動下載,復(fù)制到
./gradle/caches/modules-2/file-2.1/org.jetbrains.kotlin/kotlin-compiler-embeddable/1.3.40(版本號)/29418ab24831ca819005f20aa32862c26938f284(AS生成的)目錄下,重啟AS,就可以了。
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:preDebugBuild'.
> Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
> Could not determine artifacts for top.zibin:Luban:1.1.8
> Could not get resource 'https://jcenter.bintray.com/top/zibin/Luban/1.1.8/Luban-1.1.8.aar'.
> Could not HEAD 'https://jcenter.bintray.com/top/zibin/Luban/1.1.8/Luban-1.1.8.aar'.
> Read timed out
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 3s
Finished with error: Gradle task assembleDebug failed with exit code 1
-
Luban-1.1.8.aar也是一直下載失敗,本來想著跟上面同樣的方法處理,后面發(fā)現(xiàn)不行,會出現(xiàn)重復(fù)類錯誤:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 項目目錄/build/app/intermediates/transforms/dexBuilder/debug/xx.jar,
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
Program type already present: top.zibin.luban.BuildConfig
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 58s
Finished with error: Gradle task assembleDebug failed with exit code 1
刪除后重新編譯就下載成功了 - - !。
- 微信授權(quán)登錄包名和簽名要跟開放平臺注冊的一致,為了方便調(diào)試,把AS默認的
debug簽名改成注冊時的簽名,在app/build.gradle里設(shè)置簽名配置:
android {
...
signingConfigs {//簽名配置
release {//發(fā)布版簽名配置
storeFile file("your.jks")//密鑰文件路徑
storePassword "psw"http://密鑰文件密碼
keyAlias "alias"http://key別名
keyPassword "psw"http://key密碼
}
debug {//debug版簽名配置
storeFile file("your.jks")
storePassword "psw"
keyAlias "alias"
keyPassword "psw"
}
}
...
}
然后就可以直接編譯,調(diào)起微信登錄了。