困扰了一天,终于能正常打包apk安装了,记录下打包的流程。建议参考我这篇文章时,同时看下官网的构建说明。
官网构建并发布 Android 应用详情
1、AS创建Flutter项目
2、cmd执行命令
生成一个sunluyi.jks的文件,可以自行把sunluyi替换)
-alias sunluyi(别名),方便管理都用一个名字
keytool -genkey -v -keystore sunluyi.jks -keyalg RSA -keysize 2048 -validity 10000 -alias sunluyi
然后你会得到一个命名sunluyi.jks
的文件
3、cmd执行命令,查看文件详情
keytool -list -v -keystore sunluyi.jks
4、把生成好的
.jks
文件放在Flutter
项目android/app
目录下
并在android
目录下手动创建key.properties
文件
key.properties
文件内容就是第二步自己设置的口令密码和别名storeFile
对应的文件路径./sunluyi.jks
(app目录)
storePassword=sunluyi
keyPassword=sunluyi
keyAlias=sunluyi
storeFile=./sunluyi.jks
5、修改
/android/app/build.gradle
文件
下边代码中我标注了哪些是新增的内容,哪些是修改的内容
plugins {
id "com.android.application"
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
}
// 新增内容 ↓
def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader("UTF-8") { reader ->
localProperties.load(reader)
}
}
def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
flutterVersionCode = "1"
}
def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
flutterVersionName = "1.0"
}
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
// 新增内容↑
android {
namespace = "com.sunluyi.app.flutter_sunluyi"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.sunluyi.app.flutter_sunluyi"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
// 修改内容 ↓
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
// 修改内容 ↑
}
// 新增内容 ↓
signingConfigs {
release {
keyAlias = keystoreProperties['keyAlias']
keyPassword = keystoreProperties['keyPassword']
storeFile = keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword = keystoreProperties['storePassword']
}
}
// 新增内容 ↑
buildTypes {
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.release
// 修改内容 ↑
}
}
}
flutter {
source = "../.."
}
6、最后你可能还需要配置应用的权限
/android/app/src/main/AndroidManifest.xml
目录
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="[project]"
...
</application>
...
// 这里是新增的权限
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
最后根目录执行flutter build apk
打包成apk文件。
打包时有个提示警告:
Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.
记录下如何解决。
1、cmd执行:flutter doctor -v
下图是我解决过警告的,最开始是Platform android-35, build-tools 34.0.0
百度了下解决方案:
在设置/Android SDK
中最开始我是勾选的35,换成下方的勾选之后,再次执行
flutter doctor -v
得到:Platform android-35, build-tools 34.0.0
重新执行flutter build apk
后没有警告,完成了打包流程。
关于混淆
自己还不清楚哪些插件需要混淆,也不太理解混淆的意思,先不记录。
这篇文章内容我是创建的新项目进行打包测试的。
后边把自己用到的第三方插件在pubspec.yaml
配置后,也是可以正常打包安装的。
我的运行环境,希望这篇文章能对你有所帮助