From Eclipse migrate to Android Studio - AkuCode

From Eclipse migrate to Android Studio

    Migrating your project to Android Studio requires adapting to a new project structure, build system, and IDE functionality. If you are migrating Android project from Eclipse, Android Studio provides an import tools so you can quickly move your existing code into Android Studio project and Gradle-based build files.
    If you are migrating from IntelliJ and your project already uses Gradle, you can simply open your existing project from Android Studio. If you are using IntelliJ, but your project does not already use Gradle, you will need to do a little bit of manual preparation before you can import your project into Android Studio.

Android Studio Basic

Here are some of the key differences you should be aware of as you prepare to migrate to Android Studio.

Project and module organization

    Android Studio is based on the IntelliJ IDEA IDE. To become familiar with the IDE basics, such as navigation, code completion, and keyboard shortcuts, see Meet Android Studio. Android Studio does not use workspaces, so separate projects open in separate Android Studio windows. Android Studio organizes code into projects, which contain everything that defines your Android app, from app source code to build configurations and test code. Each project contains one or more modules, which allow you to divide your project into discrete units of functionality. Modules can be independently built, tested, and debugged.

Gradle-based build system

    Android Studio’s build system is based on Gradle and uses build configuration files written in groovy syntax for ease of extensibility and customization.
Gradle-based project offer significant features for Android development, including the following:
  • Support for binary libraries (AARs). You no longer need to copy library sources into your own project, you can simply declare a dependency and the library is automatically downloaded and merged into your project. This includes automatically merging in resources, manifest entries, proguard exclusion rules, custom lint rules, and so on at build time.
  • Support for build variants. For example, build variants let you build different versions of your app (such as a free version and a pro version) from the same project.
  • Easy build configuration and customization. For example, you can pull version name and  version codes from git tags as part of the build.
  • Gradle can be used from the IDE but also from the command line and from continuous integration servers like Jenkins, providing the same build everywhere, every time.

Dependencies

    Library dependencies in Android Studio use Gradle dependency declarations and Maven dependencies for well-known local source and binary libraries with Maven coordinates.

Test Code

    With Eclipse ADT, instrumentation tests are written in separate projects and integrated through the <instrumentation> element in your manifest file. Android Studio provides and androidTest/ directory in your project’s main sourceset so you can easily add and maintain your instrumentation test code within the same project view. Android Studio also provides a test/ directory in your project’s main sourceset for local JVM test.

Migration from Eclipse

Android Studio offers an automated import tool for existing Android projects created using Eclipse

Migration Prerequisites

    Before migrating your app from Eclipse to Android Studio, review the following steps to make sure your project is ready for conversion, and verify you have the tools configuration you need in Android Studio:

In Eclipse ADT:

  • Make sure the Eclipse ADT root directory contains the AndroidManifest.xml file. Also, the root directory must contain either the .project and .classpath file from Eclipse or the res/ and src/ directories.
  • Build your project to ensure you latest workspace and project updates are saved and included in the import.
  • Comment out any references to Eclipse ADT workspace library files in the project.properties or .classpath files for import. You can add these references in the build.gradle file after the import.
  • It may be useful to record your workspace directory, path variables, and any actual path maps that could be used to specify any unresolved relative paths, path variables, and linked resource preferences. Android Studio allows you to manually specify any unresolved paths during the import process.

In Android Studio:

  • If you don’t have it, download Android Studio. If you do have Android Studio, verify that it is the latest stable release by clicking Help > Check for Updates (on Mac, Android Studio > Check for Updates).
  • Because Android Studio does not migrate any third-party Eclipse ADT plugins, make a note of any third-party plugins you use Eclipse. You can check for equivalent features in Android Studio of search for a compatible plugin in the IntelliJ Android Studio Plugins repository. Use the File > Setting > Plugins menu option to manage plugins in Android Studio.
  • If you plan to run Android Studio behind a firewall, be sure to set the proxy settings for Android Studio and the SDK Manager. Android Studio requires an internet connection for Setup Wizard synchronization, 3rd-party library access, access to remote repositories, Gradle initialization and synchronization, and Android Studio version updates.

Import Eclipse projects to Android Studio

You should decide how you will import your existing Eclipse ADT projects depending on their structure:
  • If you have multiple related projects sharing the same workspace in Eclipse ADT, import the first project as a project, then add subsequent related projects as modules within that project.
  • If your Eclipse ADT projects share dependencies within the same workspace but are not otherwise related, import each Eclipse ADT project individually into Android Studio as a separate project. Android Studio maintains the shared dependencies across the newly created projects as part of the import process
  • If your Eclipse ADT project includes native (C/C++) libraries,

Import as a project:

  1. Start Android Studio and close any open Android Studio projects.
  2. From the Android Studio menu click File > New > Import Project
  3. Select the Eclipse ADT project folder with the AndroidManifest.xml and click Ok.
  4. Select the destination folder and click Next.
  5. Select the import options and click Finish
  6. The import process prompts you to migrate any library and project dependencies to Android Studio, and add the dependency declarations to the build.gradle file.
    The import process also replaces any well-known source libraries, binary libraries and JAR file that have known Maven coordinates with Maven dependencies, so you no longer need to maintain these dependencies manually. The import options also allow you to enter your workspace directory and any actual path maps to handle any unresolved relative paths, path variables, and linked resource references.
      7. Android Studio imports the app and displays the project import summary. Review the summary for details about the project restructuring and the import process.
    After importing the project from Eclipse ADR into Android Studio, each app module folder in Android Studio contains the complete source set for that module, including the src/main/ and src/androidTest/ directories, resources, build file, and Android manifest. Before starting app development, you should resolve any issues shown in the project import summary to make sure the project re-structuring and import process completed properly.

Import as a module:

  1. Start Android Studio and open the project you’d like to add the module to
  2. From the Android Studio menu click File > New > Import Module.
  3. Select the Eclipse ADT project folder with the AndroidManifest.xml file and click Ok.
  4. Modify the module name if desired, and click Next.
  5. The import process prompts you to migrate any library and project dependencies to Android Studio, and add the dependency declarations to the build.gradle file. The import process also replaces any well-known source libraries, binary libraries, and JAR files that have known Maven coordinates with Maven dependencies, so you no longer need to maintain these dependencies manually. The import options also allow you to enter your workspace directory and any actual path maps to handle any unresolved relative paths, path variables, and linked resource references.
  6. Click Finish.

Validate imported projects

    After completing the import process, use the Android Studio Build and Run menu options to build your project and verify the output. If your project is not building properly, check the following settings:
  • Verify the installed version of your tools match the settings for your Eclipse project by opening the SDK Manager (click the Android SDK Manager button in Android Studio or Tools > SDK Manager). Android Studio inherits the SDK Manager and JDK settings from your imported Eclipse project
  • To verify additional Android Studio Settings, click File > Project Structure and inspect the following:
            o Under SDK Location verifies Android Studio has access to the correct SDK, NDK, and JDK locations and versions.
            o Under Project verify the Gradle version, Android plugin version, and related repositories.
            o Under Modules verify the app and module setting, such as signing configuration and library dependencies.
  • If your project depends on another project, make sure that dependency is defined properly in the build.gradle file in the app module folder. 
If there still are unexpected issues when building and running your project in Android Studio after you have checked these settings, consider modifying the Eclipse ADT project and re-starting the import process.

Migrating from IntelliJ

    If your IntelliJ project uses the Gradle build system, you can automatically import your project directly into Android Studio. If your IntelliJ project uses Maven or another build system, you need to set it up to work with Gradle before you can migrate to Android Studio.

Import a Gradle-based IntelliJ project

    If you are already using Gradle with your IntelliJ project, you can open it in Android Studio using the following steps:
  1. Click File > New > Import Project.
  2. Select your  IntelliJ project directory, and click OK. Your project will open in Android Studio

Import a non-Gradle IntelliJ project

    If your IntelliJ project does not already use the Gradle build system, you have two options for importing your project into Android Studio:
  • Create a new empty Android Studio project and copy your existing source code into the directories associated with the new project.
  • Manually create a new Gradle build file for your project and then import the project and new build file into Android Studio.

Migrate by creating a new empty project

    To migrate your project into Android Studio by creating a new empty project and copying your source files into the new directories, proceed as follows:
  1. Open Android Studio, and click File > New > New Project.
  2. Enter a name for your app project and specify the location where it should be created, and then click Next.
  3. Select the form factors your app will run on, and then click Next.
  4. Click Add No Activity, and then click Finish.
  5. In the project tool windows, click the arrow to open the view dropdown, and select the Project view to see and explore the organization of your new Android Studio project.
  6. Navigates to the location you selected for your new project and move the code, unit tests, instrumentation tests, and resources from your old project directories into the correct locations in you new project structure.
  7. In Android Studio, click File > Project Structure to open the Project Structure dialog. Ensure that your app’s module is selected in the left pane.
  8. Make any necessary modifications in the Properties tab for your project (for example, modifying the minSdkVersion or targetSdkVersion).
  9. Click Dependencies and add any libraries your project depends on as Gradle dependencies. To add a new dependency, click Add +, then selected the type of dependency you would like to add and follow the prompts.
  10. Click OK to save your modifications.
  11. Click Build > Make Project to test building your project, and resolve any outstanding errors.

Migrate by creating a custom Gradle build file

    To migrate your project into Android Studio by creating a new Gradle build file to point to your existing source files, proceed as follows:
  1. Before you begin, be sure to back up your project files in a separate location, as the migration process will modify the contents of your project in place.
  2. Next, create a file in your project directory called build.gradle. the build.gradle file will contain all the information required for Gradle to run your build.
        By default, Android Studio expects your project to be organized as shown in figure 1.
    Since you IntelliJ project does not use the same structure, you build.gradle file needs to point the source directories for the build to your existing folders (for example, res/ and src/) instead of the default new directory structure. The following example build.gradle file includes the basic setup for a Gradle build, as well as a sourceSets{} block inside the android{} block to define the correct source directories and move the test and build types to avoid naming conflicts. Copy the code block below into your build.gradle file and make any changes necessary to work with your existing project setup. For example, you may have additional dependencies to include, be using a different target SDK version, or need to specify different locations for your source directories.
// This buildscript{} block configures the code driving the build
buildscript {
   /**
    * The nested repositories{} block declares that this build uses the
    * jcenter repository.
    */
    repositories {
        jcenter()
    }

   /**
    * This block declares a dependency on the 3.6.0 version
    * of the Gradle plugin for the buildscript.
    */
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0'
    }
}

/**
 * This line applies the com.android.application plugin. Note that you should
 * only apply the com.android.application plugin. Applying the Java plugin as
 * well will result in a build error.
 */
apply plugin: 'com.android.application'

/**
 * This dependencies block includes any dependencies for the project itself. The
 * following line includes all the JAR files in the libs directory.
 */
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // Add other library dependencies here (see the next step)
}

/**
 * The android{} block configures all of the parameters for the Android build.
 * You must provide a value for at least the compilation target.
 */
android {
    compileSdkVersion 28

    /**
    * This nested sourceSets block points the source code directories to the
    * existing folders in the project, instead of using the default new
    * organization.
    */
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

       /**
        * Move the build types to build-types/
        * For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        * This moves them out of them default location under src//... which would
        * conflict with src/ being used by the main source set.
        * Adding new build types or product flavors should be accompanied
        * by a similar customization.
        */
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
     }
} 
      3. Next, identify which library projects you are using. With Gradle, you no longer need to add these libraries as source code projects, you can refer to them in the dependencies{} block of your build file instead. The build system then handles these libraries for you, including downloading libraries, merging in resources, and merging manifest entries. The following example adds the declaration statements for Google Play Services and a number of support libraries to the dependencies{} block shown in the example build file above.
...
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Google Play Services
    compile 'com.google.android.gms:play-services:9.8.0'

    // Support Libraries
    compile 'com.android.support:appcompat-v7:28.0.0'
    compile 'com.android.support:cardview-v7:28.0.0'
    compile 'com.android.support:design:28.0.0'
    compile 'com.android.support:gridlayout-v7:28.0.0'
    compile 'com.android.support:leanback-v17:28.0.0'
    compile 'com.android.support:mediarouter-v7:28.0.0'
    compile 'com.android.support:palette-v7:28.0.0'
    compile 'com.android.support:recyclerview-v7:28.0.0'
    compile 'com.android.support:support-annotations:28.0.0'
    compile 'com.android.support:support-v13:28.0.0'
    compile 'com.android.support:support-v4:28.0.0'

    // Note: these libraries require the "Google Repository" and "Android Repository"
    //       to be installed via the SDK manager.
} 
      4. Save your build.gradle file, then close the project in IntelliJ. Navigate to your project directory, and delete the .idea directory any .iml files inside your project.
      5. Launch Android Studio, and click File > New > Import Project.
      6. Locate your project directory, click the build.gradle file you created above to select it, and then click OK to import your project.
      7. Click Build > Make Project to test your build file by building your project and address any errors you find.

Next Steps

    Once you have migrated your project to Android Studio, learn more about building with Gradle and running your app in Android Studio. Depending on your project and workflow, you may also wish to read more about using version control, managing dependencies, signing and packaging your app, or configuring and updating Android Studio.

Configure version control

    Android Studio supports a variety of version control systems, including Git, Github, VCS, Mercurial, Subversion, and Google Cloud Source Repositories. After importing your app into Android Studio, use the Android Studio VCS menu options to enable VCS support for the desired version control system, create a repository, import the new files into version control, and perform other version control operations:
  1. From the Android Studio VCS menu, click Enable Version Control Integration.
  2. Select a version control system to associate with the project root from the drop-down menu, then click OK. The VCS menu now displays a number of version control options based on the system you selected.

Android support repository and Google Play Services repository

    While Eclipse ADT uses the Android Support Library and Google Play services Library, Android Studio replaces these libraries during the import process with the Android Support Repository and Google Repository to maintain compatible functionality and support new Android features. Android Studio adds these dependencies as Maven dependencies using known Maven coordinates, so these dependencies do not require manual updates.
    In Eclipse, in order to use a Support Library, you must modify your project’s classpath dependencies within your development environment for each Support Library you want to use. In Android Studio, you no longer need to copy library sources into your own projects, you can simply declare a dependency and the library is automatically downloaded and merged into your project. This includes automatically merging in resources, manifest entries, ProGuard exclusion rules, and custom lint rules at build time.

App signing

    If your app used a debug certificate in Eclipse ADT, Android Studio continues to reference that certificate. Otherwise, the debug configuration uses the Android Studio generated debug keystore, with a known password and a default key with a known password located in $HOME/.android/debug.keystore. The debug build type is set to use this debug configuration automatically when you run or debug your project from Android Studio.
    When building your app for release, Android Studio applies the release certificate used in Eclipse ADT. If no release certificate was located during the import process, add the release signing configuration to the build.gradle file or use the Build > Generate Signed APK menu option to open the Generate Signed APK wizard.

Comments