Android

UnsupportedJavaRuntimeException.assertUsingVersion in Android Studio

After upgrading Android Studio to version 2.3, you may get:

Fatal Error: Unable to find package java.lang in classpath or bootclasspath

This error: Fatal Error: Unable to find package java.lang in classpath or bootclasspath can occur when /usr/lib/jvm/default-java is missing

This can be worked around with:

cd /usr/lib/jvm
sudo ln -s java-8-oracle default-java

Robolectric in gradle

Robolectric is a unit test framework that allows you to test Android code in a normal JVM without an emulator. There are several versions of gradle plugins that are supposed to allow you to run robolectric tests in a gradle project, but none worked out of the box for me.

Shared variables in Gradle's buildscript block

Gradle documentation on the internet has numerous examples of global variables that are defined at the top level of the file like this:

androidPluginVersion = '0.8.+'
buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath "com.android.tools.build:gradle:$androidPluginVersion"
  }
}

adb force install

If you get Failure [INSTALL_FAILED_ALREADY_EXISTS] or Failure [INSTALL_FAILED_VERSION_DOWNGRADE] when installing an .apk on your Android emulator, you can run:

adb install -r -d path/to/the-debug-unaligned.apk

to force downgrading and replacing of the existing .apk package.

Most compact way to store UUIDs in sqlite

If you want to store a UUID in sqlite and use the minimal possible amount of disk space, it seems like the best solution may be to represent each UUID as a 16 byte binary string and store it in a BLOB column.  I tested inserting 100,000 UUIDs into a table using three strategies with Sqlite 3.7.9, and the resulting file sizes were:

Using Eclipse with a Android project using the new gradle build system / project structure / layout

The Eclipse Android ADT plugin hard-codes paths for the AndroidManifest.xml file, the res directory and the gen directory.  If you migrate an Android project to the new Gradle project structure, it will stop working in Eclipse.  You can work around this by creating symbolic links from the new Gradle layout to the old locations expected by ADT.  For example, from the root of your project, if you have a android directory, you can run these commands:

What is Gradle? (comparison of gradle vs ant vs make)

Gradle is a build tool similar to make or ant which is being adopted by the Android project as a replacement for ant scripts and the automated "build" step in Eclipse.

There's a useful book about Gradle available online for free at http://www.gradleware.com/registered/books/building-and-testing/

Here is a comparison between gradle and two other commonly used build tools:

Android SQLiteOpenHelper onUpgrade()

It's not necessary to wrap your SQLiteOpenHelper.onUpgrade() code in beginTransaction() and endTransaction() pairs, because ever since the earliest version of SQLiteOpenHelper, the Android source code has done this for you:

Query the COUNT of rows in a SQLite table in Android

There's a function DatabaseUtils.queryNumEntries() which can be used to query SELECT COUNT(*) FROM table; in a SQLite database in Android.

Pages

Subscribe to RSS - Android