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:

Now, rename the library directory inside the folder to something more descriptive, like '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. 


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

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

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"?>
    <style name="MyTheme.Light" parent="@android:style/Theme.Light">
        <item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>

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:text="Hello World, MyActivity"

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.

Inga kommentarer:

Skicka en kommentar