Flutter module生成aar供Android已有工程依赖

背景:

1、新版本1.12+与老版本引入方式发生了变化,特此记录
2、FlutterView已经废弃,替换为FlutterFragment,但它不支持生命周期管理,需要借助Activity

Flutter module工程结构预览

image.png

开始打包

  1、首先连接真机,run一下,保证正常启动Flutter应用
  2、flutter clean(在flutter根目录输入)
  3、flutter build aar
      
  注意:如果flutter工程中使用第三方库那么需要用到fat-aar 插件工具
  详细[https://blog.csdn.net/weixin_31925685/article/details/112531943](https://blog.csdn.net/weixin_31925685/article/details/112531943)
  https://juejin.im/post/5e153f10f265da5d6b3cb767
  https://blog.csdn.net/qizewei123/article/details/102768038
  第三方Fat-aar  https://github.com/kezong/fat-aar-android
截屏2021-10-19 下午10.47.16.png

接入方式一(依赖本地仓库)

  1. Open /app/build.gradle

  2. Ensure you have the repositories configured, otherwise add them:

    repositories {
      maven {
          url 
              '/Users/niudong/Desktop/Project/android/flutter_library/build/host/outputs/repo'
          }
      maven {
      url '$storageUrl/download.flutter.io'
    }
    }
    
  3. Make the host app depend on the Flutter module:

dependencies {
  debugImplementation 'com.example.flutter_library:flutter_debug:1.0'
  profileImplementation 'com.example.flutter_library:flutter_profile:1.0'
  releaseImplementation 'com.example.flutter_library:flutter_release:1.0'
}
  1. Add the profile build type:
android {
  buildTypes {
    profile {
      initWith debug
    }
  }
}

详细介绍:

[https://flutter.dev/docs/development/add-to-app/android/project-setup#option-a---depend-on-the-android-archive-aar]

接入方式二 (不依赖本地仓库) 推荐

1、找到你的flutter根目录下的build/host/outputs/repo,将flutter_release-1.0.aar复制到android项目下

/Users/niudong/Desktop/Project/android/flutter_library/build/host/outputs/repo/com/example/flutter_library/flutter_release/1.0
image.png

2、打开你的安卓工程,将aar copy到libs目录下

image.png

3、app build.grade配置 也可以上传maven

repositories {
    flatDir {
    dirs 'libs'   // aar目录
  }
}
添加如下:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(name: 'flutter_release-1.0', ext: 'aar')
implementation 'io.flutter:flutter_embedding_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
implementation 'io.flutter:armeabi_v7a_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
implementation 'io.flutter:arm64_v8a_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
implementation 'io.flutter:x86_64_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
}

注意:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a 来自哪里呢?
姿势:找到flutter根目录下的build/host/outputs/repo, 将flutter_release-1.0.pom,里面有如下flutter基础库的地址
  com.example.flutter_library
  flutter_release
  1.0
  aar
  
  
  io.flutter.plugins.sharedpreferences
  shared_preferences_release
  1.0
  compile
  
  
  io.flutter
  flutter_embedding_release
  1.0.0-626244a72c5d53cc6d00c840987f9059faed511a
  compile
  

4、外层build.grade配置

buildscript {
repositories {
    google()
    jcenter()
    maven {
        url "http://download.flutter.io"
    }
  }
dependencies {
    classpath 'com.android.tools.build:gradle:4.0.0'
  }
}

5、组件化工程通常是在某个module/lib下依赖,比如module_flutter

 在module_flutter build.gradle下配置
  repositories {
      flatDir {
        dirs 'libs'   // aar目录
      }
    }

在主App 下配置
repositories {
//    详细路径
flatDir {
    dirs 'libs', '../module_flutter/libs'
  }
}

6、在你的MainActivity 加入如下代码

import androidx.appcompat.app.AppCompatActivity;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.view.FlutterMain;

/**
 * 原生安卓跳转Flutter
 */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    FlutterMain.startInitialization(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivity(FlutterActivity.createDefaultIntent(this));
  }
}

7、在你的清单文件中加入FlutterActivity,注意导包io.flutter.embedding.android




    
    
        
            

            
        
    


8、run你的Android程序,将看到:


image.png

9、与Flutter通信

 public class BaseFlutterActivity extends FlutterActivity {
 //跳转该页面的时候可以传要跳转的页面,参数名固定为route
 private static final String ROUTE_PAGE = "route";
 private static final String CHANNEL = "channel_test";
 public void onCreate(@Nullable Bundle savedInstanceState) {
    //初始化Flutter
    super.onCreate(savedInstanceState);

    new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
            (call, result) -> {
                if (call.method.equals("getRes")) {
                    Toast.makeText(BaseFlutterActivity.this, "来自 flutter 的 toast ", Toast.LENGTH_LONG).show();
                    result.success("我是Android App");
                } else if (TextUtils.equals("exit", call.method)) {
                    finish();
                } else {
                    result.notImplemented();

                }
            });
  }
 }

你可能感兴趣的:(Flutter module生成aar供Android已有工程依赖)