Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio, Gradle: How to automatically publish release.apk to svn repository?

In Maven, a release process works without problems. Since I was forced to use Android Studio with Gradle, I want to achieve the same behaviour as I had with Eclipse and Maven. I found some quite good Maven-like release plugin (https://github.com/researchgate/gradle-release), it almost works as expected, but it does not upload the release apk, although it correctly creates the apk in /app/build/outputs/apk and a tag with the source code on my SVN repository. The release folder is still empty. I'm not even sure if this plugin can do that, because it's not very well documented but I guess I need to tell this plugin what to upload and where to find it. There is only a afterReleaseBuild.dependsOn uploadArchives. What does this mean, that I have to override this uploadArchives method or whatever this means in Gradle? It seems to be something built-in because Gradle build does not complain if I write this in my script and run gradlew release. Obviously the plugin already knows the repository and can connect via ssh, otherwise it could not create the tag, but it seems that it does not know what to upload, the release folders stays empty. Meanwhile I tried to solve that with a lot of other Gradle attempts, e.g. with the Maven plugin, but then I run into SSH cert problems without a solution, whereby even if it works, it wouldn't be a good solution, because I don't want to create pom.xml files or so on, it would have been just a workaround.

The target is that the release process should automatically create a folder in the svn repository's releases folder which is named like the current version (as it already works in tags folder, e.g. 1.0.0) and copy the released apk into it which was already renamed to myapp-app-1.0.0-release.apk by Gradle, since this is the name of the package in the local output folder.

Here is my current gradle script which can generate the release apk in the output folder but does not upload it to svn:

apply plugin: 'com.android.application'
apply plugin: 'net.researchgate.release'

def keystorePropertiesFile = rootProject.file("/home/myuser/.android/keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    lintOptions {
        //TODO remove this or set to true
        abortOnError false
    }

    signingConfigs {
        myapp_release_config {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    packagingOptions {
        exclude 'META-INF/ASL2.0'
    }

    defaultConfig {
        applicationId "com.mydomain.myapp"
        minSdkVersion 9
        targetSdkVersion 24
        versionCode 1
        versionName '1.0.0'
        archivesBaseName = "myapp-app-$versionName"
    }
    buildTypes {
        debug {
            debuggable true
            zipAlignEnabled false
        }
        release {
            minifyEnabled true
            proguardFiles 'proguard-rules.pro'
            signingConfig signingConfigs.myapp_release_config
            afterReleaseBuild.dependsOn uploadArchives
        }
    }
}

release {
    failOnUnversionedFiles = false

    svn {
        username = 'myuser'
        password = 'mypassword'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.mydomain.myapp:model:1.0'
    //support-v4
    compile 'com.android.support:support-core-utils:24.2.0'
    compile 'com.android.support:support-fragment:24.2.0'
    //compile 'com.android.support:multidex:1.0.0'
    //support-design
    compile 'com.android.support:design:24.2.0'
    //support-v7
    compile 'com.android.support:appcompat-v7:24.2.0'
    //FlowLayout
    compile 'com.wefika:flowlayout:0.4.1'
    compile 'javax.validation:validation-api:1.1.0.Final'
    compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'

    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
}
like image 810
Bevor Avatar asked Sep 07 '16 19:09

Bevor


1 Answers

You can add a task to your build.gradle

This is a sample task and bash script for publishing:

task publishit (type: Exec) {
    commandLine './publish.sh', android.defaultConfig.versionName
}
assembleRelease.finalizedBy(publishit)

and put publish.sh in your app path (and make it executable)

#!/bin/bash
 
### Settings
svn_path='/path/to/repo/'
svn_user='user'
svn_password='password'
app_prefix='myapp-app-'
app_suffix='-release.apk'
release_name='app-release.apk'
dir='/'

current_path=`pwd`
release_path=$current_path$dir$release_name
code_name=$1
app_path=$svn_path$code_name$dir$app_prefix$code_name$app_suffix

cd $svn_path
mkdir $code_name
cp $release_path $app_path

# check and fix these:
svn add $code_name
svn update
svn commit -m "new version" --username $svn_user --password $svn_passwd > publish_log

You can do it with gradle syntax, for more info read here

like image 81
ADev Avatar answered Nov 15 '22 09:11

ADev