View Javadoc
1   package com.simpligility.maven.plugins.android.standalonemojos;
2   
3   import com.android.annotations.NonNull;
4   import com.google.api.client.http.FileContent;
5   import com.google.api.services.androidpublisher.AndroidPublisher;
6   import com.google.api.services.androidpublisher.model.Apk;
7   import com.google.api.services.androidpublisher.model.ApkListing;
8   import com.google.api.services.androidpublisher.model.Track;
9   import com.simpligility.maven.plugins.android.AbstractPublisherMojo;
10  import com.simpligility.maven.plugins.android.common.AndroidPublisherHelper;
11  
12  import org.apache.maven.plugin.MojoExecutionException;
13  import org.apache.maven.plugin.MojoFailureException;
14  import org.apache.maven.plugins.annotations.Mojo;
15  import org.apache.maven.plugins.annotations.Parameter;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import static com.simpligility.maven.plugins.android.common.AndroidExtension.APK;
23  
24  /**
25   *
26   * @author Joris de Groot
27   * @author Benoit Billington
28   */
29  @Mojo( name = "publish-apk", requiresProject = false )
30  public class PublishApkMojo extends AbstractPublisherMojo
31  {
32  
33      private static final int MAX_CHARS_WHATSNEW = 500;
34  
35      @Parameter( property = "android.publisher.track", defaultValue = "alpha" )
36      private String track;
37  
38      @Parameter( property = "android.publisher.apkpath" )
39      private File apkFile;
40  
41      @Parameter( property = "android.publisher.filename.whatsnew", defaultValue = "whatsnew.txt" )
42      private String fileNameWhatsnew;
43  
44      /**
45       *
46       * @throws org.apache.maven.plugin.MojoExecutionException
47       * @throws org.apache.maven.plugin.MojoFailureException
48       */
49      public void execute() throws MojoExecutionException, MojoFailureException
50      {
51          if ( apkFile == null )
52          {
53              apkFile = new File( targetDirectory, finalName + "-aligned." + APK );
54          }
55  
56          String packageName = extractPackageNameFromApk( apkFile );
57          getLog().debug( "Package name: " + packageName );
58  
59          initializePublisher( packageName );
60          publishApk( packageName );
61      }
62  
63      private void publishApk( @NonNull String packageName ) throws MojoExecutionException
64      {
65          try
66          {
67              getLog().info( "Starting upload of apk " + apkFile.getAbsolutePath() );
68              FileContent newApkFile = new FileContent( AndroidPublisherHelper.MIME_TYPE_APK, apkFile );
69              Apk apk = edits.apks().upload( packageName, editId, newApkFile ).execute();
70  
71              List<Integer> versionCodes  = new ArrayList<Integer>();
72              versionCodes.add( apk.getVersionCode() );
73              Track newTrack = new Track().setVersionCodes( versionCodes );
74              edits.tracks().update( packageName, editId, track, newTrack ).execute();
75  
76              publishWhatsNew( packageName, edits, editId, apk );
77  
78              edits.commit( packageName, editId ).execute();
79          }
80          catch ( Exception e )
81          {
82              throw new MojoExecutionException( e.getMessage(), e );
83          }
84      }
85  
86      private void publishWhatsNew( String packageName, AndroidPublisher.Edits edits, String editId, Apk apk )
87              throws IOException
88      {
89          warnPlatformDefaultEncoding();
90          
91          File[] localeDirs = getLocaleDirs();
92          if ( localeDirs == null )
93          {
94              return ;
95          }
96  
97          for ( File localeDir : localeDirs )
98          {
99              String recentChanges = readFileWithChecks( localeDir, fileNameWhatsnew,
100                     MAX_CHARS_WHATSNEW, "What's new texts are missing." );
101             if ( recentChanges == null )
102             {
103                 continue;
104             }
105             ApkListing newApkListing = new ApkListing().setRecentChanges( recentChanges );
106             edits.apklistings()
107                     .update( packageName, editId, apk.getVersionCode(), localeDir.getName(), newApkListing )
108                     .execute();
109         }
110     }
111 }