原生安卓引入Flutter的arr包

162 阅读2分钟

执行命令先产生arr包

flutter build aar

构建flutter的arr包产物,最终都会产生debug,profile,release三个arr包,包括项目中引入的插件也会生成对应产物。

执行完成后会有下面提示:

Consuming the Module
  1. Open <host>\app\build.gradle
  2. Ensure you have the repositories configured, otherwise add them:

      String storageUrl = System.env.FLUTTER_STORAGE_BASE_URL ?: "https://storage.googleapis.com"
      repositories {
        maven {
            url 'D:\project\项目名称\build\host\outputs\repo'
        }
        maven {
            url "$storageUrl/download.flutter.io"
        }
      }

  3. Make the host app depend on the Flutter module:

    dependencies {
      debugImplementation '包名:flutter_debug:1.0'
      profileImplementation '包名:flutter_profile:1.0'
      releaseImplementation '包名:flutter_release:1.0'
    }


  4. Add the `profile` build type:

    android {
      buildTypes {
        profile {
          initWith debug
        }
      }
    }

To learn more, visit https://flutter.dev/go/build-aar
Process finished with exit code 0

根据提示进行对应操作就行,其中'D:\project\项目名称\build\host\outputs\repo'就是我们打包的产物

maven {
    url 'D:\project\项目名称\build\host\outputs\repo'
}

flutter会将我们打包的产物变成一个maven仓库,我们这里会声明仓库地址

dependencies {
  debugImplementation '包名:flutter_debug:1.0'
  profileImplementation '包名:flutter_profile:1.0'
  releaseImplementation '包名:flutter_release:1.0'
}

最终产物的目录结构大概就是这样

---repo
    ---com.xx.xxx
        ---flutter_debug
            ---1.0
                -flutter_debug-1.0.aar
                -flutter_debug-1.0.pom
        ---flutter_profile
            ...
        ---flutter_release
            ...
    ---io.flutter.plugins.packageinfo
        ---package_info_plus_debug
            ---1.0
                -package_info_plus_debug-1.0.aar
                -package_info_plus_debug-1.0.pom
        ---package_info_plus_profile
            ...
        ---package_info_plus_release
            ...

这里引入对应三个模式的包,在仓库包的同级目录中的pom文件会引入对应插件,所以我们不用在这里声明 例如flutter-debug-1.0.pom文件的内容为

<?xml version="1.0" encoding="UTF-8"?>  
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  <!-- This module was also published with a richer model, Gradle metadata,  -->  
  <!-- which should be used instead. Do not delete the following line which  -->  
  <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->  
  <!-- that they should prefer consuming it instead. -->  
  <!-- do_not_remove: published-with-gradle-metadata -->  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>包名</groupId>  
  <artifactId>flutter_debug</artifactId>  
  <version>1.0</version>  
  <packaging>aar</packaging>  
  <dependencies>  
    <dependency>  
      <groupId>io.flutter</groupId>  
      <artifactId>flutter_embedding_debug</artifactId>  
      <version>1.0.0-1a65d409c7a1438a34d21b60bf30a6fd5db59314</version>  
      <scope>compile</scope>  
    </dependency>  
    <dependency>  
      <groupId>io.flutter</groupId>  
      <artifactId>armeabi_v7a_debug</artifactId>  
      <version>1.0.0-1a65d409c7a1438a34d21b60bf30a6fd5db59314</version>  
      <scope>compile</scope>  
    </dependency>  
    <dependency>  
      <groupId>io.flutter</groupId>  
      <artifactId>arm64_v8a_debug</artifactId>  
      <version>1.0.0-1a65d409c7a1438a34d21b60bf30a6fd5db59314</version>  
      <scope>compile</scope>  
    </dependency> 
  </dependencies>  
</project>

其中dependencies下就是引入的第三方包,他会在本地仓库去查找该包,所以你需要上传到本地maven仓库的话需要将这个pom文件也上传上去,不然他会找不到对应的包下载。

这里贴上 官方文档


扩展:

如果我们是多人合作开发的话,使用本地的产物终究不合适,总不可能每次打包后每个人发一份,除刚刚说的上传到maven仓库外我们还可以上传到git仓库上去

我们新建一个仓库,在仓库建一个目录maven-repo,将产物丢进去,例如gitee仓库

然后我们这样配置maven地址

maven {
  url 'https://gitee.com/username/仓库名称/raw/release/maven-repo'
  allowInsecureProtocol = true // 如果你的仓库是http协议的
}

使用http协议的仓库必须加allowInsecureProtocol=true,https的话删除这行就行

使用本地仓库可能会出现

Could not HEAD 'http://git地址:8080/xxx/xxx/raw/release/maven-repo/包名/flutter_debug/1.0/flutter_debug-1.0.pom'.
Received status code 502 from server: No data received from server or forwarder

解决方法:

前往 C:\Users\UserName.gradle 目录下

修改 gradle.properties 文件 将代理注释掉就行

#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1080
#systemProp.https.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1080

本文章就讲述怎么引入,具体怎么使用怎么玩请看官方文档 ↑