flutter插件 aar引入 2
发布时间:2021-05-18 20:13
目的: 在 flutter Plugin中引入 aar
引言:Android项目中引入aar,直接放在本地,在gradle中implementation即可,同样操作在Flutter项目却报错not found aar,如此需要其他解决方法。本文中介绍一种自己项目中运用到的maven引入法,供参考~
装备:安装mvn :1、纯原mac使用$ brew install maven 安装;
2、使用了zsh 命令和其他系统一样先去 apache官网下载 maven安装包,然后安装后配置到系统路径;
3、创建Flutter Plugin module,此处省略.
操作:
1、在你的Flutter Plugin中的native-android目录中新建一个目录libs,与src同级;
2、把你要引入的aar拷入 libs;
3、使用maven创建本地仓库,以供本地代码使用。
4、编辑build.gradle:
案例教程:这里用的是网易云盾aar
1. 在AndroidStudio的Terminal或者Mac终端,cd xxx/libs(刚创建的目录),执行下面语句:
2. mvn deploy:deploy-file -Dfile=captcha-release_v3.1.5.aar -Durl="file://." -DgroupId="com.captcha" -DartifactId="sdk" -Dversion="3.1.5"
3.
注:这里-Dfile 指向 aar 文件, url 指向目标地址,因上面已cd进入目录,此处是“.”, DgroupId是组名,DartifactId是品名,Dversion是版本号
maven创建成功日志:
1. [INFO] Scanning for projects...
2. [INFO]
3. [INFO] ------------------< org.apache.maven:standalone-pom >-------------------
4. [INFO] Building Maven Stub Project (No POM) 1
5. [INFO] --------------------------------[ pom ]---------------------------------
6. [INFO]
7. [INFO] --- maven-deploy-plugin:2.7:deploy-file (default-cli) @ standalone-pom ---
8. Uploading to remote-repository: file://./com/captcha/sdk/3.1.5/sdk-3.1.5.aar
9. Uploaded to remote-repository: file://./com/captcha/sdk/3.1.5/sdk-3.1.5.aar (99 kB at 2.7 MB/s)
10. Uploading to remote-repository: file://./com/captcha/sdk/3.1.5/sdk-3.1.5.pom
11. Uploaded to remote-repository: file://./com/captcha/sdk/3.1.5/sdk-3.1.5.pom (417 B at 139 kB/s)
12. Downloading from remote-repository: file://./com/captcha/sdk/maven-metadata.xml
13. Uploading to remote-repository: file://./com/captcha/sdk/maven-metadata.xml
14. Uploaded to remote-repository: file://./com/captcha/sdk/maven-metadata.xml (294 B at 147 kB/s)
15. [INFO] ------------------------------------------------------------------------
16. [INFO] BUILD SUCCESS
17. [INFO] ------------------------------------------------------------------------
18. [INFO] Total time: 0.382 s
19. [INFO] Finished at: 2021-05-03T20:50:29+08:00
生成的maven目录结构:
编辑build.gradle,以引入maven下的aar:
1. // 定义一个方法,用于获取当前moudle的dir
2. def getCurrentProjectDir() {
3. String result = ""
4. rootProject.allprojects { project ->
5. if (project.properties.get("authCode").toString() == ":verify_code") { //verify_code是Flutter Plugin module的插件名,注意project前必须加":"
6. result = project.properties.get("projectDir").toString()
7. }
8. }
9. return result
10. }
11.
12. 在rootProject.allprojects配置:
13. rootProject.allprojects {
14. def dir = getCurrentProjectDir()
15. repositories {
16. google()
17. jcenter()
18. maven { // 添加这个指向本地的仓库目录
19. url "$dir/libs"
20. }
21. }
22. }
23.
24. 最后在dependencies引入
25. dependencies {
26. implementation "com.captcha:sdk:3.1.5"
27. }
至此,aar成功导入Flutter Plugin的native中,可以愉快的写代码了~
更新时间:2024-12-18 20:26