简体   繁体   English

如何从 Android Studio 为 Flutter 构建签名的 apk

[英]How to build signed apk from Android Studio for Flutter

Is there a way to build an apk for flutter in Android Studio?有没有办法在 Android Studio 中构建一个用于颤振的 apk?

I have seen this guideline: https://flutter.dev/docs/deployment/android我看过这个指南: https ://flutter.dev/docs/deployment/android
But here flutter console is used to build apk.但是这里flutter console是用来构建apk的。

You can build the Apk/AppBundle using IDE and command line.您可以使用 IDE 和命令行构建 Apk/AppBundle。

  • Building APK/AppBundle through IDE:通过 IDE 构建 APK/AppBundle:

    Step-1步骤1

    In Android Studio's tab bar, click on Tools and then Flutter and then Open Android module in Android Studio :在 Android Studio 的标签栏中,点击Tools然后Flutter然后Open Android module in Android Studio

    在此处输入图像描述

    Step-2第2步

    Open Project it in New Window:在新窗口中打开项目:

    在此处输入图像描述

    Step-3第三步

    Having opened the project, click on Build and then Generate Signed Bundle / APK ...打开项目后,点击Build然后Generate Signed Bundle / APK ...

    在此处输入图像描述


  • Building APK/AppBundle through command:通过命令构建 APK/AppBundle:

    Step-1:步骤1:

    Modify your build.gradle(app) file and include your key information there:修改您的build.gradle(app)文件并在其中包含您的关键信息:

     android { compileSdkVersion 31 signingConfigs { release { storeFile file("<path-to-keys.jks>") storePassword "********" keyAlias "<key-alias>" keyPassword "********" } } buildTypes { release { signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }

    Step-2:第2步:

    Build AppBundle:构建 AppBundle:

     flutter build appbundle --target-platform android-arm,android-arm64,android-x64 --obfuscate --split-debug-info=/<directory>

    Build APK:构建 APK:

     flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi --obfuscate --split-debug-info=/<directory>

For create/generate Signed APK using Android Studio version 3.5.1 for Flutter follow the following steps -要使用 Android Studio 3.5.1 版为 Flutter 创建/生成签名 APK,请执行以下步骤 -

Step 1 :步骤1 :

Go to in your project & then Tools -> Flutter -> Open for Editing in Android Studio as shown below转到您的项目,然后选择工具 -> Flutter -> 在 Android Studio 中打开以进行编辑,如下所示

在此处输入图像描述

Then Select New Window option as shown below然后选择新窗口选项,如下所示

在此处输入图像描述

Step 2 :第 2 步

Wait for while until project synchronization.等待一段时间,直到项目同步。 After that在那之后
Go to Build -> GenerateSigned Bundle/APK... option from menu bar as shown bellow从菜单栏中转到Build -> GenerateSigned Bundle/APK...选项,如下所示

在此处输入图像描述

Step 3 :第 3 步:

Select Android App Bundle or APK Option as per your need.根据需要选择Android App Bundle 或 APK选项。 (Android App Bundle is best solution) and click Next button. (Android App Bundle 是最好的解决方案)并单击Next按钮。

在此处输入图像描述

Step 4 :第4步 :

Select Create new.. option to generate new Signed key (When you release your app First Time)选择Create new..选项以生成新的签名密钥(当您首次发布应用程序时)

在此处输入图像描述

Step 5 :第 5 步:

Fill all options as follow填写所有选项如下

在此处输入图像描述

Note -笔记 -

Key store Path -密钥存储路径-

Path where your key store file ie .jks file stored (as shown in above image).您的密钥存储文件(即.jks文件)存储的路径(如上图所示)。 [Best way Select the path within your project directory.] [最佳方式选择项目目录中的路径。]

Key store password -密钥库密码-

Enter password eg 123456输入密码,例如123456

Key alias -密钥别名-

Enter Key alias (Name of .jks file) eg key输入密钥别名(.jks 文件的名称),例如密钥

Key Password -密钥密码-

Enter Key password (Choose different password than Key store password ) eg key123456输入密钥密码(选择与密钥存储密码不同的密码)例如key123456

Validity(years) - Keep it as it is or change as per your requirements.有效期(年) - 保持原样或根据您的要求进行更改。

Certificate -证书-

Fill certificate information (Not all fields are mandatory)填写证书信息(并非所有字段都是必填项)

Then click OK and you will get following screen...然后单击确定,您将看到以下屏幕...

Step 6 -第 6 步-

在此处输入图像描述

Step 7 -第 7 步-

Click Next button and you will get following screen...单击下一步按钮,您将看到以下屏幕...

在此处输入图像描述

Select options选择选项

Build variants - release and构建变体 - 发布

Signature versions both V1 and V2 respectively as shown above screen shot and click Finish button.签名版本分别为V1 和 V2 ,如上图所示,然后单击完成按钮。

Step 8 -第 8 步-

Wait for a while until Gradle Build Running process complete as shown below...等待一段时间,直到 Gradle Build Running 过程完成,如下所示...

在此处输入图像描述

and finally you will get the Generate Signed APK : (APKs) generated successfully .最后你会得到Generate Signed APK : (APKs) generated successfully from that click on Locate option to get Location of your Generate Signed APK Key .从中单击Locate选项以获取您的Generate Signed APK Key的位置。 as shown bellow.如下图所示。

在此处输入图像描述

That's it you generated Signed APK successfully for your Flutter project.至此,您为 Flutter 项目成功生成了 Signed APK。

IMPORTANT:重要的:

Please DO NOT lose the key and its all information ie Key store path,Key store password, Key alias and Key password (Best way write down it in to the note book or make text file and store on your drive while generating it.).请不要丢失密钥及其所有信息,即密钥存储路径、密钥存储密码、密钥别名和密钥密码(最好将其写入笔记本或制作文本文件并在生成时存储在驱动器上。)。 Without this, you won't be able to update your application because the new release will need to be signed with the same key.如果没有这个,您将无法更新您的应用程序,因为新版本需要使用相同的密钥进行签名。

I hope you will get detailed information.我希望你能得到详细的信息。

Instead of using Android Studios UI you could easily also just use a command prompt.除了使用 Android Studios UI,您还可以轻松地使用命令提示符。 This way was way easier for me, cause I had many issues creating a signed apk by using the ui.这种方式对我来说更容易,因为我在使用 ui 创建签名 apk 时遇到了很多问题。

Before you create the signed APK you must prepare your project.在创建签名的 APK 之前,您必须准备好您的项目。

1. Add Flutter automation dependencies to your pubspec.yaml 1. 将 Flutter 自动化依赖项添加到您的pubspec.yaml

dependencies:  
    flutter:  
        sdk: flutter  
    flutter_automation: ^1.1.4    

Currently ^1.1.4 is the newest dependency, this will eventually change.目前 ^1.1.4 是最新的依赖项,这最终会改变。

2. Get the new dependency 2.获取新的依赖

Run flutter packages get in your command prompt in your projects directoy在项目目录中的命令提示符下运行flutter packages get

3. Setup the signing 3.设置签名

Run flutter pub pub run flutter_automation --android-sign in your command prompt.Then you will be asked to input运行flutter pub pub run flutter_automation --android-sign在你的命令提示符下。然后你会被要求输入

  • a key alias,一个关键别名,
  • your certificate information ( CN=First and Last Name (??), OU=Organizational Unit, O=Organization, L=City or Locality, S=State or Province, C=Country Code (XX)您的证书信息(CN=First and Last Name (??), OU=Organizational Unit, O=Organization, L=City or Locality, S=State or Province, C=Country Code (XX)
  • a key password一个密钥密码
  • and a keystore password和密钥库密码

4. Generate your signed apk or bundle 4. 生成您签名的 apk 或捆绑包

Simply run flutter build apk --split-per-abi or flutter build appbundle to generate a signed apk/bundle of your app.只需运行flutter build apk --split-per-abiflutter build appbundle即可为您的应用生成签名的 apk/bundle。

For a FULL Solution that helps everyone, (including those that cant find "open for editing in android studio" option in the sdk) follow steps below..对于可以帮助所有人的完整解决方案(包括那些在 sdk 中找不到“在 android studio 中打开以进行编辑”选项的解决方案),请按照以下步骤操作。

  1. Lets say you have a project called "YourProjectName" ... Copy your project name in flutter as i have censored in my picture below, and right-click the android folder to create a new file.. The name on the new file must be "YourProjectName_android.iml"假设您有一个名为“YourProjectName”的项目...复制您的项目名称,就像我在下面的图片中审查的那样,然后右键单击android文件夹以创建一个新文件。新文件上的名称必须是“你的项目名称_android.iml” step1图片

  2. After creating the file in the format ive described in step 1, you will now fully see "Open for editing in android studio" option..以步骤 1 中描述的格式创建文件后,您现在将完全看到“在 android studio 中打开以进行编辑”选项。 在此处输入图像描述

  3. Now let it build after clicking the option in step 2 above, then you click the usual option of "Generate signed apk" as you see in the picture below现在让它在单击上面步骤 2 中的选项后构建,然后单击通常的选项“生成签名的 apk” ,如下图所示最后一步

As per DanyPata For flutter you have to set the signing file and then run flutter build APK.根据DanyPata对于颤振,您必须设置签名文件,然后运行颤振构建 APK。 It's quite important to run the flutter command so that all the changes/libs added in flutter are also added in the Android project.运行flutter命令非常重要,这样flutter中添加的所有更改/库也会添加到Android项目中。

Step 1步骤1

Go to File - Project Structure and select app under Modules .转到File - Project Structure并在Modules下选择app NOTE: It probably won't be called app - It may be under whatever name you gave it when creating the project.注意:它可能不会被称为app - 它可能是您在创建项目时给它的任何名称。 Select the Signing tab.选择Signing选项卡。

Android Studio 的项目结构窗口截图

You need to create a certificate so click the blue plus sign and fill in the blanks.您需要创建一个证书,因此单击蓝色加号并填写空白。 When you finish this process then you head over to the Build Types tab and make sure you have two - debug and release , if you don't have release then again press the blue plus sign and create it.完成此过程后,您将转到Build Types选项卡并确保您有两个 - debugrelease ,如果您没有release则再次按蓝色加号并创建它。

For a release build, you will want to select the Signing Config you previously created and leave the rest as default.对于发布版本,您将需要选择您之前创建的Signing Config并将其余部分保留为默认值。

Then to build your release APK go to Build on the top main bar, then Build Bundle(s) / APK(s) and select Build APK(s) .然后构建您的发布 APK 转到顶部主栏上的Build ,然后Build Bundle(s) / APK(s)并选择Build APK(s)

Keep a close eye on the Event Log to see when its complete and the path where APK was saved to.密切关注Event Log以查看其完成时间以及 APK 保存到的路径。

Step 2第2步

Update the gradle.build file.更新gradle.build文件。 Make sure your project visibility is set to something like Project so you can see all the files in the file tree.确保您的项目可见性设置为类似于Project的内容,以便您可以查看文件树中的所有文件。

Head down to yourapp/src then open build.gradle .前往yourapp/src然后打开build.gradle

Under, android add:下, android添加:

buildTypes {
    release {
        minifyEnabled false
        signingConfig signingConfigs.theNameForCertificateYouCreated
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

IMPORTANT : As a quick reminder and precaution, DO NOT lose the key it creates.重要提示:作为快速提醒和预防措施,请勿丢失它创建的密钥。 Without this, you won't be able to update your application because the new release will need to be signed with the same key.如果没有这个,您将无法更新您的应用程序,因为新版本需要使用相同的密钥进行签名。

Every times you run your app, it will generate apks.每次运行应用程序时,它都会生成 apk。

You can find it in your project files :您可以在项目文件中找到它:

yourProject\build\app\outputs\apk yourProject\build\app\outputs\apk


Don't use Android Studio key generation GUI不要使用 Android Studio 密钥生成 GUI

Don't use Android Studio key generation GUI不要使用 Android Studio 密钥生成 GUI

Don't use Android Studio key generation GUI不要使用 Android Studio 密钥生成 GUI


Just Running the following at the command line:只需在命令行运行以下命令:

On Mac/Linux, use the following command:在 Mac/Linux 上,使用以下命令:

  keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload

On Windows, use the following command:在 Windows 上,使用以下命令:

  keytool -genkey -v -keystore c:\Users\USER_NAME\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload

for next steps and publish on Google Play look at :有关后续步骤并在 Google Play 上发布,请查看:

I found a permanent solution我找到了一个永久的解决方案

Just open your existing flutter app directly from the "app directly" which contain the android files like Android Manifest directory directly in android studio.只需直接从“应用程序直接”打开您现有的颤振应用程序,其中包含直接在 android studio 中的 Android Manifest 目录等 android 文件。 This will load your project as an android project and i believe the rest is self explanatory what you need to do.这会将您的项目加载为一个 android 项目,我相信其余部分是不言自明的,您需要做什么。

add this code and save it you app and android.iml添加此代码并将其保存在您的应用程序和 android.iml

<?xml version="1.0" encoding="UTF-8"?>
    <module type="JAVA_MODULE" version="4">
      <component name="FacetManager">
        <facet type="android" name="Android">
          <configuration>
            <option name="ALLOW_USER_CONFIGURATION" value="false" />
            <option name="GEN_FOLDER_RELATIVE_PATH_APT" value="/gen" />
            <option name="GEN_FOLDER_RELATIVE_PATH_AIDL" value="/gen" />
            <option name="MANIFEST_FILE_RELATIVE_PATH" value="/app/src/main/AndroidManifest.xml" />
            <option name="RES_FOLDER_RELATIVE_PATH" value="/app/src/main/res" />
            <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/app/src/main/assets" />
            <option name="LIBS_FOLDER_RELATIVE_PATH" value="/app/src/main/libs" />
            <option name="PROGUARD_LOGS_FOLDER_RELATIVE_PATH" value="/app/src/main/proguard_logs" />
          </configuration>
        </facet>
      </component>
      <component name="NewModuleRootManager" inherit-compiler-output="true">
        <exclude-output />
        <content url="file://$MODULE_DIR$">
          <sourceFolder url="file://$MODULE_DIR$/app/src/main/java" isTestSource="false" />
          <sourceFolder url="file://$MODULE_DIR$/app/src/main/kotlin" isTestSource="false" />
          <sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
        </content>
        <orderEntry type="jdk" jdkName="Android API 29 Platform" jdkType="Android SDK" />
        <orderEntry type="sourceFolder" forTests="false" />
        <orderEntry type="library" name="Flutter for Android" level="project" />
        <orderEntry type="library" name="KotlinJavaRuntime" level="project" />
      </component>
    </module>

Instead of using Android Studios UI, you could easily also just use a command prompt.除了使用 Android Studios UI,您还可以轻松地使用命令提示符。

Follow the below steps to generate a signed apk in flutter using the command line.按照以下步骤使用命令行在 Flutter 中生成签名的 apk。

  • Step 1: Generate the keystone file第一步:生成keystone文件

    Running the following at the command line:在命令行运行以下命令:

    On Mac/Linux,在 Mac/Linux 上,

     keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload

    On Windows,在 Windows 上,

     keytool -genkey -v -keystore c:\Users\USER_NAME\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload
  • Step 2: Modify your build.gradle(app) file第 2 步:修改您的build.gradle(app)文件

    Include your key information on the build.gradle(app) file:build.gradle(app)文件中包含您的关键信息:

     android { compileSdkVersion 31 signingConfigs { release { storeFile file("<path-to-keys.jks>") storePassword "********" keyAlias "<key-alias>" keyPassword "********" } } buildTypes { release { signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
  • Step 3: Generate the apk第 3 步:生成 apk

    Run the following command from the flutter root directory using the command line.使用命令行从flutter根目录运行以下命令。

     flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi --obfuscate --split-debug-info=/<directory>

    Provide a valid directory path as split-debug-info提供一个有效的目录路径作为split-debug-info

    The build will be generated under the build\app\outputs\flutter-apk folder.构建将在build\app\outputs\flutter-apk文件夹下生成。

For more: https://docs.flutter.dev/deployment/android更多信息: https ://docs.flutter.dev/deployment/android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM