【Android开发学Flutter】微信登录

微信没有提供官方的Flutter插件,所以找了个比较多人用的微信插件:fluwx,项目只需要微信授权登录,还有其他功能大家可以试试。

首先需要在 微信开放平台 注册成为开发者并创建应用,审核通过后得到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) {
  //监听授权登录回调
  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 一直下载不成功,最后还是手动下载,复制到./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 也是一直下载失败,本来想着跟上面同样的方法处理,后面发现不行,会出现重复类错误:
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

删除后重新编译就下载成功了 - - !。

  • 微信授权登录包名和签名要跟开放平台注册的一致,为了方便调试,把AS默认的 debug 签名改成注册时的签名,在app/build.gradle 里设置签名配置:
android {
    ...
    signingConfigs {//签名配置
        release {//发布版签名配置
            storeFile file("your.jks")//密钥文件路径
            storePassword "psw"//密钥文件密码
            keyAlias "alias"//key别名
            keyPassword "psw"//key密码
        }
        debug {//debug版签名配置
            storeFile file("your.jks")
            storePassword "psw"
            keyAlias "alias"
            keyPassword "psw"
        }
    }
    ...
}

然后就可以直接编译,调起微信登录了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今日推荐 经常阅读博客是个好习惯推荐鸿洋CSDN的博客 微信支付相关文章 详细介绍Android开发集成微信支付(...
    MrQ_Android阅读 6,211评论 9 52
  • 闲暇之余,为了打发这种无聊的感觉,随手找了一部电影,看过之后内心久久不能平复,因为自己刚刚经历了片中女主角安迪的一...
    空灵飘飘阅读 553评论 0 0
  • 大一结束了,同学们都陆陆续续的回家了,何小艺独自一人喝着酒拿着手机在编辑短信,好像在哭,他喜欢陈瑜已经一年了...
    程老板阅读 434评论 0 1
  • 速写蹲姿讲解 ▬ ▬ ▬ ▬ 蹲姿没有站姿舒展, 没有坐姿平稳, 四肢因挤压的关系遮挡较严重, 非常规状态下的...
    美术生小助手阅读 1,151评论 0 0
  • 前几天我接到了一位顾客,这个顾客进来之后看了看裤子然后接着看了看毛衣,我便上前搭话问道“您好请问您是需要件...
    高一凡阅读 188评论 0 0