How to package a project written with flutter into an apk file

One, app signature

This article is written for Xiaobai, and the great god is selective for reference

1. Create a keystore

   To be honest, whenever there is an article on the Internet that can solve a problem, I won't spend so much time writing this article. Next, I will take you step by step to step on the pit until the apk is packaged.
   If you have an existing keystore, please skip to the next step. If not, please create one by running the following command:

keytool -genkey -v -keystore D:\my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

  The following will explain what this code means, first of all you will see this thing if no surprises.

  Because keytool is in jdk, we need to go to a specific path to execute the above code. If there is no jdk, search and download it on the Internet by ourselves, and then my path is here: go
Insert picture description here
  back to the bin directory and hold down the shift mouse Right click and open the powershell window here.
Insert picture description here
  After entering the code just now again, you will see: The
Insert picture description here
  author directly adds .\ which is:

.\keytool -genkey -v -keystore  D:\my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

  Then, no surprise, you will see:
Insert picture description here
  Now explain what happened to the code just now:

  

  1. The value after the -keystore parameter: my-release-key.keystore, which represents the name of the signature file to be generated in a while; please save this name first, because it will be used during the release of the project later. (Note that the default path of the system is the c drive and you may not have permission, so I added a D:\, you can also change it to another drive, such as D:\my-release-key.keystore)
      

  2. The value after the -alias parameter: my-key-alias, which will also be used during the subsequent release of the project, so you need to save this parameter value; Note: The value of this parameter can be customized according to your needs.
      

  3. When running this command, you need to enter a series of parameters, and the password of the related password. Be sure to save the related password, which will be used later.
      

  4. After the signature is generated, the signature is saved to D:\my-release-key.keystore in your user directory by default.

2. Enter the key password

  Enter the key password twice, press Enter directly for the others, and finally press y to confirm, as shown in the figure: The
Insert picture description here
  above code should also have an enter, anyway, the last one is y.
  Then you will find the file my-release-key.keystore in the location you just set on the D drive.

3. Place the key

  Copy the key file just now to the android/app directory

  because I want to demonstrate to you, I dragged it directly to the desktop for convenience. This is my directory. Which project you want to package, you can find your own project. Note: Keep the keystore file private; do not add it to public source control (the best way is to add the keystore file to the .gitignore file.

4. Reference the keystore in the application

  Create a file named /android/key.properties, which contains a reference to the keystore: (Note: In the code below, the column to the right of the equal sign needs to be changed)

storePassword=password from previous step
keyPassword=password from previous step
keyAlias=key
storeFile=location of the key store file, e.g. /Users/<user name>/key.jks
  1. password from previous step, this is your key password
  2. key is the name of your key file just now, if you follow what I said, it is my-key-alias
  3. location of the key store file, eg /Users//key.jks, this is the entire file name my-release-key.keystore

5. Configure the signature in gradle. Configure the signature
  for your app by editing the /android/app/build.gradle file:

  1. replace:
android {

for:

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {

2. Replace:

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
    }
}

for:

signingConfigs {
    release {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile file(keystoreProperties['storeFile'])
        storePassword keystoreProperties['storePassword']
        }
}
buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}

  Be careful not to make a mistake, don't have one more parenthesis and one less parenthesis. The author is missing one parenthesis. Finally, the error is reported.
  At this step, all the configurations are ready, and finally the apk is packaged.

Two, package apk

  1. Go to your project directory and open the command window. For example, for my project, type in the command window: cd Desktop\myapp\apptwo to go to my project directory, and then type: flutter build apk. The exciting time has arrived:
    Insert picture description here
      why the author insisted To write this article, because there are too many pitfalls.
    Error: Unable to find git in your PATH. It means that you have not configured git environment variables.
      All of you here should have downloaded git. As a programmer must, if you don’t have one, go to the Internet to find a tutorial to download one, and then add the following path in the environment variable path: (Note: the location may not be below me This depends on where you install git)

  2. C:\Program Files (x86)\Git\bin

  3. C:\Program Files (x86)\Git\mingw64\libexec\git-core
    Then it still doesn’t work: then add another

  4. C:\Windows\System32

  5. Some people may ask whether to add user variables or system variables. You can add them to the system variables first. If not, add user variables as well. Or not? Open the command window and execute echo %PATH%, which should be effective, which means closing the cmd command window and reopening it.

  6. Finally, you can find the packaged apk in the directory: project location\build\app\outputs\apk\release.

This is my first article and I wrote it very carefully. Finally, I wish you all a happy Mid-Autumn Festival and a happy family!

Guess you like

Origin blog.csdn.net/weixin_43899542/article/details/100729443