In this document

  1. Signing Overview
    1. Signing in Debug Mode
    2. Signing in Release Mode
    3. Signing Android Wear Apps
  2. Signing Your App in Android Studio
    1. Automatically Signing Your App
  3. Signing Your App with Android Studio
  4. Signing Considerations
  5. Securing Your Private Key
  6. Expiry of the Debug Certificate
  7. Signing Your App Manually

Signing Your Applications

Android requires that all apps be digitally signed with a certificate before they can be installed. Android uses this certificate to identify the author of an app, and the certificate does not need to be signed by a certificate authority. Android apps often use self-signed certificates. The app developer holds the certificate's private key.

Signing Overview


You can sign an app in debug or release mode. You sign your app in debug mode during development and in release mode when you are ready to distribute your app. The Android SDK generates a certificate to sign apps in debug mode. To sign apps in release mode, you need to generate your own certificate.

Signing in Debug Mode

In debug mode, you sign your app with a debug certificate generated by the Android SDK tools. This certificate has a private key with a known password, so you can run and debug your app without typing the password every time you make a change to your project.

Android Studio signs your app in debug mode automatically when you run or debug your project from the IDE.

You can run and debug an app signed in debug mode on the emulator and on devices connected to your development manchine through USB, but you cannot distribute an app signed in debug mode.

By default, the debug configuration uses a debug keystore, with a known password and a default key with a known password. The debug keystore is located in $HOME/.android/debug.keystore, and is created if not present. The debug build type is set to use this debug SigningConfig automatically.

For more information about how to build and run apps in debug mode, see Building and Running.

Signing in Release Mode

In release mode, you sign your app with your own certificate:

  1. Create a keystore. A keystore is a binary file that contains a set of private keys. You must keep your keystore in a safe and secure place.
  2. Create a private key. A private key represents the entity to be identified with the app, such as a person or a company.
  3. Add the signing configuration to the build file for the app module:

    ...
    android {
        ...
        defaultConfig { ... }
        signingConfigs {
            release {
                storeFile file("myreleasekey.keystore")
                storePassword "password"
                keyAlias "MyReleaseKey"
                keyPassword "password"
            }
        }
        buildTypes {
            release {
                ...
                signingConfig signingConfigs.release
            }
        }
    }
    ...
  4. Invoke the assembleRelease build task from Android Studio.

The package in app/build/apk/app-release.apk is now signed with your release key.

Note: Including the passwords for your release key and keystore inside the build file is not a good security practice. Alternatively, you can configure the build file to obtain these passwords from environment variables or have the build process prompt you for these passwords.

To obtain these passwords from environment variables:

storePassword System.getenv("KSTOREPWD")
keyPassword System.getenv("KEYPWD")

To have the build process prompt you for these passwords if you are invoking the build from the command line:

storePassword System.console().readLine("\nKeystore password: ")
keyPassword System.console().readLine("\nKey password: ")

After you complete this process, you can distribute your app and publish it on Google Play.

Warning: Keep your keystore and private key in a safe and secure place, and ensure that you have secure backups of them. If you publish an app to Google Play and then lose the key with which you signed your app, you will not be able to publish any updates to your app, since you must always sign all versions of your app with the same key.

The rest of this document provides detailed instructions about how to generate a private key and sign your apps in release mode with Android Studio.

Signing Android Wear Apps

When publishing Android Wear apps, you package the wearable app inside of a handheld app, because users cannot browse and install apps directly on the wearable. Both apps must be signed. For more information on packaging and signing Android Wear apps, see Packaging Wearable Apps.

Signing Your App in Android Studio


To sign your app in release mode in Android Studio, follow these steps:

  1. On the menu bar, click Build > Generate Signed APK.
  2. On the Generate Signed APK Wizard window, click Create new to create a new keystore.

    If you already have a keystore, go to step 4.

  3. On the New Key Store window, provide the required information as shown in figure 1.

    Your key should be valid for at least 25 years, so you can sign app updates with the same key through the lifespan of your app.

    Figure 1. Create a new keystore in Android Studio.

  4. On the Generate Signed APK Wizard window, select a keystore, a private key, and enter the passwords for both. Then click Next.

    Figure 2. Select a private key in Android Studio.

  5. On the next window, select a destination for the signed APK and click Finish.

    Figure 3. Generate a signed APK in Android Studio.

Automatically Signing Your App

In Android Studio, you can configure your project to sign your release APK automatically during the build process:

  1. On the project browser, right click on your app and select Open Module Settings.
  2. On the Project Structure window, select your app's module under Modules.
  3. Click on the Signing tab.
  4. Select your keystore file, enter a name for this signing configuration (as you may create more than one), and enter the required information.

    Figure 4. Create a signing configuration in Android Studio.

  5. Click on the Build Types tab.
  6. Select the release build.
  7. Under Signing Config, select the signing configuration you just created.

    Figure 5. Select a signing configuration in Android Studio.

  8. Click OK.

You can also specify your signing settings in Gradle configuration files. For more information, see Configuring Gradle Builds.

Signing Considerations


You should sign all of your apps with the same certificate throughout the expected lifespan of your applications. There are several reasons why you should do so:

  • App upgrade: When the system is installing an update to an app, it compares the certificate(s) in the new version with those in the existing version. The system allows the update if the certificates match. If you sign the new version with a different certificate, you must assign a different package name to the application—in this case, the user installs the new version as a completely new application.
  • App modularity: Android allows apps signed by the same certificate to run in the same process, if the applications so requests, so that the system treats them as a single application. In this way you can deploy your app in modules, and users can update each of the modules independently.
  • Code/data sharing through permissions: Android provides signature-based permissions enforcement, so that an app can expose functionality to another app that is signed with a specified certificate. By signing multiple apps with the same certificate and using signature-based permissions checks, your apps can share code and data in a secure manner.

If you plan to support upgrades for an app, ensure that your key has a validity period that exceeds the expected lifespan of that app. A validity period of 25 years or more is recommended. When your key's validity period expires, users will no longer be able to seamlessly upgrade to new versions of your application.

If you plan to publish your apps on Google Play, the key you use to sign these apps must have a validity period ending after 22 October 2033. Google Play enforces this requirement to ensure that users can seamlessly upgrade apps when new versions are available.

Securing Your Private Key


Maintaining the security of your private key is of critical importance, both to you and to the user. If you allow someone to use your key, or if you leave your keystore and passwords in an unsecured location such that a third-party could find and use them, your authoring identity and the trust of the user are compromised.

If a third party should manage to take your key without your knowledge or permission, that person could sign and distribute apps that maliciously replace your authentic apps or corrupt them. Such a person could also sign and distribute apps under your identity that attack other apps or the system itself, or corrupt or steal user data.

Your private key is required for signing all future versions of your app. If you lose or misplace your key, you will not be able to publish updates to your existing appn. You cannot regenerate a previously generated key.

Your reputation as a developer entity depends on your securing your private key properly, at all times, until the key is expired. Here are some tips for keeping your key secure:

  • Select strong passwords for the keystore and key.
  • Do not give or lend anyone your private key, and do not let unauthorized persons know your keystore and key passwords.
  • Keep the keystore file containing your private key in a safe, secure place.

In general, if you follow common-sense precautions when generating, using, and storing your key, it will remain secure.

Expiry of the Debug Certificate


The self-signed certificate used to sign your application in debug mode has an expiration date of 365 days from its creation date. When the certificate expires, you will get a build error.

To fix this problem, simply delete the debug.keystore file. The default storage location is in ~/.android/ on OS X and Linux, in C:\Documents and Settings\<user>\.android\ on Windows XP, and in C:\Users\<user>\.android\ on Windows Vista and Windows 7.

The next time you build, the build tools will regenerate a new keystore and debug key.

Note that, if your development machine is using a non-Gregorian locale, the build tools may erroneously generate an already-expired debug certificate, so that you get an error when trying to compile your application. For workaround information, see the troubleshooting topic I can't compile my app because the build tools generated an expired debug certificate.

Signing Your App Manually


You do not need Android Studio to sign your app. You can sign your app from the command line using standard tools from the Android SDK and the JDK. To sign an app in release mode from the command line:

  1. Generate a private key using keytool. For example:

    $ keytool -genkey -v -keystore my-release-key.keystore
    -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

    This example prompts you for passwords for the keystore and key, and to provide the Distinguished Name fields for your key. It then generates the keystore as a file called my-release-key.keystore. The keystore contains a single key, valid for 10000 days. The alias is a name that you will use later when signing your app.

  2. Compile your app in release mode to obtain an unsigned APK.

  3. Sign your app with your private key using jarsigner:

    $ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1
    -keystore my-release-key.keystore my_application.apk alias_name

    This example prompts you for passwords for the keystore and key. It then modifies the APK in-place to sign it. Note that you can sign an APK multiple times with different keys.

  4. Verify that your APK is signed. For example:

    $ jarsigner -verify -verbose -certs my_application.apk
  5. Align the final APK package using zipalign.

    $ zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk

    zipalign ensures that all uncompressed data starts with a particular byte alignment relative to the start of the file, which reduces the amount of RAM consumed by an app.

Signing Your Applications(Android签名相关)的更多相关文章

  1. android签名相关

    生成apk.keystore: 右键工程:Android tools——>Export Signed Application Package后,相册中截图: 一直下一步下一步,成功后jarsig ...

  2. android 签名相关

    查看keystorekeytool -list -v -keystore debug.keystoreapk签名不带别名 apksigner sign --ks debug.keystore test ...

  3. Android签名机制

    Android APK 签名比对 发布过Android应用的朋友们应该都知道,Android APK的发布是需要签名的.签名机制在Android应用和框架中有着十分重要的作用. 例如,Android系 ...

  4. Android签名总结

    signapk.jar与eclipse export插件默认赋予程序一个DEBUG权限的签名 signapk.jar包含有系统权限(system api, permission),而eclipse e ...

  5. Android签名详解(debug和release)

    1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包不被替换 2) 保证信息传输的完整性 签名对于包中的每个文 ...

  6. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  7. 【Mood-12】Android开发相关书籍推荐

    新年伊始,找到Android进阶干货若干,2015拜读. 1.Android应用UI设计模式 目前,谷歌Android操作系统在移动市场中风头正劲,并且未来发展势不可挡.<Android应用UI ...

  8. Android签名详解(debug和release)

    Android签名详解(debug和release)   1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...

  9. (转)Android签名详解(debug和release)

    1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包不被替换 2) 保证信息传输的完整性 签名对于包中的每个文 ...

随机推荐

  1. linux5.6以下版本的不兼容问题

    之前一直用的都是mysql5.6版本,最近突然使用到了mysql5.1版本,于是在导入数据的时候便出现了很多由于版本不兼容的问题. 1.mysql5.1没有datetime类型,所以对于时间类型,只能 ...

  2. 04 bash程序的基本特性

    我们知道Linux系统有两种操作方式,一种为GUI的图形界面化管理方式,其中图形程序常见的有Gnome.KDE.xfce:另一种管理方式就是就是GLI的命令行管理方式,而命令行的管理方式就是通过she ...

  3. scrapy框架之CrawlSpider操作

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  4. 《马哥出品高薪linux运维教程》wingkeung学习笔记-linux基础入门课程

    计算机原理概念: 1.CPU和内存中的存储单元通信线路称为总线(BUS),总线是被指令和数据复用的,所以也称为前端总线. 2.计算机中计算频率的时间标准即晶体振荡器原理,精确计算时间长度,根据相同的时 ...

  5. webstorm命令行无法使用node-gyp进行编译

    换成cmd命令即可:

  6. 小数据池 id

    1. 小数据池, id() 小数据池针对的是: int, str, bool 在py文件中几乎所有的字符串都会缓存. id() 查看变量的内存地址 # id()函数可以帮我们查看一个变量的内存地址 # ...

  7. [python] 初学python,打卡签到

    自学python第一周,学了变量和简单的条件判断. 附上猜数游戏代码 #Author:shijt trueAge=40 count=0 while count<3: guessAge=int(i ...

  8. #151: 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-x

    试题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...

  9. win7 数据源只有 SQL SERVER, WIN7 64bit 环境使用 access 作为 CIS的数据源

    最近换了个工作电脑,安装的是 WIN7 64BIT,结果配置CIS数据源的时候出现问题了,默认的数据源只有 SQL SERVER,没有ACCESS的数据源.后来在网上寻找了一圈后,找到了解决方法: C ...

  10. spring boot js 文件引用 单引问题。

    首页 index.html 访问首页空白 . F12 控制台看页面元素布局.发现有两个body 标签 ,而且 </head> 标红. 各种检查....好像是没有问题. 但就是访问空白.去掉 ...