Click here to Skip to main content
15,891,033 members
Articles / Mobile Apps / Android

Time to Migrate Android Projects to Android Studio

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Jul 2015CPOL5 min read 7.7K   1  
Time to migrate Android projects to Android Studio

Last week, Android Developers Blog posted an article – An update on Eclipse Android Developer Tools. It announced that Google will end development and official support for the Android Developer Tools (ADT) in Eclipse at the end of the year. Many developers have been using Eclipse to develop Android applications with ADT for many years. Someone may be reluctant to get started with a new IDE from scratch, though, it is worth spending time on the new tool if it is more powerful and efficient than Eclipse. As Google said, they will deliver a great experience on a unified development environment and make developers productive. Since I’ve been playing Android Studio for a few days, I’d like to share my experiences.

Android Studio Installation

I often see some developers asking questions about their development environment issues on StackOverflow, such as Eclipse boot exception, Java virtual machine exception, Android Studio cannot work, etc. How to fix these problems quickly? To save time, just download the bundle that contains both Android IDE and SDK, unless you are interested in configuring environment manually. Here is the download link of Android Studio.

Import Eclipse ADT Projects to Android Studio

Assume you’d like to embrace Android Studio, you want to know how to import your existing projects to the new IDE. Let’s do it with the following steps:

  1. In Android Studio, click File->New->New Projects.
  2. Specify the path of your Android project and click Next.
  3. Check all options and click Finish.

    ADT options

  4. It is not done yet. The IDE shows me an alert to set the output path.

    output dir

  5. After specifying the output path, I could successfully build the project.

Android Studio Shortcut Keys

While using an IDE, to write and edit code quickly and efficiently, developers always rely on some useful shortcut keys. When you start up Android Studio, you will see a tips window that assists you to get familiar with the new environment. Here is the list of shortcuts that I feel somewhat useful:

  • Ctrl+ALT+Shift+N: Search Globally for any symbol
  • Ctrl+N: Search Globally for any Class
  • Ctrl+Shift+A: Find menu command or toolbar action
  • Alt+F7: Find variable usages
  • Ctrl+ALT+Shift+S: Open Project Structure
  • Ctrl+Shift+Enter: Create braces for parenthesis
  • Alt+Enter: Invoke a quick fix or intention action
  • Ctrl+Alt+T: Surround with e.g. try/catch, if/else
  • Shift+Shift: Search through the classes, files, tool windows, actions, settings, and symbols of your project

Most of shortcut keys are totally new to Eclipse users. It takes time to get used to it.

First Android Studio Project

We can create a new project to see how different Android Studio is compared to Eclipse.

Project Wizard

Click File->New->New Project:

Android Studio new project

Android Studio can help you select the target devices.

Android Studio target device

Pick one Activity template you would like to use.

Activity template

How do you like it? I feel the project wizard is more user-friendly and intelligent than Eclipse. Besides, it is pretty convenient to import any sample code for learning. You just need to click File->Samples. Then search and get the code from GitHub.

Import GitHub sample

Building Android Apps with Gradle

Gradle is an excellent cross-platform project building tool. With Gradle, Android Studio can flexibly make build configurations for projects.

Open AndroidManifest.xml, can you find out any differences?

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dynamsoft.com.demo" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The SDK version information is now in build.gradle.

XML
apply plug in: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "dynamsoft.com.demo"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

Android UI Editor

In Android Studio, we can quickly design the user interface by dragging UI elements from the palette.

Drag UI element

When switching to text mode, you can write code and see the real-time changes.

Android UI editor

Creating Custom View

When creating a custom view in Eclipse, we have to create a Class file and a corresponding layout file ourselves. Sometimes, we may make some mistakes to cause exceptions. Using Android Studio, it is very easy. You just need to right-click on your project root and select New -> UI Component -> Custom View.

Android custom view

Once it is done, Android Studio will generate an XML file and a Java file for custom view.

How to Generate Signed Apk in Android Studio

If you want to publish your Android apps, you have to create signed application package. Here are the basic steps:

  1. Click menu Build and select Generate Signed APK.

    generate signed apk

  2. Specify a key file and APK destination folder.

    signature key

  3. Find the generated APK file.

    apk file

  4. You can confirm the alignment with the following command:
    zipalign -c -v <alignment> existing.apk

How to Add Dependencies to Android Studio Project

When developing Android applications, sometimes we need to use some third-party libraries, such as OpenCV, Zxing, etc. So how to add the dependencies in Android Studio?

Press Ctrl+ALT+Shift+S to popup Project Structure window. Select Dependencies and click Add button.

Android Studio dependency

  • Library dependency is used to import libraries from Maven repository. For example, we can search for OpenCV:

    maven dependency

  • File dependency is used to import local source files or jar files.
  • Module dependency is the module created in your project. For example, you can clone Zxing source code from GitHub, and create a Zxing module to modify and build a package yourself.

Android QR Code Generator with Zxing

I have made a simple QRCode generator demo with Zxing.

Here are the basic steps:

  1. Get the Zxing source code from GitHub:
    git clone https://github.com/zxing/zxing
  2. Create a Java library module.

    Zxing module

  3. Copy Zxing source code to the new module.

    zxing source code

  4. Add module dependency.

    module dependency

  5. Created an EditText, Button and ImageView on the layout.
    XML
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
    
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_alignParentStart="true" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText"
            android:text="Generate QRCode"
            android:id="@+id/barcode_button" />
        <ImageView
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:layout_below="@id/barcode_button"
            android:id="@+id/barcode"
            android:background="@color/abc_search_url_text_normal"/>
    </RelativeLayout>
  6. Encode String with QRCodeWriter.
    Java
    public void onClick(View v) {
        String content = mEditText.getText().toString();
        QRCodeWriter write = new QRCodeWriter();
        try {
            int width = mImageView.getWidth();
            int height = mImageView.getHeight();
            BitMatrix bitMatrix = write.encode(content, BarcodeFormat.QR_CODE, width, height);
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    bitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK: Color.WHITE);
                }
            }
            mImageView.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
    
  7. Run the demo and test it.

    Android QR code generator

Is Android Studio Better than Eclipse for Android Development?

While using Android Studio, the user experience is entirely different. There are so many excellent built-in functionalities like code analyzer, source code control, UI editor, and so on that facilitate development. Although I still need time to get used to it, I have to admit it is as powerful as Android team described. Time to migrate!

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer Dynamsoft
Canada Canada
Xiao Ling is A technical content writer at Dynamsoft, the leading company of document capture and image processing SDKs.
He is in charge of managing Dynamsoft blog site codepool.biz and Dynamsoft GitHub community.

Comments and Discussions

 
-- There are no messages in this forum --