Signing your application for release

Open your Messenger app project in Android Studio. Though not the only method of signing apps, Android Studio will be used in this chapter to sign the Messenger App. First things first, generate a private key for signing by running the following command in your Android Studio terminal:

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

Running the preceding command prompts you to input a keystore password as well as to provide additional information for your key. The keystore is then generated as a file called my-release-key.jks and saved in the current directory. The key contained in the keystone is valid for 10,000 days.

Now that we have generated a private key, we will configure Gradle to sign our APK. Open the module-level build.gradle file and add a signingConfigs {} block—within the android {} block—with entries for storeFile, storePassword, keyAlias, and keyPassword. Once that has been done, pass that object to the signingConfig property in your app's release build type. Take the following snippet as an example:

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.example.messenger"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}

signingConfigs {
release {
storeFile file("../my-release-key.jks")
storePassword "password"
keyAlias "my-alias"
keyPassword "password"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}

After doing the preceding, you are ready to sign your APK; before we do this, we must modify our current package name. The com.example package name is restricted by Google Play, and, as such, we must change our package name before we attempt to publish our application to the Play Store. Don't fret: changing the name of our app's root package is easy with Android Studio. Firstly, ensure you have set Android Studio to show the project directory structure. This can be done by clicking the dropdown toward the top left of the IDE window and selecting Project:

Once you have done the preceding, unhide all empty middle packages in the project structure view by deselecting the Hide Empty Middle Packages option in the project structure settings menu, as shown in the following screenshot:

After deselecting the preceding option, empty middle packages will no longer be hidden, and, as such, com.example.messenger will be split into three visible packages: com, example, and messenger. Let's rename the example package to something else. Change example to a name obtained from the combination of your first and last names. So, if your first name and surname are Kevin Fakande, the package name will be renamed from example to kevinfakande. A package can be renamed by right-clicking on it and selecting Refactor | Rename.... After your package has been renamed, check your manifest and build.gradle files to ensure that the modification to your project's package if reflected. Thus, wherever you see the com.example.messenger string in your build.gradle or manifest files, modify it to com.{full_name}.messenger.

Having made the preceding changes, you are ready to sign your application. Type the following command in your Android Studio terminal:

./gradlew assembleRelease

Running the preceding command creates a release APK that has been signed with your private key in the <project_name>/<module_name>/build/outputs/apk/release path. The APK will be named as <module_name>-release.apk. As our module in this project is named app, the APK in this case will be named app-release.apk. APKs that have been signed with a private key are ready for distribution. Having signed our APK, we are ready to finish the publication of the Messenger app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.81.43