title: Accessing Cloud Storage from Android
body: <p>I've been unable to find any concrete documentation on how to use Cloud Storage from an Android application.</p>
  
  <p>I did come across <a href="https://cloud.google.com/storage/docs/reference/libraries" rel="nofollow noreferrer">this client library</a> from the Google Cloud SDK, however have run into many, many issues and have yet to get it working. </p>
  
  <p>I added the following code as recommended in the above link:</p>
  
  <p><strong><em>build.gradle</em></strong>:</p>
  
  <pre><code>compile group: 'com.google.cloud', name: 'google-cloud-storage', version: '0.9.3-beta'
  </code></pre>
  
  <p>I then added some simple code, though this isn't really relevant to this question as I have yet been able to run my application with the above dependency added:</p>
  
  <p><strong><em>In an Activity:</em></strong></p>
  
  <pre><code>Storage storage = StorageOptions.getDefaultInstance().getService();
  Page&lt;Bucket&gt; buckets = storage.list();
  Iterator&lt;Bucket&gt; bucketIterator = buckets.iterateAll();
  while (bucketIterator.hasNext()) {
      Bucket bucket = bucketIterator.next();
      Log.d(TAG, "Bucket name: " + bucket.getName());
  }
  </code></pre>
  
  <p>After solving a myriad of dependency issues (Conflicts with Joda, Netty, <code>DuplicateFileException</code>'s from gradle, etc.) I was able to build the project, albeit with the below errors:</p>
  
  <p><code>Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for debug as it may be conflicting with the internal version provided by Android.</code>
  <code>Warning:WARNING: Dependency org.json:json:20151123 is ignored for debug as it may be conflicting with the internal version provided by Android.</code></p>
  
  <p>I can then try running, which will fail with a few hundred errors, most of which look something like the following:</p>
  
  <pre><code>Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
  Error:(com.google.inject.internal.cglib.reflect.$FastClassEmitter$3) that doesn't come with an
  Error:associated EnclosingMethod attribute. This class was probably produced by a
  Error:compiler that did not target the modern .class file format. The recommended
  Error:solution is to recompile the class from source, using an up-to-date compiler
  Error:and without specifying any "-target" type options. The consequence of ignoring
  Error:this warning is that reflective operations on this class will incorrectly
  Error:indicate that it is *not* an inner class.
  </code></pre>
  
  <p>After quite a few of those, with varying classnames, the end of the error contains this:</p>
  
  <blockquote>
    <p>trouble processing "javax/transaction/HeuristicCommitException.class":</p>
    
    <p>Ill-advised or mistaken usage of a core class (java.* or javax.*) when
    not building a core library.</p>
    
    <p>This is often due to inadvertently including a core library file in
    your application's project, when using an IDE (such as Eclipse). If
    you are sure you're not intentionally defining a core class, then this
    is the most likely explanation of what's going on.</p>
    
    <p>However, you might actually be trying to define a class in a core
    namespace, the source of which you may have taken, for example, from a
    non-Android virtual machine project. This will most assuredly not
    work. At a minimum, it jeopardizes the compatibility of your app with
    future versions of the platform. It is also often of questionable
    legality.</p>
    
    <p>If you really intend to build a core library -- which is only
    appropriate as part of creating a full virtual machine distribution,
    as opposed to compiling an application -- then use the
    "--core-library" option to suppress this error message.</p>
    
    <p>If you go ahead and use "--core-library" but are in fact building an
    application, then be forewarned that your application will still fail
    to build or run, at some point. Please be prepared for angry customers
    who find, for example, that your application ceases to function once
    they upgrade their operating system. You will be to blame for this
    problem.</p>
    
    <p>If you are legitimately using some code that happens to be in a core
    package, then the easiest safe alternative you have is to repackage
    that code. That is, move the classes in question into your own package
    namespace. This means that they will never be in conflict with core
    system classes. JarJar is a tool that may help you in this endeavor.
    If you find that you cannot do this, then that is an indication that
    the path you are on will ultimately lead to pain, suffering, grief,
    and lamentation.</p>
  </blockquote>
  
  <p>A few questions:</p>
  
  <ol>
  <li>Is this client library the correct way to access my Google Cloud Storage from my Android application?</li>
  <li>Is there a reason I shouldn't try to access Cloud Storage from the mobile application? For example, would a better architecture be to make a REST API call to my App Engine application (using Cloud Enpoints) and pass it media objects, then have the App Engine app access and store the media in Cloud Storage, before finally returning the result to the mobile app?</li>
  <li>If I am accessing Cloud Storage correctly, using the mentioned client library, what do these errors mean, and what it the fix?</li>
  </ol>