Category Archives: Android

IntelliJ Android – Build a library project – jar file

In order to build a jar file from a project,

1. Open IntelliJ Module Settings

2. Navigate to “Artifacts” on the left most tab

3. In the Details section (right most section), be sure to specify an output directory for the jar

4. Rebuild the project. Select Build -> Build Artifacts -> Rebuild.


Settings: Show Layout Bounds

While implementing a complex UI layout in Android, it is very useful to get a good picture of the bounds (the border) of different components within the UI. For instance, if a relative layout’s position is modified programmatically, getting the bounds of the relative layout is useful. This helps in figuring out if a layout’s final position in the screen is in harmony with the expected position of the layout in the UI design. This also gives a good visual indicator of the distance between components when (say) RelativeLayout.below is specified.

In order to do this, turn on developer mode in Android. Then select Settings -> Developer Options -> Show layout bounds. This will draw blue rectangles on every UI component for all applications.

Spin up the file chooser UI and select an image.

source: Android SDK Sample code
* Fires an intent to spin up the “file chooser” UI and select an image.
public void performFileSearch() {

// BEGIN_INCLUDE (use_open_document_intent)
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system’s file browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

// Filter to only show results that can be “opened”, such as a file (as opposed to a list
// of contacts or timezones)

// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be “audio/ogg”.
// To search for all documents available via installed storage providers, it would be
// “*/*”.

startActivityForResult(intent, READ_REQUEST_CODE);
// END_INCLUDE (use_open_document_intent)

Viewing Contextual Android source code from the Developer documentation – Chrome plug in

If you want to look at Android source code in a contextual manner, use these 2 Chrome plug-ins:
AD (Android SDK Search) and ARN (Android resource navigator).

After installing these,

  • Open Chrome
  • Type AD followed by Class (say) TextView
  • The android developer site’s TextView page opens.

Notice the “View Source” link right next to the TextView title. Click on it to go to the class implementation.
Follow similar steps to look at resource source code. The beauty of this is that the tool will navigate you to whichever repo the source code is located in – you don’t have to do this yourself.

I found this when trying to see how they implemented the InputFilter class for the EditText component.

Dx trouble writing output: Too many fields: Conversion to Dalvik format failed with error 2

Android compiles source code into dex files which is executed by Dalvik. While integrating a new library project into the maincode base, I got the above build time error.

While researching the problem, here are some things I came across:

1. Each Android dex file can contain a max of 65, 536 method references (i.e.) a method reference count is incremented by 1, whenever there is a unique reference to a method from an Android project.

2. If a project grows big enough to contain more than ~65K method references, the library files can be built as separate dexes.

3. Dex files can be dynamically loaded in the main activity’s onCreate method. See example.  

4. Even if 2 Android library projects referenced the same Android project, the method reference count is counted twice – provided the 2 Android library projects have different manifest package names (i.e.) package name defined in the manifest node in AndroidManifest.xml file.  

To provide an example of #4 above, see portions of the dex build error stack trace below that caught my attention. It indicates the method reference count for these library projects – all hovering close to the number 6852. The only common library/jar reference in all these library projects referenced the android framework library itself.

Error:Android Dex: [MainProject] 6852

Error:Android Dex: [MainProject] 6964 com.handmark.pulltorefresh.library

Error:Android Dex: [MainProject] 6852


and so on…..

Given the above, some potential work-arounds emerged:

1. Do away with the need for library projects. Package everything into one monolithic structure.

2. Split the project files into multiple dex files.

#1 goes against fundamental software principles of software development – code needs to be developed as re-usable libraries.

#2 is a good long-term solution but is more complex.

The solution: A reasonable medium term solution emerged after understanding #4 more closely. The significance of Package name within the manifest tag in the AndroidManifest.xml file is that it is used as a fully qualifying name to identify Android resources. It does not have anything to do with the package name for the java classes themselves. So I tweaked 1 manifest package name (new library project) to be the same as the top level project (MainProject) manifest package name. And the dex build error was gone!

The following command returns the count of methods in a dex file:


    dexdump -f product.apk | grep method_ids_size

No Resources found that matches the given name

The error in the title is a very common error encountered in Android. There are several common reasons why this could happen. For example,

1. The resource is really missing from (say) a string resource file.

2. The SDK target is set to a version below the required version etc.

However, there is another common reason why this error occurs in IntelliJ – because the dependent or library project is *not* marked as a library project, in addition to including it in the list of module dependencies. This happens frequently while importing existing Android projects into IntelliJ. To resolve this issue, first check if the dependent project is a library project as follows:

1. Open Module Settings in IntelliJ

2. Select the ‘Android Icon’ below the library module.

3. Mark the “Library module” check box selected. Select Save.

Without step #3, Intellij will NOT see resources from a library project, even if the library project has been marked as a dependent module in the main project.