onsdag 17 april 2013

Android : Managing shared libraries / resources with IntelliJ

A quick tutorial for getting you up and running with sharing libraries & resources across your Android projects with IntelliJ.

Lets start with an example, lets use https://github.com/SimonVT/android-calendarview. It's the new CalendarView which is backported for older Android versions.

Download and place it somewhere:
C:\Android\shared-libs\android-calendarview-master\

Now, rename the library directory inside the folder to something more descriptive, like 'calendarview'.
C:\Android\shared-libs\android-calendarview-master\calendarview

IntelliJ uses modules so we can't use the Eclipse structure.

Step 1: Import the Project.

Choose 'Import Project...' in IntelliJ.

Create project from existing sources. It should auto-detect it's an Android project, so click 'Next' until the project is opened. Next we need to make it a library.


Go to 'File -> Project Structure', choose 'Facets' and make sure 'Library Module' is ticked.

I'd recommened to create a README.TXT or the like if you need to alter your styles in the project we are going to use it in. 

Android 4.0 CalendarView backported to 2.2 Probably has to be built against API level 15

To use this library, it's required that the 1 attribute is added to your theme. 

<resources>

    <style name="SampleTheme" parent="@android:style/Theme">
        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
    </style>

    <style name="SampleTheme.Light" parent="@android:style/Theme.Light">
        <item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>
    </style>
</resources>

Okay, the module is ready to imported and used.

Create a new Android application, after it's been created go to 'Project Structure'.
Choose 'Modules', click the green '+' icon and choose 'Import module', choose the .IML file.

Next switch to our own module, called 'TestApp', hit the right green '+'-icon and click on 'Module Dependency...' and choose 'calendarview'.

Now to see if everything works, create a themes.xml in /values

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme.Light" parent="@android:style/Theme.Light">
        <item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>
    </style>
</resources>

Be sure to add your custom theme to the activity in the AndroidManifest.xml.

Now we'll just add the custom view in our layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />
    
    <net.simonvt.calendarview.CalendarView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            />
</LinearLayout>

The module works, ready to be used and re-used in other projects! When creating a module as a 'Library' you can skip the hassle with copying the correct resources, values, etc to your project.