Android原生项目的flutter module目录下是有本地自动生成的.android目录、.ios目录等文件夹的,比如:
Android原生项目下有这样一个flutter module:
在这里插入图片描述

而这些都是根据本地环境在创建flutter module时自动生成的,提交代码的时候是不会push到远程仓库的,即使push到远程仓库中,别人git clone下来也不能用这些自动生成的文件。因为gradle构建时会报错。

那么别人从远程仓库git clone这个项目到本地时,如何构建项目呢?因为clone下来的项目在polkawallet目录下是没有图中的.android、.ios、.dart_tool、build、.flutter-plugins、.flutter-plugins-dependencies等文件夹的(即图中灰色的文件夹和文件是没有的,需要执行flutter命令根据本地环境自动生成),而gradle构建的时候却依赖:polkawallet/.android/include_flutter.groovy这个文件,因为这是配置在Android项目的settings.gradle文件中的,比如Android项目的settings.gradle文件的内容如下:

include ':app'

// 加入下面配置
include ':polkawallet'
setBinding(new Binding([gradle:this]))
evaluate(new File(
        settingsDir,
        'polkawallet/.android/include_flutter.groovy'
))

以Android原生项目下的flutter module是polkawallet为例,执行以下命令即可自动构建项目:

假设执行下面的命令前已经clone了Android项目,并且命令行在项目的根目录下

//执行下面的命令前,当前命令行在Android项目的根目录下

git checkout -b feature-wallet_sdk_source origin/feature-wallet_sdk_source
cd polkawallet
flutter clean
flutter build bundle
cd ..
./gradlew assemblePlayDebug

git checkout -b feature-wallet_sdk_source origin/feature-wallet_sdk_source表示checkout到要打包的分支
flutter clean 清除flutter的资源目录
flutter build bundle 表示创建flutter的资源目录,即创建图中的.android、.ios这些资源目录
./gradlew assemblePlayDebug 构建的是debug包

flutter build的具体使用介绍可以使用 flutter build -h 命令进行查看

执行flutter clean的输出信息:

$ flutter clean
Deleting build...                                                    3ms
Deleting .dart_tool...                                               3ms
Deleting .packages...                                                0ms
Deleting .android...                                                 0ms
Deleting .ios...                                                     0ms
Deleting .flutter-plugins-dependencies...                            0ms
Deleting .flutter-plugins...                                         0ms

如果当前还没有clone项目,则操作要复杂点

需要执行如下命令:
(当前在计算机的任意目录下)

git clone https://github.com.cnpmjs.org/coming-chat/coming-android.git
cd coming-android/
git checkout -b feature-wallet_sdk_source origin/feature-wallet_sdk_source
cd polkawallet
flutter clean
flutter build bundle
cd ..
./gradlew assemblePlayDebug

如果报:

SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at '/home/liucz/AndroidStudioProjects/coming_androidv4_66_1_test/coming-android/local.properties'.

这是因为缺少local.properties 文件,这个文件是Android studio打开Android项目时,gradle会在项目的根目录下根据配置的sdk位置自动生成的,所以出现这个错误说明是在电脑的命令行执行的上述命令,而不是打开Android studio在Android studio的命令行中执行上述命令的。

解决方式是拷贝一份提前写好的local.properties 文件到当前Android项目的根目录下,或者也可以拷贝其他Android项目的(前提是是要本机的Android项目且正常构建的Android项目的local.properties 文件,只有这种项目中的local.properties 文件中的sdk的路径才是正确的)。

所以上述命令需要修改为:

git clone https://github.com.cnpmjs.org/coming-chat/coming-android.git
cd coming-android/
cp /home/liucz/StudioProjects/coming_androidv4_66_1/coming-android/local.properties . 
git checkout -b feature-wallet_sdk_source origin/feature-wallet_sdk_source
cd polkawallet
flutter clean
flutter build bundle
cd ..
./gradlew assemblePlayDebug

注意:在windows系统下,./gradlew assemblePlayDebug 要换为gradlew assemblePlayDebug

windows系统与linux/mac系统的命令使用略有区别:

//windows
gradlew :app:assembleRelease
//mac
./gradlew :app:assembleRelease

cp /home/liucz/StudioProjects/coming_androidv4_66_1/coming-android/local.properties . 拜师拷贝正常的local.properties 文件到当前目录下,可以拷贝其他正常运行的Android项目的根目录下的local.properties 文件即可。

git checkout -b feature-wallet_sdk_source origin/feature-wallet_sdk_source 表示checkout要打包的分支
cd polkawallet 进入到flutter module
flutter clean 清除flutter的资源目录
flutter build bundle 表示创建flutter的资源目录,即创建图中的.android、.ios这些资源目录
./gradlew assemblePlayDebug 构建的是debug包

更多:
flutter build aot 和flutter build bundle
研读Flutter——打包编译流程详解

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐