Advertisement
Guest User

Why app/build/outputs/apk folder deleted during config phase

a guest
Feb 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 9.01 KB | None | 0 0
  1. apply plugin: 'com.android.application'
  2. apply plugin: 'android-apt'
  3.  
  4. import org.apache.tools.ant.filters.*;
  5.  
  6. def AAVersion = '3.2'
  7.  
  8. Date.metaClass.'static'.fromString = { str ->
  9.     com.mdimension.jchronic.Chronic.parse(str).beginCalendar.time
  10. }
  11.  
  12. project.ext {
  13.     buildBranch = "NA"
  14.     buildDate = new Date()
  15.     buildGuid = "NA"
  16.     buildNumber = 9999
  17.     buildVersion = '1.0.0'
  18.     debugAPKFileSpec = "NA"
  19.     releaseAPKFileSpec = "NA"
  20. }
  21.  
  22. def getBuildBranch() {
  23.     if( System.getenv("BUILD_BRANCH") != null ) {
  24.         project.buildBranch = System.getenv("BUILD_BRANCH");
  25.     } else {
  26.         def stdout = new ByteArrayOutputStream()
  27.         exec{
  28.             commandLine('git','rev-parse','--abbrev-ref','HEAD');
  29.             standardOutput = stdout;
  30.         }
  31.         project.buildBranch = stdout.toString().trim();
  32.     }
  33.     return project.buildBranch;
  34. }
  35.  
  36. def getBuildDate() {
  37.     if( System.getenv("BUILD_DATE") != null ) {
  38.         project.buildDate = Date.fromString(System.getenv("BUILD_DATE"));
  39.     }
  40.     return project.buildDate;
  41. }
  42.  
  43. def getBuildGuid() {
  44.     if( System.getenv("BUILD_GUID") != null ) {
  45.         project.buildGuid = System.getenv("BUILD_GUID");
  46.     } else {
  47.         def stdout = new ByteArrayOutputStream()
  48.         exec{
  49.             commandLine("git", "rev-parse", "HEAD");
  50.             standardOutput = stdout;
  51.         }
  52.         project.buildGuid = stdout.toString().trim().substring(0,11);
  53.     }
  54.     return project.buildGuid;
  55. }
  56.  
  57. def getBuildKey() {
  58.     if( System.getenv("BUILD_KEY") != null ) {
  59.         project.buildGuid = System.getenv("BUILD_KEY");
  60.     } else {
  61.         project.buildGuid = InetAddress.getLocalHost().getHostName() + "-" + ((Date)getBuildDate()).time;
  62.     }
  63.     return project.buildGuid;
  64. }
  65.  
  66. def getBuildNumber() {
  67.     if( System.getenv("BUILD_NUMBER") != null ) {
  68.         project.buildNumber = Integer.parseInt(System.getenv("BUILD_NUMBER"));
  69.     }
  70.     return project.buildNumber;
  71. }
  72.  
  73. def getBuildVersion() {
  74.     if( System.getenv("BUILD_VERSION") != null ) {
  75.         project.buildVersion = System.getenv("BUILD_VERSION");
  76.     }
  77.     return project.buildVersion;
  78. }
  79.  
  80. buildscript {
  81.     repositories {
  82.         mavenCentral()
  83.         jcenter()
  84.     }
  85.     dependencies {
  86.         classpath 'com.android.tools.build:gradle:1.5.0'
  87.         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
  88.         classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2'
  89.  
  90.         // NOTE: Do not place your application dependencies here; they belong
  91.         // in the individual module build.gradle files
  92.     }
  93. }
  94.  
  95. apt {
  96.     arguments {
  97.         resourcePackageName 'com.scout.ui'
  98.         androidManifestFile variant.outputs[0].processResources.manifestFile
  99.     }
  100. }
  101.  
  102. android {
  103.     compileSdkVersion 23
  104.     buildToolsVersion "23.0.1"
  105.  
  106.     defaultConfig {
  107.         applicationId "com.scout.scoutandroidapp"
  108.         minSdkVersion 16
  109.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  110.         targetSdkVersion 23
  111.         versionCode getBuildNumber()
  112.         versionName getBuildVersion()
  113.     }
  114.  
  115.     signingConfigs {
  116.         release {
  117.             storeFile file("../keystore")
  118.             storePassword "STOREPASS"
  119.             keyAlias "KEYALIAS"
  120.             keyPassword "KEYPASS"
  121.         }
  122.     }
  123.  
  124.     buildTypes {
  125.         release {
  126.             minifyEnabled false
  127.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  128.             signingConfig signingConfigs.release
  129.         }
  130.         debug {
  131.             debuggable true
  132.         }
  133.     }
  134.  
  135.     applicationVariants.all { variant ->
  136.         variant.outputs.each { output ->
  137.             delete output.outputFile.parent;
  138.         }
  139.     }
  140.  
  141.     applicationVariants.all { variant ->
  142.         variant.outputs.each { output ->
  143.             output.outputFile = new File(
  144.                     output.outputFile.parent,
  145.                     output.outputFile.name.replace(".apk", "-${variant.versionName}.${variant.versionCode}.apk").replace("app-", "scout-android-app-")
  146.             )
  147.  
  148.             if(output.outputFile.name.toLowerCase().contains("-debug-")) {
  149.                 project.debugAPKFileSpec = output.outputFile.parent + "/" + output.outputFile.name;
  150.             }
  151.         }
  152.     }
  153. }
  154.  
  155. dependencies {
  156.     compile fileTree(dir: 'libs', include: ['*.jar'])
  157.  
  158.     apt "org.androidannotations:androidannotations:$AAVersion"
  159.     compile "org.androidannotations:androidannotations-api:$AAVersion"
  160.     compile 'com.android.support:support-v4:23.1.1'
  161.     compile 'com.android.support:appcompat-v7:23.1.1'
  162.     compile 'com.android.support:recyclerview-v7:23.1.1'
  163.     compile 'com.android.support:cardview-v7:23.1.1'
  164.     compile 'com.android.support:design:23.1.1'
  165.     compile 'com.squareup.retrofit:retrofit:1.9.0'
  166.     compile files('libs/ormlite-android-4.48.jar')
  167.     compile files('libs/ormlite-core-4.48.jar')
  168.     compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
  169.     compile 'de.greenrobot:eventbus:2.4.0'
  170.     compile 'net.hockeyapp.android:HockeySDK:3.5.0'
  171.     compile 'com.squareup.picasso:picasso:2.5.2'
  172.     compile 'de.hdodenhof:circleimageview:2.0.0'
  173.     compile('com.github.afollestad.material-dialogs:core:0.8.5.3@aar') {
  174.         transitive = true
  175.     }
  176.     compile('com.mikepenz:materialdrawer:4.6.4@aar') {
  177.         transitive = true
  178.     }
  179.  
  180.     androidTestCompile ('com.android.support.test:runner:0.4.1') {
  181.         exclude module: 'support-annotations'
  182.     }
  183.  
  184.     androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1') {
  185.         exclude module: 'support-annotations'
  186.     }
  187. }
  188.  
  189. task deleteBuildInfo(type: Delete) {
  190.     delete 'src/main/assets/build.json'
  191. }
  192.  
  193. task deleteReleaseNotes(type: Delete) {
  194.     delete 'HockeyAppDistributionReleaseNotes.txt'
  195. }
  196.  
  197. task updateBuildInfo(type: Copy, dependsOn: deleteBuildInfo) {
  198.     from '../build.json.sample'
  199.     into 'src/main/assets'
  200.     rename 'build.json.sample', 'build.json'
  201.     filter(ReplaceTokens, tokens: [
  202.             BUILD_BRANCH: getBuildBranch(),
  203.             BUILD_DATE: getBuildDate().getDateTimeString(),
  204.             BUILD_DIR: System.getenv('PWD'),
  205.             BUILD_GUID: getBuildGuid(),
  206.             BUILD_HOST: InetAddress.getLocalHost().getCanonicalHostName(),
  207.             BUILD_KEY: getBuildKey(),
  208.             BUILD_NUMBER: getBuildNumber().toString(),
  209.             BUILD_USER: System.getenv('USER'),
  210.             BUILD_VERSION: getBuildVersion()
  211.     ])
  212. }
  213.  
  214. task updateReleaseNotes(type: Copy, dependsOn: deleteReleaseNotes) {
  215.     from '../HockeyAppDistributionReleaseNotes.md.sample'
  216.     into '..'
  217.     rename 'HockeyAppDistributionReleaseNotes.md.sample',
  218.             'HockeyAppDistributionReleaseNotes.md'
  219.     filter(ReplaceTokens, tokens: [
  220.             BUILD_BRANCH: getBuildBranch(),
  221.             BUILD_DATE: getBuildDate().getDateTimeString(),
  222.             BUILD_DIR: System.getenv('PWD'),
  223.             BUILD_GUID: getBuildGuid(),
  224.             BUILD_HOST: InetAddress.getLocalHost().getCanonicalHostName(),
  225.             BUILD_KEY: getBuildKey(),
  226.             BUILD_NUMBER: getBuildNumber().toString(),
  227.             BUILD_USER: System.getenv('USER'),
  228.             BUILD_VERSION: getBuildVersion()
  229.     ])
  230. }
  231.  
  232. preBuild.dependsOn(['updateBuildInfo','updateReleaseNotes']);
  233.  
  234. task signJar << {
  235.     def cmdLine = "jarsigner -storepass 'STOREPASS' -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ~/.keystore " +
  236.         "./app/build/outputs/apk/scout-android-app-release-unsigned-1.0.0." + getBuildNumber() + ".apk " +
  237.         "-signedjar ./app/build/outputs/apk/scout-android-app-release-signed-1.0.0." + getBuildNumber() + ".apk KEYSTORE"
  238.     println ("signJar cmdLine: " + cmdLine)
  239.     exec {
  240.         executable "sh"
  241.         args "-c", cmdLine
  242.     }
  243. }
  244.  
  245. task alignJar << {
  246.     def cmdLine = "zipalign -v 4 ./app/build/outputs/apk/scout-android-app-release-signed-1.0.0." + getBuildNumber() + ".apk " +
  247.             "scout-android-app-release-signed-aligned-1.0.0." + getBuildNumber() + ".apk"
  248.     println ("alignJar cmdLine: " + cmdLine)
  249.     exec {
  250.         executable "sh"
  251.         args "-c", cmdLine
  252.     }
  253. }
  254.  
  255. task publishToHockeyApp {
  256.     doLast {
  257.         def puckCommandLine = "app_id='APIID' -api_token='APITOKEN' -submit='auto' -force='true' -download='true' -notify='true' -open='nothing' -notes_path='../HockeyAppDistributionReleaseNotes.md' -notes_type=markdown '$project.debugAPKFileSpec'";
  258.         println("puckCommandLine: " + puckCommandLine);
  259.         exec {
  260.             executable "sh"
  261.             args "-c", "/usr/local/bin/puck " + puckCommandLine
  262.         }
  263.     }
  264. }
  265.  
  266. //alignJar.dependsOn(['signJar']);
  267. //publishToHockeyApp.dependsOn(['alignJar']);
  268.  
  269. preBuild {
  270.     doLast {
  271.         println ("I am the prebuild task.");
  272.     }
  273. }
  274.  
  275. clean {
  276.     doFirst {
  277.         println ("I am the clean task. first");
  278.     }
  279.     println "I am clean task. middle"
  280.     doLast {
  281.         println ("I am the clean task. last");
  282.     }
  283. }
  284.  
  285. build {
  286.     doLast {
  287.         println ("I am the build tasks.");
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement