Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jorgevila/39ee3cb5a1bcf7936d87 to your computer and use it in GitHub Desktop.
Save jorgevila/39ee3cb5a1bcf7936d87 to your computer and use it in GitHub Desktop.

###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.

3.- Configure bash_profile file

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"

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, 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

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

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.- Install the apk. Remember that apk's folder is located in

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

Now run install APK in your emulator:

adb install my-app-name/build/outputs/apk/my-app.apk

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