Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phynet/90cfcf464c28c80e5247 to your computer and use it in GitHub Desktop.
Save phynet/90cfcf464c28c80e5247 to your computer and use it in GitHub Desktop.
Install APK file using Gradle and Command Line

Install APK file using Gradle and Command Line

1.- First thing, install Gradle SDK. Download the latets version and install it in your root.

https://gradle.org/

2.- You'll need to have Android Studio installed also. I've downloaded the sdk and went to tools directory to start the android sdk (http://developer.android.com/sdk/installing/adding-packages.html )

$: android-sdk-macosx/tools

Later I run android sdk and installed all SDK tools needed

$: android sdk

3.- Configure bash_profile file: (update 2015/02/16: added last two lines to configure NPM for Jenkins automation)

export HOME=/Users/Myuser
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH_ANDROID=$PATH_ANDROID:$ANDROID_HOME:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$ANDROID_HOME/add-ons
export GRADLE_HOME=$HOME/gradle-2.2.1/bin
export PATH="$PATH_ANDROID/platform-tools:$GRADLE_HOME:$PATH"
export NPM_PATH=/usr/local/bin​
export PATH="$PATH_ANDROID/platform-tools:$GRADLE_HOME:$NPM_PATH:$PATH"

3.1.- Remember to refresh your .bash_profile file in the command line

$: source ~/.bash_profile

4.- Resign apk using keystore if you have private signature you will need to resign it using gradle (next step). The code above shows how gradle properies file looks like with your credentials.

Put this into ~/.gradle/gradle.properties

RELEASE_STORE_FILE={path to your keystore}
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****

signingConfigs {

       release {
           storeFile file(RELEASE_STORE_FILE)
           storePassword RELEASE_STORE_PASSWORD
           keyAlias RELEASE_KEY_ALIAS
           keyPassword RELEASE_KEY_PASSWORD
       }
    }
    
    buildTypes {
            release {
                signingConfig signingConfigs.release
            }
    }

4.-1 In my case, I didn't have the release key signing for the project I was testing (and uses it :[), so I just wrote this Regex to comment all release lines so the compiler can work using only debug ones in gradle. You can write a simple script to comment those line using this regex. NOTE: Remember to change YOUR-PROJECT-NAME-HERE for your project's name.

sed -i -e 's/\/\/storeFile file(project.property("android.signing.keyStore"))/storeFile file(project.property("android.signing.keyStore"))/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/\/\/keyPassword project.property("android.signing.keyPassword")/keyPassword project.property("android.signing.keyPassword")/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/\/\/keyAlias project.property("android.signing.keyAlias")/keyAlias project.property("android.signing.keyAlias")/g' $build_folder/YOUR-PROJECT-NAME-HEREBUZ/build.gradle
sed -i -e 's/\/\/storePassword project.property("android.signing.storePassword")/storePassword project.property("android.signing.storePassword")/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/\/\/signingConfig signingConfigs.release/signingConfig signingConfigs.release/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle


sed -i -e 's/storeFile file(project.property("android.signing.keyStore"))/\/\/storeFile file(project.property("android.signing.keyStore"))/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/keyPassword project.property("android.signing.keyPassword")/\/\/keyPassword project.property("android.signing.keyPassword")/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/keyAlias project.property("android.signing.keyAlias")/\/\/keyAlias project.property("android.signing.keyAlias")/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/storePassword project.property("android.signing.storePassword")/\/\/storePassword project.property("android.signing.storePassword")/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle
sed -i -e 's/signingConfig signingConfigs.release/\/\/signingConfig signingConfigs.release/g' $build_folder/YOUR-PROJECT-NAME-HERE/build.gradle

5.- Compiling the app using gradle. This step will do the resign also using info from step 4. Go to your project path and use the next command:

In Debug mode:
gradle assembleDebug

In Release mode:
gradle assembleRelease

6.- Now, to start your emulator with:

  • Dalvik: use emulator's command to start dalvik (obviously you have to have the device configured. You don't have it configured? go to Notes above).

       emulator -avd Nexus-5
    
  • Genymotion: You can also use Genymotion, which is faster. Just start it by:

       player --vm-name <VM id | VM name>
    

You can find VM id and VM using (those are virtual machines, you will need virtual box. I'm assuming you already know this.)

   VBoxManage list vms

I particulary use this command for genymotion

/Applications/Genymotion.app/Contents/MacOS/player --vm-name "2-Motorola Moto X - 4.3 - API 18 - 720x1280_1" {1ad66bb8-712a-4c17-9624-7e5e28dad22c}

7.- Installing the apk.

Remember that apk's folder is located in

cd my-app-name/build/outputs/apk/

Now run install APK in your emulator or device: (note that you will need to connect your device and close dalvik or genymotion process in order to run in device)

 adb install <1>/<2>.apk

Replace <1> with the path to your APK file and replace <2> with the name of the APK file.

Having troubles installing? Use:

adb uninstall com.mypackgage.something

Notes:

Starting avd

android avd

That's it. The most difficult part is to have android sdk and gradle sdk configured. Remember that is important to have your ENV well configured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment