【Android開發(fā)學(xué)Flutter】微信登錄

微信沒有提供官方的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 用到 kotlinLuban,編譯的時候遇到一些問題:

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)起微信登錄了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今日推薦 經(jīng)常閱讀博客是個好習(xí)慣推薦鴻洋CSDN的博客 微信支付相關(guān)文章 詳細介紹Android開發(fā)集成微信支付(...
    MrQ_Android閱讀 6,281評論 9 52
  • 閑暇之余,為了打發(fā)這種無聊的感覺,隨手找了一部電影,看過之后內(nèi)心久久不能平復(fù),因為自己剛剛經(jīng)歷了片中女主角安迪的一...
    空靈飄飄閱讀 606評論 0 0
  • 大一結(jié)束了,同學(xué)們都陸陸續(xù)續(xù)的回家了,何小藝獨自一人喝著酒拿著手機在編輯短信,好像在哭,他喜歡陳瑜已經(jīng)一年了...
    程老板閱讀 471評論 0 1
  • 速寫蹲姿講解 ? ? ? ? 蹲姿沒有站姿舒展, 沒有坐姿平穩(wěn), 四肢因擠壓的關(guān)系遮擋較嚴重, 非常規(guī)狀態(tài)下的...
    美術(shù)生小助手閱讀 1,262評論 0 0
  • 前幾天我接到了一位顧客,這個顧客進來之后看了看褲子然后接著看了看毛衣,我便上前搭話問道“您好請問您是需要件...
    高一凡閱讀 195評論 0 0

友情鏈接更多精彩內(nèi)容