Flutter AOP framework.

Related tags

Templates aop flutter
Overview

Beike_AspectD

This is a fork of AspectD.

Beike_AspectD is an aop framework for dart. AspectD has provide developers call/execute/inject grammer to manipulate the dart code. Besides that, Beike_AspectD also provides

  • Support add grammer to add function to classes.
  • Support field get grammer to exchange the field get call.
  • Support flutter web.

What can we use Beike_AspectD for?

Beike has used Beike_AspectD in many packages.

  • Event tracking.
  • Json to model.
  • Performance monitoring.
  • Flutter framework bug fixing.

Installation

1. Apply flutter_tools.patch.

cd path-for-flutter-tools-git-repo
git apply --3way path-for-beike_aspectd-package/inner/0001-aspectd.patch
rm ../../bin/cache/flutter_tools.stamp

Next time when you build your project, flutter tools will build automatically.

2. Add Beike_AspectD to your yaml.

dependencies:
  beike_aspectd:
    path: ../

3. Add aop_config.yaml to your flutter project.

Add a file named aop_config.yaml to your flutter project's root directory(the same directory as pubspec.yaml).

You can copy the file from the example of Beike_AspectD. The content of the file are as follow

flutter_tools_hook:
  - project_name: 'beike_aspectd'
    exec_path: 'bin/starter.snapshot'

Flutter_tools will check the file to find if Beike_AspectD is enabled. And it will get the starter.snapshot to process the dill file.

4. Write your hook file and import the file.

hook_example.dart(aop implementation)

import 'package:beike_aspectd/aspectd.dart';

@Aspect()
@pragma("vm:entry-point")
class CallDemo {
  @pragma("vm:entry-point")
  CallDemo();

 //实例方法
 @Call("package:example/main.dart", "_MyHomePageState",
     "-_incrementCounter")
 @pragma("vm:entry-point")
 void _incrementCounter4(PointCut pointcut) {
   print('call instance method2!');
   pointcut.proceed();
 }
}

As hook_example.dart is not used in your project, we should import it in the project, or it will be tree shaked while compiling.

For example, we can import the file in main.dart.

// ignore: unused_import
import 'package:example/hook_example.dart';

Tutorial

In addition to the 3 ways(call/execute/inject) to do AOP programming AspectD provide us, Beike_AspectD also provide add/field get manipulation.

add

Add a method to a class, support class name regex, support super class filter.

  @Add("package:.+\\.dart", ".*", isRegex: true)
  @pragma("vm:entry-point")
  dynamic getBasicInfo(PointCut pointCut) {
    return pointCut?.sourceInfos ?? {};
  }

Code above add getBasicInfo() method to all the classes, you can use your own regex and superCls parameter to filter classes. We can call the function using the following code.

    dynamic self = someinstance;
    Map info = self.getBasicInfo(PointCut.pointCut());

field get

Every callsites of the a field will be manipulated.

 @pragma("vm:entry-point")
 @FieldGet('package:example/main.dart', 'MyApp', 'field', false)
 static String exchange2(PointCut pointCut) {
    return 'Beike_Aspectd';
}

Suppose MyApp class has a property called field, by using the the above code, when calling the property field, it will always return string 'Beike_Aspectd'.

Compatibility

Currently Beike_Aspectd support flutter 1.22.4 and 2.2.2 .

Q&A

  • How to know if my code is hooked successfully?
    1. First you need to download the dart-sdk and checkout to the corresponding revision of flutter. The revision of dart can be found at path_to_flutter/bin/cache/dart-sdk/revision.
    2. Run the following command.
    path_to_flutter/bin/cache/dart-sdk/bin/dart  path_to_dart/pkg/vm/bin/dump_kernel.dart path_to_your_project/.dart_tool/flutter_build/***/app.dill output_path/out.dill.txt
    1. Open output_path/out.dill.txt file, check if your code is hook.

Contact

If you have any question, please feel free to file a issue.

Comments
  • 嵌套函数hook会报错---Null check operator used on a null value

    嵌套函数hook会报错---Null check operator used on a null value

    class TextRightImageModel {
      String showName;
    
      String rightIcon;
    
      Widget showCardWidget(BuildContext context) {
        return Container(
          padding: const EdgeInsets.only(left: 4.0, right: 4.0),
          alignment: Alignment.center,
          child: wrapTextLimit(context, const Text("")),
        );
      }
    
      Widget wrapTextLimit(BuildContext str, Widget widget) {
        return widget;
      }
    
    }
    

    我的业务代码是上面这个,然后通过以下代码去hook,编译的时候一定会报错

     @Call(
          'package:test/test.dart', 'TextRightImageModel', '-.*',
          isRegex: true)
      @pragma("vm:entry-point")
      dynamic _aop_Two(PointCut pointcut) {
        return pointcut.proceed();
      }
    

    flutter 环境:2.5.3

    bug documentation 
    opened by LDouble 9
  • Flutter3.0.0版本没有生成aspectd.dart文件

    Flutter3.0.0版本没有生成aspectd.dart文件

    在Flutter3.0环境下执行: cd ...path/to/flutter/packages/flutter_tools/ git apply --3way path-for-beike_aspectd-package/inner/flutter_tools.patch rm ../../bin/cache/flutter_tools.stamp 之后没有反应,flutter_tools/bin目录下没有aspectd.dart文件。 执行第二步apply之后本地没有任何代码变化,git status是clean的。 image

    opened by jincan-he 8
  • frontend_server.dart.snapshot文件如何生成?

    frontend_server.dart.snapshot文件如何生成?

    想定制一些自己的hook规则,请问如何生成frontend_server.dart.snapshot? 目前看,应该是贝壳先生成好了frontend_server.dart.snapshot后在flutter_tools编译的时候直接替换的,但是根据gen_frontend_server_snapshot.sh脚本无法生成snapshot,package_config.json中指明的third_party找不到。

    opened by Night1992 6
  • 按着调试文档步骤调试无法生成frontend_server参数

    按着调试文档步骤调试无法生成frontend_server参数

    按着调试文档调试流程调试,在调试Flutter_tools时获取不到frontend_server参数。想问一下我的配置参数是否错误。 在Dart Command Line App中我的Working directory 选择的是Beike_AspectD/example文件,想问一下是不是这个选择的不对导致的。希望得到解答。 image

    opened by luckyBoyFeng 4
  • 执行git apply --3way后没有生成aspectd.dart文件

    执行git apply --3way后没有生成aspectd.dart文件

    下载了demo工程后按照readme.md配置后,把demo跑起来,解析发现dill文件里面并没有add的插桩代码。检查发现在执行这部git apply --3way /Users/xxxx/tools/aspected/Beike_AspectD/inner/flutter_tools.patch 的时候,并没有在flutter sdk的packages/flutter_tools/lib/src中生成aspectd.dart文件。

    和我这里的操作有关么?

    版本配置: Flutter 2.5.3 Dart 2.14.4

    我最开始是执行这个,有提示这个错误 ` cd /Users/xxxx/tools/aspected/flutter/packages/flutter_tools git apply --3way /Users/xxxx/tools/aspected/Beike_AspectD/inner/flutter_tools.patch

    error: packages/flutter_tools/lib/src/build_system/targets/common.dart: does not match index error: packages/flutter_tools/lib/src/build_system/targets/web.dart: does not match index error: packages/flutter_tools/lib/src/commands/build_bundle.dart: does not match index error: packages/flutter_tools/lib/src/compile.dart: does not match index error: packages/flutter_tools/lib/src/web/chrome.dart: does not match index `

    我在这个目录执行这命令是没有错误,但是aspectd.dart没有生成 pwd /Users/xxxx/tools/aspected/flutter/packages/flutter_tools/bin git apply --3way /Users/xxxx/tools/aspected/Beike_AspectD/inner/flutter_tools.patch

    opened by kasentom 4
  • 3.0.5分支下混合开发模式安卓,debug模式正常,release模式触发点击事件后报错

    3.0.5分支下混合开发模式安卓,debug模式正常,release模式触发点击事件后报错

    I/flutter: type '_OneByteString' is not a subtype of type 'int' of 'index' I/flutter: #0 PointCut.aop_stub_26 (package:beike_aspectd/src/plugins/aop/annotation/pointcut.dart) I/flutter: #1 PointCut.proceed (package:beike_aspectd/src/plugins/aop/annotation/pointcut.dart) I/flutter: #2 ClickAopHook.hookInvokeCallback (package:tdx_flutter/aop/hook_entry_points.dart:36) I/flutter: #3 new _GrowableList._literal2 (dart:core-patch/growable_array.dart) I/flutter: #4 DragGestureRecognizer._checkDown (package:flutter/src/gestures/monodrag.dart) I/flutter: #5 DragGestureRecognizer.addAllowedPointer (package:flutter/src/gestures/monodrag.dart:276)

    Flutter版本,3.0.5 混合开发模式是否需要自己编译引擎?

    opened by weicheng59 3
  • flutter2.10.5使用2.10.4分支出现null safety

    flutter2.10.5使用2.10.4分支出现null safety

    : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/lib/aspectd.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/aop/aop.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/annotation/call.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/annotation/execute.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/annotation/inject.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/annotation/pointcut.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/beike_annotation/add.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/beike_annotation/field_get.dart:1 // @dart=2.8 ^^^^^^^^^^^^ : Error: A library can't opt out of null safety by default, when using sound null safety. ../…/annotation/annotation_info.dart:1 // @dart=2.8 ^^^^^^^^^^^^ Error: Cannot run with sound null safety, because the following dependencies don't support null safety: - package:beike_aspectd For solutions, see https://dart.dev/go/unsound-null-safety Failed to package /Users/xx/Desktop/xx/xxxx. Command PhaseScriptExecution failed with a nonzero exit code note: Using new build system note: Planning

    opened by softAlexs 2
  • 修改flutter_tool后执行项目出错,Can't load Kernel binary: Invalid kernel binary format version.

    修改flutter_tool后执行项目出错,Can't load Kernel binary: Invalid kernel binary format version.

    重新构建flutter_tools的信息为 Flutter 3.0.5 • channel stable • https://github.com/flutter/flutter.git Framework • revision f1875d570e (9 weeks ago) • 2022-07-13 11:24:16 -0700 Engine • revision e85ea0e79c Tools • Dart 2.17.6 • DevTools 2.12.2

    但是执行项目代码失败 Can't load Kernel binary: Invalid kernel binary format version. the Dart compiler exited unexpectedly. the Dart compiler exited unexpectedly.

    是哪里版本不一致吗

    opened by berniebd 1
  • 运行项目后修改代码再次运行出现Unhandled exception,需要执行flutter clean清理缓存才可以恢复

    运行项目后修改代码再次运行出现Unhandled exception,需要执行flutter clean清理缓存才可以恢复

    WX20220802-195451@2x Unhandled exception: root::package:beike_aspectd/src/plugins/aop/annotation/pointcut.dart::PointCut::@methods::aop_stub_0 is already bound to Reference to package:beike_aspectd/src/plugins/aop/annotation/pointcut.dart::PointCut::@methods::aop_stub_0 with node PointCut.aop_stub_0 (Procedure:114447), trying to bind to Reference to PointCut.aop_stub_0 with node PointCut.aop_stub_0 (Procedure:4371882) package:beike_aspectd/…/annotation/pointcut.dart:1 #0 CanonicalName.bindTo (package:kernel/canonical_name.dart:236:7) #1 Class.computeCanonicalNames (package:kernel/ast.dart:1284:51) #2 Library.computeCanonicalNames (package:kernel/ast.dart:509:14) #3 Component.computeCanonicalNamesForLibrary (package:kernel/ast.dart:13669:13) #4 Component.computeCanonicalNames (package:kernel/ast.dart:13631:7) #5 sortComponent (package:vm/kernel_front_end.dart:702:13) #6 FrontendCompiler.writeDillFile (package:frontend_server/frontend_server.dart:696:5) #7 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:576:13) #8 listenAndCompile. (package:frontend_server/frontend_server.dart:1154:11)

    2 the Dart compiler exited unexpectedly.

    opened by softAlexs 1
  • 静态方法用execute报错

    静态方法用execute报错

    3.0.0分支,hook静态方法,比如demo里的

    @Execute("package:example/receiver_test.dart", "Receiver", "+tap") @pragma("vm:entry-point") static dynamic tap(PointCut pointcut) { print('[beike_aspectd]: Execute static method!'); pointcut.proceed(); }

    报错:

    Unhandled exception: [ ] Crash when compiling null, [ ] at character offset null: [ ] NoSuchMethodError: The getter 'nonNullable' was called on null. [ ] Receiver: null [ ] Tried calling: nonNullable [ ] #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5) [ ] #1 Procedure.getterType (package:kernel/ast.dart:3204:59) [ ] #2 AopUtils.insertProceedBranch

    opened by Night1992 1
  • 泛型会报错

    泛型会报错

      T testGenericType<T>(Map<T, dynamic> param) {
        return null;
      }
    
    

    如果去切泛型函数的话,就会抛出这个错误

    [+2599 ms] [+2561 ms] Unhandled exception:
    [        ]            Invalid argument(s): Type parameter TypeParameter(T) is not indexed
    [        ]            #0      TypeParameterIndexer.[] (package:kernel/binary/ast_to_binary.dart:2871:8)
    [        ]            #1      BinaryPrinter.visitTypeParameterType (package:kernel/binary/ast_to_binary.dart:2397:38)
    [        ]            #2      TypeParameterType.accept (package:kernel/ast.dart:11472:42)
    [        ]            #3      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:420:10)
    [        ]            #4      BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:331:7)
    [        ]            #5      BinaryPrinter.visitInterfaceType (package:kernel/binary/ast_to_binary.dart:2295:7)
    [        ]            #6      InterfaceType.accept (package:kernel/ast.dart:10703:42)
    [        ]            #7      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:420:
    
    opened by LDouble 1
Owner
Ke Technologies
Ke Technologies
Flying Fish is full-stack Dart framework - a semi-opinionated framework for building applications exclusively using Dart and Flutter

Flying Fish is full-stack Dart framework - a semi-opinionated framework for building applications exclusively using Dart and Flutter.

Flutter Fish 3 Dec 27, 2022
Intel Corporation 238 Dec 24, 2022
The ROHD Verification Framework is a hardware verification framework built upon ROHD for building testbenches.

ROHD Verification Framework The ROHD Verification Framework (ROHD-VF) is a verification framework built upon the Rapid Open Hardware Development (ROHD

Intel Corporation 18 Dec 20, 2022
A flutter project with a description of the basic Flutter framework

Pengenalan kepada Mobile Platform (Slide) Apa itu Flutter Framework Pemasangan persekitaran Flutter, Android SDK & VSCode IDE Widget Layout & UI Eleme

Mohamad Zaki Mustafa 3 Jan 3, 2022
Design system flutter - A framework contains SBB (Swiss Federal Railways) UI elements for Flutter Apps

Design System Mobile for Flutter Design System Mobile in Flutter (yes, it could

Swiss Federal Railways (SBB) 14 Dec 22, 2022
Simple tool to open WhatsApp chat without saving the number, developed using Google's Flutter Framework. for Android/ IOS/ Desktop/ Web

OpenWp Simple tool to open WhatsApp chat without saving the number Explore the docs » View Demo · Report Bug · Request Feature Table of Contents About

Swarup Bhanja Chowdhury 15 Nov 1, 2022
Project demonstrates building a simple chat application using Flutter framework and Firebase cloud

Flutter Chat on Firebase Project demonstrates building a simple chat application using Flutter framework and Firebase cloud. App does not poll for new

Sukitha Udugamasooriya 8 Feb 2, 2022
A comprehensive guide on learning how to code cross platform mobile applications with the Flutter framework, from the ground up.

✳️ The Ultimate Guide to App Development with Flutter ✳️ A complete and comprehensive guide to learning Flutter with explanations, screenshots, tips,

Anthony 243 Jan 1, 2023
This is an android application using Flutter Framework

quickpic A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if

Havishwaran J 0 Nov 8, 2021
For Practice Flutter Framework

xylophone A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

Kaung Pyae Min Thein 1 Nov 13, 2021
Prism is a beautiful open-source wallpapers app for Android. It is built with Dart on top of Google's Flutter Framework.

Prism Prism is a beautiful open-source wallpapers app for Android. It is built with Dart on top of Google's Flutter Framework. Prism brings you exclus

Hash Studios 473 Dec 31, 2022
A mobile app created using Flutter Framework for School management.

Our E School This Project has been Archived as I'm no longer working on it NOTE TO NEW CLONERS: (READ THIS BEFORE USING OR CREATING AN ISSUE) || This

Ketan Choyal 442 Jan 3, 2023
An app made using the Flutter framework that allows users to track information for over 1500 cryptocurrencies

Platypus Crypto Platypus Crypto is an ad-free cross-platform robust solution for tracking cryptocurrency assets. Our intuitive interface includes real

null 179 Jan 4, 2023
An MVP framework for flutter applications

mvp_core An MVP (model/view/presenter) framework for applications written in dart. This package gives specific support to the flutter framework. Getti

Josiah Saunders 3 Jan 4, 2021
Speed Share is a highly available file sharing terminal on LAN(local area network) developed by flutter framework.

速享 Language: 中文简体 | English 这是一款完全基于局域网的文件互传终端,速享不使用任何服务器,不使用您的移动流量,不收集任何用户数据,完全的点对点传输。 可以快速共享文本消息,图片或其他文件,文件夹。 适用于局域网中的文件互传,解决 QQ,微信等上传文件会经过服务器的问题,或者

null 477 Dec 31, 2022
Basic file managing app for Android using Flutter framework

core_file_manager A simple application for managing files on Android devices using Flutter framework core_file_manager Getting Started Running the app

Mohamed Naga 25 Sep 20, 2022
EZ Flutter is a collection of widgets, packages and many more usefull things, mixed up in little framework.

(Alpha) EZ Flutter is a collection of widgets, packages and many more usefull things, mixed up in a little framework. The aim is to make standard feat

null 65 Nov 5, 2022
A powerful UI framework for Google Flutter.

English | 简体中文 FLUI A powerful UI framework for Google Flutter Demo apk Features A set of high-quality Flutter widgets out of the box Comprehensive us

Hanran Liu 1.4k Dec 30, 2022
A simple yet elegant tip calculator created using flutter framework and dart language.

CAL- TIP, A TIP CALCULATOR APPLICATION A simple yet elegant tip calculator created using flutter framework and dart language. As the name suggests, th

Nitin Verma 0 Dec 26, 2021