1.生成签名文件

keytool -genkey -v -keystore test.keystore  -alias test -keyalg RSA -validity 36000

生成test.keystore文件,别名test,有效期36000天

2.在android目录新建sign目录并拷贝签名文件到此目录

3.在sign目录新建keystore.properties文件

storePassword=123456
keyPassword=123456
keyAlias=test
storeFile=../sign/test.keystore

4.修改android/app/build.gradle文件

def signProperties = rootProject.file("sign/keystore.properties")
def props = new Properties()
props.load(new FileInputStream(signProperties))
def file = file(props['storeFile'])

android {
    //添加此项
    signingConfigs {
        release {
            //如果秘钥库文件和配置文件存在就签名,否则不签名
            if (file.exists() && signProperties.exists()) {
                keyAlias props['keyAlias']
                keyPassword props['keyPassword']
                storeFile file
                storePassword props['storePassword']
            }
        }

    }
    buildTypes {
        // 修改
        release {
             //混淆
            minifyEnabled true
            //去除无用res文件
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            //使用签名文件
            signingConfig signingConfigs.release
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            // signingConfig signingConfigs.debug
        }
    }
}

5.执行flutter build apk --release打包apk