您现在的位置是:网站首页> Flutter
搭建Fluter Android Studio开发环境
- Flutter
- 2021-10-03
- 925人已阅读
//如果 pub get总是下载失败的话,可以使用国内的镜像
//设置环境变量:PUB_HOSTED_URL=https://pub.flutter-io.cn
命令行
flutter pub get
flutter packages get
特别注意项目的配置文件:pubspec.yaml 格式严格遵循否则编译报错
AndroidStudio安装Flutter的爬坑之路
PUB_HOSTED_URL 对应 https://pub.flutter-io.cn
FLUTTER_STORAGE_BASE_URL 对应 https://storage.flutter-io.cn
这两个镜像已经不能使用改为
FLUTTER_STORAGE_BASE_URL https://mirrors.tuna.tsinghua.edu.cn/flutter
PUB_HOSTED_URL https://mirrors.tuna.tsinghua.edu.cn/dart-pub
Android Studio取消HTTP Proxy无效处理
Android Studio之前设置了HTTP Proxy,但是在修改设置为No Proxy之后,HTTP 代理依然存在,原因在于AS生成的配置文件gradle.properties文件中依然存在代理设置,需要手动删除。
删除路径:C盘-用户-用户名-打开.gradle文件-删除后面的代理设置
如果要生成编译后的版本,执行flutter build web,在build\web目录下会生成html文件和js脚本。这时候直接运行是不行的,file:///...是不能识别的,要在正式域名下才能正常显示,和Vue下打包的生产版本是一个道理。
正常运行的代码下载 打开代理编译
设置dark-sdk
正常的文件
app下的build.gradle
localProperties = Properties() localPropertiesFile = rootProject.file() (localPropertiesFile.exists()) { localPropertiesFile.withReader() { reader -> localProperties.load(reader) } } flutterRoot = localProperties.getProperty() (flutterRoot == ) { GradleException() } flutterVersionCode = localProperties.getProperty() (flutterVersionCode == ) { flutterVersionCode = } flutterVersionName = localProperties.getProperty() (flutterVersionName == ) { flutterVersionName = } apply : apply : apply : $flutterRootandroid { compileSdkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = } sourceSets { main.java.srcDirs += } defaultConfig { applicationId minSdkVersion targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source } dependencies { implementation $kotlin_version}
android下的build.gradle
buildscript { ext.kotlin_version = repositories { maven { url } maven { url } maven { url } maven { url } google() mavenCentral() } dependencies { classpath classpath $kotlin_version} } allprojects { repositories { maven { url } maven { url } maven { url } maven { url } google() mavenCentral() } } rootProject.buildDir = subprojects { project.buildDir = ${rootProject.buildDir}${project.name}project.evaluationDependsOn() } task clean(: Delete) { delete rootProject.buildDir } #Fri Jun 23 08:50:38 CEST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 添加第三方dart插件 修改pubspec.yaml文件
name: flutter_app
description: A new Flutter application.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
english_words: ^3.0.1
dio: ^3.0.0
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
#english_words: ^3.0.1
#dio: ^3.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see
https://flutter.dev/custom-fonts/#from-packages
安装Flutter插件后不显示Flutter向导
可能未启用Android APK support插件
你可能改了Android studio配置需要安装插件后手动再解压zip安装
如何打包APK
创建一个用于上传的密钥库
在 macOS 或者 Linux 系统上,执行下面的代码:
keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload
在 Windows 系统上,执行下述代码:
keytool -genkey -v -keystore %userprofile%\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload
该命令将会把 upload-keystore.jks 文件储存在你的主文件夹中。如果你想要储存在其他地方,请通过指定 -keystore 传入参数。 注意,请保证这个文件的私有性,不要将它提交到公共的代码管理空间。
从 app 中引用密钥库
创建一个名为 [project]/android/key.properties 的文件,它包含了密钥库位置的定义:
content_copy
storePassword=<上一步骤中的密码>
keyPassword=<上一步骤中的密码>
keyAlias=upload
storeFile=<密钥库的位置,e.g. /Users/<用户名>/upload-keystore.jks>
提示
(再次)请保证这个文件的私有性,不要将它提交到公共的代码管理空间。
在 gradle 中配置签名
在以 release 模式下构建你的应用时,修改 [project]/android/app/build.gradle 文件,以通过 gradle 配置你的上传密钥。
在 android 代码块之前将你 properties 文件的密钥库信息添加进去:
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
...
}
将 key.properties 文件加载到 keystoreProperties 对象中。
找到 buildTypes 代码块:
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.debug
}
}
将其替换为我们的配置内容:
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
修改工程下gradle.build的内容
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
//google()
//mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
//google()
//mavenCentral()
}
}
现在我们 app 的发布版本就会被自动签名了。
最后在项目目录执行flutter build apk