From fea0f2b6443ffe1683395d828f95b0f54b4cd4f1 Mon Sep 17 00:00:00 2001 From: v7lin Date: Thu, 1 Aug 2019 22:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20Android=20=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=20SDK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 31 +++++++------- android/build.gradle | 9 +++-- android/consumer-proguard-rules.txt | 5 +++ android/src/main/AndroidManifest.xml | 15 ++++--- .../v7lin/fakewechat/FakeWechatPlugin.java | 40 ++++++++++++++++--- example/android/app/build.gradle | 2 +- example/pubspec.yaml | 2 +- pubspec.yaml | 10 ++--- 8 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 android/consumer-proguard-rules.txt diff --git a/.drone.yml b/.drone.yml index 9333f4e..3b46649 100644 --- a/.drone.yml +++ b/.drone.yml @@ -62,10 +62,6 @@ steps: from_secret: CODECOV_TOKEN files: - ./coverage/lcov.info - when: - event: - exclude: - - pull_request - name: publish-check image: v7lin/flutter:1.7.8-hotfix.3-stable @@ -91,9 +87,10 @@ steps: volumes: - name: pub-cache path: /opt/flutter/.pub-cache + environment: + PUB_CACHE: /opt/flutter/.pub-cache commands: - - FLUTTER_HOME=/opt/flutter/.pub-cache - - wget -P $FLUTTER_HOME https://raw.githubusercontent.com/v7lin/pub_credentials/master/credentials.json.enc + - wget -P $PUB_CACHE https://raw.githubusercontent.com/v7lin/pub_credentials/master/credentials.json.enc - name: restore-cache-openssl image: v7lin/openssl:1.1.1b @@ -101,14 +98,14 @@ steps: - name: pub-cache path: /opt/flutter/.pub-cache environment: + PUB_CACHE: /opt/flutter/.pub-cache ENC_METHOD: from_secret: ENC_METHOD ENC_PASSWORD: from_secret: ENC_PASSWORD commands: - - FLUTTER_HOME=/opt/flutter/.pub-cache - - openssl enc -d -$ENC_METHOD -k $ENC_PASSWORD -in $FLUTTER_HOME/credentials.json.enc -out $FLUTTER_HOME/credentials.json - - rm $FLUTTER_HOME/credentials.json.enc + - openssl enc -d -$ENC_METHOD -k $ENC_PASSWORD -in $PUB_CACHE/credentials.json.enc -out $PUB_CACHE/credentials.json + - rm $PUB_CACHE/credentials.json.enc - name: publish image: v7lin/flutter:1.7.8-hotfix.3-stable @@ -124,14 +121,14 @@ steps: - name: pub-cache path: /opt/flutter/.pub-cache environment: + PUB_CACHE: /opt/flutter/.pub-cache ENC_METHOD: from_secret: ENC_METHOD ENC_PASSWORD: from_secret: ENC_PASSWORD commands: - - FLUTTER_HOME=/opt/flutter/.pub-cache - - openssl enc -e -$ENC_METHOD -k $ENC_PASSWORD -in $FLUTTER_HOME/credentials.json -out $FLUTTER_HOME/credentials.json.enc - - rm /opt/flutter/.pub-cache/credentials.json + - openssl enc -e -$ENC_METHOD -k $ENC_PASSWORD -in $PUB_CACHE/credentials.json -out $PUB_CACHE/credentials.json.enc + - rm $PUB_CACHE/credentials.json - name: save-cache image: docker:git @@ -139,6 +136,7 @@ steps: - name: pub-cache path: /opt/flutter/.pub-cache environment: + PUB_CACHE: /opt/flutter/.pub-cache GIT_USER_EMAIL: from_secret: GIT_USER_EMAIL GIT_USER_NAME: @@ -146,14 +144,13 @@ steps: GIT_USER_PASSWORD: from_secret: GIT_USER_PASSWORD # 密码含'@',用'%40'替换 -> URLEncoder.encode("@","utf-8"); commands: - - FLUTTER_HOME=/opt/flutter/.pub-cache - git config --global user.email $GIT_USER_EMAIL - git config --global user.name $GIT_USER_NAME - git config --global credential.helper store - - git clone -b master https://$GIT_USER_NAME:$GIT_USER_PASSWORD@github.com/v7lin/pub_credentials.git $FLUTTER_HOME/pub_credentials - - rm $FLUTTER_HOME/pub_credentials/credentials.json.enc - - mv $FLUTTER_HOME/credentials.json.enc $FLUTTER_HOME/pub_credentials/credentials.json.enc - - cd $FLUTTER_HOME/pub_credentials + - git clone -b master https://$GIT_USER_NAME:$GIT_USER_PASSWORD@github.com/v7lin/pub_credentials.git $PUB_CACHE/pub_credentials + - rm $PUB_CACHE/pub_credentials/credentials.json.enc + - mv $PUB_CACHE/credentials.json.enc $PUB_CACHE/pub_credentials/credentials.json.enc + - cd $PUB_CACHE/pub_credentials - git commit -am "update credentials by ci/cd tools" - git push diff --git a/android/build.gradle b/android/build.gradle index c3bf223..72439ce 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -28,15 +28,18 @@ android { defaultConfig { minSdkVersion 16 + + // library 混淆 -> 随 library 引用,自动添加到 apk 打包混淆 + consumerProguardFiles 'consumer-proguard-rules.txt' + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } + lintOptions { disable 'InvalidPackage' } } dependencies { - implementation fileTree(include: ['*.jar'], dir: 'libs') - - implementation 'io.github.v7lin:wechat-android:5.3.1+1' + implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:5.4.3' } diff --git a/android/consumer-proguard-rules.txt b/android/consumer-proguard-rules.txt new file mode 100644 index 0000000..dd6b55d --- /dev/null +++ b/android/consumer-proguard-rules.txt @@ -0,0 +1,5 @@ +# 微信 + +-keep class com.tencent.mm.opensdk.** {*;} +-keep class com.tencent.wxop.** {*;} +-keep class com.tencent.mm.sdk.** {*;} diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 72c1d8a..b1f6318 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -2,25 +2,30 @@ package="io.github.v7lin.fakewechat"> - - - + android:theme="@style/fake_wechat.Theme.Transparent" + android:exported="true" + android:launchMode="singleTask" + android:taskAffinity="${applicationId}" /> diff --git a/android/src/main/java/io/github/v7lin/fakewechat/FakeWechatPlugin.java b/android/src/main/java/io/github/v7lin/fakewechat/FakeWechatPlugin.java index 19f20b1..57fab1b 100644 --- a/android/src/main/java/io/github/v7lin/fakewechat/FakeWechatPlugin.java +++ b/android/src/main/java/io/github/v7lin/fakewechat/FakeWechatPlugin.java @@ -1,9 +1,13 @@ package io.github.v7lin.fakewechat; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.net.Uri; import com.tencent.mm.opensdk.constants.Build; +import com.tencent.mm.opensdk.constants.ConstantsAPI; import com.tencent.mm.opensdk.diffdev.DiffDevOAuthFactory; import com.tencent.mm.opensdk.diffdev.IDiffDevOAuth; import com.tencent.mm.opensdk.diffdev.OAuthErrCode; @@ -124,7 +128,7 @@ public class FakeWechatPlugin implements MethodCallHandler, PluginRegistry.ViewD private static final String ARGUMENT_KEY_RESERVED = "reserved"; private static final String ARGUMENT_KEY_PARTNERID = "partnerId"; private static final String ARGUMENT_KEY_PREPAYID = "prepayId"; -// private static final String ARGUMENT_KEY_NONCESTR = "noncestr"; + // private static final String ARGUMENT_KEY_NONCESTR = "noncestr"; // private static final String ARGUMENT_KEY_TIMESTAMP = "timestamp"; private static final String ARGUMENT_KEY_PACKAGE = "package"; private static final String ARGUMENT_KEY_SIGN = "sign"; @@ -151,6 +155,7 @@ public class FakeWechatPlugin implements MethodCallHandler, PluginRegistry.ViewD private final AtomicBoolean register = new AtomicBoolean(false); private IWXAPI iwxapi; + private BroadcastReceiver refreshWxappReceiver; private FakeWechatPlugin(Registrar registrar, MethodChannel channel) { this.registrar = registrar; @@ -187,7 +192,9 @@ public class FakeWechatPlugin implements MethodCallHandler, PluginRegistry.ViewD private WechatReceiver wechatReceiver = new WechatReceiver() { @Override public void handleIntent(Intent intent) { - iwxapi.handleIntent(intent, iwxapiEventHandler); + if (iwxapi != null) { + iwxapi.handleIntent(intent, iwxapiEventHandler); + } } }; @@ -242,10 +249,7 @@ public class FakeWechatPlugin implements MethodCallHandler, PluginRegistry.ViewD @Override public void onMethodCall(MethodCall call, Result result) { if (METHOD_REGISTERAPP.equals(call.method)) { - String appId = call.argument(ARGUMENT_KEY_APPID); - iwxapi = WXAPIFactory.createWXAPI(registrar.context().getApplicationContext(), appId); - iwxapi.registerApp(appId); - result.success(null); + registerApp(call, result); } else if (METHOD_ISWECHATINSTALLED.equals(call.method)) { result.success(iwxapi.isWXAppInstalled()); } else if (METHOD_ISWECHATSUPPORTAPI.equals(call.method)) { @@ -285,6 +289,26 @@ public class FakeWechatPlugin implements MethodCallHandler, PluginRegistry.ViewD } } + private void registerApp(MethodCall call, Result result) { + final String appId = call.argument(ARGUMENT_KEY_APPID); + iwxapi = WXAPIFactory.createWXAPI(registrar.context().getApplicationContext(), appId); + iwxapi.registerApp(appId); + if (refreshWxappReceiver != null) { + registrar.context().unregisterReceiver(refreshWxappReceiver); + refreshWxappReceiver = null; + } + refreshWxappReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (iwxapi != null) { + iwxapi.registerApp(appId); + } + } + }; + registrar.context().registerReceiver(refreshWxappReceiver, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP)); + result.success(null); + } + private void handleAuthCall(MethodCall call, Result result) { SendAuth.Req req = new SendAuth.Req(); req.scope = call.argument(ARGUMENT_KEY_SCOPE); @@ -450,6 +474,10 @@ public class FakeWechatPlugin implements MethodCallHandler, PluginRegistry.ViewD WechatReceiver.unregisterReceiver(registrar.context(), wechatReceiver); } qrauth.removeAllListeners(); + if (refreshWxappReceiver != null) { + registrar.context().unregisterReceiver(refreshWxappReceiver); + refreshWxappReceiver = null; + } return false; } } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 454958d..daa359d 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -34,7 +34,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "io.github.v7lin.fakewechatexample" - minSdkVersion 16 + minSdkVersion 21 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/example/pubspec.yaml b/example/pubspec.yaml index f453969..976caf8 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -16,7 +16,7 @@ dependencies: image: ^2.0.7 path: ^1.6.2 - fake_path_provider: ^0.0.1+1 + fake_path_provider: ^0.1.2 fake_wechat: path: ../ diff --git a/pubspec.yaml b/pubspec.yaml index 2107c70..dca8f85 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,12 +11,12 @@ dependencies: flutter: sdk: flutter - meta: '>=1.1.6 <2.0.0' + meta: ^1.1.6 uuid: '>=1.0.0 <3.0.0' - convert: '>=2.0.2 <3.0.0' - crypto: '>=2.0.6 <3.0.0' + convert: ^2.0.2 + crypto: ^2.0.6 - jaguar_serializer: '>=2.2.12 <3.0.0' + jaguar_serializer: ^2.2.12 dev_dependencies: flutter_test: @@ -27,7 +27,7 @@ dev_dependencies: image: ^2.0.7 path: ^1.6.2 - fake_path_provider: ^0.0.1+1 + fake_path_provider: ^0.1.2 build_runner: jaguar_serializer_cli: