【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"
        }
    }
    ...
}

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

你可能感兴趣的:(【Android开发学Flutter】微信登录)