目录
可随意转载!
前言
安卓系统为我们提供了Accessibility(辅助功能)服务。安卓官方的的UI Automator也是依赖这项基础能力来完成各种复杂的自动化测试。那么在Flutter中要如何实现相同的能力呢?
一、创建插件项目
flutter create --org cn.abilitygame -t plugin -a java --platforms android ability_accessibility_plugin
输出以下日志表示成功:
All done!
Your plugin code is in ability_accessibility_plugin/lib/ability_accessibility_plugin.dart.
Your example app code is in ability_accessibility_plugin/example/lib/main.dart.
Host platform code is in the android directories under ability_accessibility_plugin.
To edit platform code in an IDE see https://flutter.dev/developing-packages/#edit-plugin-package.
To add platforms, run `flutter create -t plugin --platforms <platforms> .` under ability_accessibility_plugin.
For more information, see https://flutter.dev/go/plugin-platforms.
二、配置项目
用AndroidStudio打开项目,切换左上视图为Project,打开android目录并修改四个配置文件:
- $ROOT/android/build.gradle
- $ROOT/example/android/build.gradle
- $ROOT/example/android/app/build.gradle
- $ROOT/example/android/gradle/wrapper/gradle-wrapper.properties
group 'cn.abilitygame.ability_accessibility_plugin'
version '1.0'
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
}
dependencies {
// classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:7.1.3'
}
}
rootProject.allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 31
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion 23
}
}
android {
compileSdkVersion 31
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "cn.abilitygame.ability_accessibility_plugin_example"
minSdkVersion 23
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
三、构建插件项目
退出Android Studio,回到命令行执行以下命令:
cd ability_accessibility_plugin/example
flutter build apk --target-platform android-arm,android-arm64 --split-per-abi
输出以下日志表示环境好了,可以开始编码了:
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
💪 Building with sound null safety 💪
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Running Gradle task 'assembleRelease'... 19.9s
✓ Built build/app/outputs/flutter-apk/app-armeabi-v7a-release.apk (9.8MB).
四、编辑UI
- $ROOT/example/lib/main.dart
- $ROOT/lib/ability_accessibility_plugin.dart
- $ROOT/lib/accessibility_event.dart
- $ROOT/lib/constants.dart
- $ROOT/lib/utils.dart
五、编写安卓本地代码
- $ROOT/example/android/app/src/main/AndroidManifest.xml
- $ROOT/example/android/app/src/main/java/cn/abilitygame/ability_accessibility_plugin/AbilityAccessibilityPlugin.java
- $ROOT/example/android/app/src/main/java/cn/abilitygame/ability_accessibility_plugin/AccessibilityListener.java
- $ROOT/example/android/app/src/main/java/cn/abilitygame/ability_accessibility_plugin/AccessibilityReceiver.java
- $ROOT/example/android/app/src/main/java/cn/abilitygame/ability_accessibility_plugin/Utils.java
- $ROOT/example/android/app/src/main/res/xml/accessibilityservice.xml
六、源代码&效果展示
github源码(未开源)