Wednesday, March 30, 2016

Sign Android apk using gradle.


1. With Android Studio create a new release key.
***Save android.kesytore in a safe place and don't forget the password.

2. Put this into ~/.gradle/gradle.properties
RELEASE_STORE_FILE=/../PathToYour/keystore
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****
Modify your build.gradle like this: 
...
signingConfigs {

   release {
       storeFile file(RELEASE_STORE_FILE)
       storePassword RELEASE_STORE_PASSWORD
       keyAlias RELEASE_KEY_ALIAS
       keyPassword RELEASE_KEY_PASSWORD
   }
}

buildTypes {
        release {
            signingConfig signingConfigs.release
        }
}
....

3. Then in terminal run: $./gradlew assembleRelease
4. Done. Now you have a signed apk that is ready to publish to the android play store.
$cd MyApp/app/build/outputs/apk/app-release.apk


Check who sign the apk with keystone info:
$jarsigner -verify -verbose -certs myApp.apk

Resources:

Friday, March 25, 2016

Android Gradle: How to rename apk depending on build type

1. Add the code below to your project app/build.gradle:
android {
    signingConfigs {
        release {
           .....
        }
    }
    def jenkinsBuild = System.getenv("BUILD_NUMBER") ?: "10"   
    defaultConfig {
        .....
        versionCode jenkinsBuild.toInteger()
        versionName "2.0.0."+ versionCode    }
    buildTypes {
        release {
           ...
        }
        qa {
            ..
        }
        debug {
           ...
        }
    }
    applicationVariants.all { variant ->
        if (variant.buildType.name == 'release') {
            variant.mergedFlavor.versionName = android.defaultConfig.versionName;
        }
        if (variant.buildType.name == 'qa') {
            variant.mergedFlavor.versionName = android.defaultConfig.versionName;
        }
        if (variant.buildType.name == 'debug') {
            variant.mergedFlavor.versionName = android.defaultConfig.versionName;
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File( output.outputFile.parent,output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
   ..
}


2. $gradlew build
It will generate all  the apk's for build types (shown below).


Now when you push apps from Jenkins to HockeyApp the 'apk name' will help you identify the build of the apk :)

Friday, March 18, 2016

Cool Android Libraries

MVP

Design Patter: Model View Controller
Good tutorial
uncle bob clean architecture
tutorial

Renderers

Useful to reduce boilerplate code in ListView and RecyclerView
Renderers
code

ORM

ActiveAndroid

ActiveAndroid is an active record style ORM ( object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to save and retrieve SQLite database records without ever writing a single SQL statement. Each database record is wrapped neatly into a class with methods like save() anddelete().
ActiveAndroid does so much more than this though. Accessing the database is a hassle, to say the least, in Android. ActiveAndroid takes care of all the setup and messy stuff, and all with just a few simple steps of configuration.

Cool Android Studio trick, wifi adb

AndroidWiFiADB 

adb wifi  

connect 

$adb tcpip 555

$adb connect #.#.#.# (device I.P.)

Monday, March 14, 2016

Android Versioning your App

Every time you submit your app to the Google Play Market you have to increment android:versionCode by 1, this field is located in your Android Studio project app/build.gradle. If you don't increment the versionCode you can't update your app in the Market. This is the value the app checks to perform an app update. Also in app/build.gradle we have android:versionName this is the string user sees for app version, it can be any String that specifies your app version.



1. Edit app/build.gradle:
android {
    compileSdkVersion 23    
    buildToolsVersion "23.0.1"
    def jenkinsBuild = System.getenv("BUILD_NUMBER") ?: "10"
    defaultConfig {
        applicationId "andres_sjsu.notegit"        
        minSdkVersion 16        
        targetSdkVersion 23        
        versionCode jenkinsBuild.toInteger()
        versionName "1."+ versionCode    
}

2. Commit your code to Github.
3. Kick a build from Jenkins.
4. Now the apk will have the versionCode injected by Jenkins build #. Example is your Jenkins project build is number 118 the versionCode will be 118 (Jenkins build number increments by one, which is perfect since every time you publish to Google Play Market the developer needs to increment the versionCode by one, otherwise the Play Market will not let you update your apk).
5. To verify the versionCode was incremented correctly, you can use the buid-tool aapt
$./aapt dump badging /Users/andres/Downloads/app-debug2.apk
It will output info about Android package, example below:


You can see the results are as expected:
versionCode: 118 (Jenkins project build #)
versionName: 1.118



TODO: add versionName to apk name, this will help to easier see the app user(QA, Dev,..) installs.
Example of app Name: qa-go90_1.0.12.apk, release-go90_1.0.12.apk, etc...

FYI:
aapt location is in your android/build-tools

ex: 
$cd /Applications/adt-bundle-mac-x86_64-20131030/sdk/build-tools/android-4.4
$./aapt d badging myapk.apk | grep package

Output
TUSCA09TMLVT029:23.0.2 v644084$ ./aapt d badging /Users/Desktop/base.apk | grep package
 
package: name='com.myPackageName.release' 
versionCode='97040' 
versionName='1.7.0' 
platformBuildVersionName='6.0-2166767' 

Resources:
http://developer.android.com/intl/es/tools/publishing/versioning.html
Configure gradle: https://developer.android.com/intl/es/tools/building/configuring-gradle.html
good read:* http://www.androidshortcuts.com/tags/gradle/
http://eric-liang.com/category/android-app-development/

Wednesday, March 9, 2016

Fix Jenkins error for Android build, missing sdk #.#.#

Error: failed to find Build Tools revision 23.0.2


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> failed to find Build Tools revision 23.0.2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Solution:


$android list sdk -a
Which showed me the following list:
1- Android SDK Tools, revision 24.0.2
2- Android SDK Platform-tools, revision 23.0.2
... and a great many more
Followed by the command:
$android update sdk -a -u -t 2
It will install the 23.0.2 SDK Platform-tools components. DONE. Run Build again in Jenkins.


Example below:

Andress-MacBook-Pro:tools andres$ pwd
/Users/Shared/Jenkins/Home/tools/android-sdk/tools

Andress-MacBook-Pro:tools andres$ ls
android ....  .....
....  ...   ...
.... ... ...

Andress-MacBook-Pro:tools andres$ ./android list sdk -a
Refresh Sources:
  Fetching https://dl.google.com/android/repository/addons_list-2.xml
  Fetched Add-ons List successfully
  Refresh Sources
  ...
  Parse XML:    https://s3.amazonaws.com/android-sdk-manager/redist/addon.xml
Packages available for installation or update: 170
   1- Android SDK Tools, revision 24.4.1
   2- Android SDK Tools, revision 25.0.9 rc10
   3- Android SDK Platform-tools, revision 24 rc1
   4- Android SDK Build-tools, revision 24 rc1
   5- Android SDK Build-tools, revision 23.0.2
   6- Android SDK Build-tools, revision 23.0.1

Andress-MacBook-Pro:tools andres$ sudo ./android update sdk -a -u -t 5

Reference:
http://stackoverflow.com/questions/27272605/failed-to-find-build-tools-revision-21-1-1-sdk-up-to-date