Use Android Studio to package the Android apk of uni-app; step-by-step tutorial and detailed pitfall avoidance

Uni-app offline packaging apk

1. Android Studio download

2. HBuilderX download

3. App offline SDK download

  • Android Offline SDK - Official Version

  • After downloading, unzip the file and renameHBuilder-Integrate-ASbuild-template a> and copy it to a folder specially used for packaging as a packaging template

  • There is a simpleDemo folder in this folder

    • Right nowbuild-template\simpleDemo\src\main\assets\apps

    • Delete the contents of the apps folder

    • Run HbuilderX

      image-20230513191744199

    • Choose to generate local packaged APP resources

    • After packaging is completed, the console will prompt the following content

      image-20230513191907918

    • Find the packaged resource directory and copy the folder below to the folder specially used for packaging, namelybuild-template\simpleDemo\src\main\assets\apps under the folder

      image-20230513192108596

4. Apply for Appkey

  • Install the JRE environment (JRE8 environment is recommended, you can skip it if you already have one)

    • You can download the jre installation package from Oracle official:

    • It is recommended to add the JRE installation path to the system environment variables

      image-20230513192813810

    • Use the keytool -genkey command to generate a certificate:

      • cmd Enter the following command to switch to the D drive

        d:  
        
        keytool -genkey -alias testalias -keyalg RSA -keysize 2048 -validity 36500 -keystore test.keystore
        
        • testalias is the certificate alias, which can be modified to the characters you want to set. It is recommended to use English letters and numbers.
        • test.keystore is the name of the certificate file. It can be modified to the file name you want to set, or you can specify the complete file path.
        • 36500 is the validity period of the certificate, which means 100 years of validity, in days. It is recommended to set the time longer to avoid certificate expiration.
      • After pressing Enter, you will be prompted:

        Enter keystore password:  //输入证书文件密码,输入完成回车  
        Re-enter new password:   //再次输入证书文件密码,输入完成回车  
        What is your first and last name?  
          [Unknown]:  //输入名字和姓氏,输入完成回车  
        What is the name of your organizational unit?  
          [Unknown]:  //输入组织单位名称,输入完成回车  
        What is the name of your organization?  
          [Unknown]:  //输入组织名称,输入完成回车  
        What is the name of your City or Locality?  
          [Unknown]:  //输入城市或区域名称,输入完成回车  
        What is the name of your State or Province?  
          [Unknown]:  //输入省/市/自治区名称,输入完成回车  
        What is the two-letter country code for this unit?  
          [Unknown]:  //输入国家/地区代号(两个字母),中国为CN,输入完成回车  
        Is CN=XX, OU=XX, O=XX, L=XX, ST=XX, C=XX correct?  
          [no]:  //确认上面输入的内容是否正确,输入y,回车  
        
        Enter key password for <testalias>  
                (RETURN if same as keystore password):  //确认证书密码与证书文件密码一样(HBuilder|HBuilderX要求这两个密码一致),直接回车就可以
        
      • After the above command is completed, the certificate will be generated, and the path is "D:\test.keystore"

    • View certificate information

      Run cmd under D drive

      keytool -list -v -keystore test.keystore  
      Enter keystore password: //输入密码,回车
      

      The following format information will be output:

      image-20230513193504383

      Only SHA1 and SHA256, but also requires an MD5

    • At this time, you need to download a openssl software and add it to the environment variable

      image-20230513193644829

      • After the installation is complete, execute the following command to see the output md5

        keytool -exportcert -keystore xxx.keystore | openssl dgst -md5
        
  • Record the SHA1 SHA256 MD5 generated above

    • image-20230513220140352
  • Login Developer Center

  • Create and view offline AppKey in Application Management - Click Application - Platform Information:

    • Enter the corresponding SHA1 SHA256 MD5 when creating

5. Configure Android Studio

  • build.gradle configuration

    signingConfigs {
          
          
            config {
          
          
                keyAlias '签名别名'
                keyPassword '签名密码'
                storeFile file('xxx/xxx/xxx.keystore或xxx/xxx/xxx.jks') // 路劲为相对路径或绝对路径
                storePassword '证书文件密码'
                v1SigningEnabled true //兼容v1
                v2SigningEnabled true //兼容v2
            }
    }
    
    buildTypes {
          
          
            debug {
          
          
                signingConfig signingConfigs.config
                ...
            }
            release {
          
          
                signingConfig signingConfigs.config
                ...
            }
    }
    
  • Note: The signature information needs to be consistent with the background. Inconsistency will also cause the appkey verification to fail.

    • The signature alias is the alias required when generating a certificate using the keytool tool, which is the testalias above.
    • The certificate signing password is the certificate password required when generating a certificate using the keytool tool.
    • Certificate file password is the certificate file password that is required when using the keytool tool to generate a certificate (press enter in that step, the certificate password is the same as the certificate file password)
    • storeFile is the location of the generated keystore file
  • Androidmanifest.xml configuration

    • After obtaining the AppKey of the android platform! Open the Android project, configure the Androidmanifest.xml file of the main APP, find the meta-data node, the name is dcloud_appkey, and the value is the applied AppKey as follows:

      	<application
              ...>
              <meta-data
                  android:name="dcloud_appkey"
                  android:value="替换为自己申请的Appkey" />
      
    • Find the data directory under the assets directory

      • Set the appid in dcloud_control.xml to the appid when applying for AppKey
        Insert image description here
      • Set applicationId in build.gradle to the package name when applying for AppKey
        Insert image description here
  • Set application name

    • Edit strings.xml in the values ​​directory under the res directory

      image-20230513223538252

    • Set to the name of the application

  • settings icon

    • In the drawable directory in the res directory
      • icon.png : logo picture
      • push.png: message push logo
      • splash.png: splash page
  • Set version number

    image-20230513232600631

6. Packing

image-20230513232631326

image-20230513232650940

image-20230513232738940

image-20230513232802788

After packaging is completed, you can view the packaged apk file in build-template\simpleDemo\release

End of full text

Guess you like

Origin blog.csdn.net/it_xcr/article/details/130663985