Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining directory structure during Android Studio import

I have cloned a public github repository and am trying to import it into Android Studio 0.4.4. The import works great, but it wants to copy all the source to a new directory and creates an 'app' directory for the source. Since I am using a public repo, I want to use the repo cloned directory and keep the directory structure in tact so I can get updates and push changes without having to copy files every which way.

Is there a way to tell Android Studio to keep the given directory structure on an import?

Or, put another way, Is there a way to skip the gradle conversion and import the source as is?

Thanx in advance.

like image 221
NLam Avatar asked Feb 14 '14 15:02

NLam


2 Answers

At present we don't have an option in Eclipse project import to preserve the existing directory structure. If you have a copy of Eclipse installed you could open it there and then export the project to Gradle build files; that does an in-place export. You'd have to modify the build files it exports since it specifies a very old version of the plugin. Specifically, you'll have to update the dependencies.buildscript.classpath statement to specify 0.8.+, and also update the distributionUrl property in the gradle/wrapper/gradle-wrapper.properties file to specify Gradle 1.10.

If you don't have Eclipse, you can use this boilerplate build.gradle file to get you started. Copy it into the project's root directory:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    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/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... 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')
    }
}

Next, put this settings.gradle file in your root directory.

include ':'

Next, copy the gradlew and gradlew.bat files and the gradle directory from the root directory of another project you've created to install the wrapper in your project.

It's complete. You should now be able to import it in Android Studio and use it.

like image 113
Scott Barta Avatar answered Sep 24 '22 17:09

Scott Barta


Here's the steps I did to convert my project from Eclipse ADT to Android Studio while keeping the git history:

  1. rename my project directory to Project.eclipse
  2. Select the "Import from non-Android Studio project"
  3. Give it the original project name for the target directory (it will create files that are directory specific and use that name)
  4. After it's done converting, make sure it compiles.
  5. close out of Android Studio
  6. Rename the new project directory to Project.astudio
  7. Rename the original Eclipse ADT project directory back to the original directory name
  8. Read through the import-summary.txt file created by the importer, and recreate all of the changes it lists by hand, using git commands (usually consists of moving the AndroidManifest.xml and the source and res files into a new directory path
  9. Change your .gitignore file to read as follows:

    .gradle
    /local.properties
    /.idea/workspace.xml
    /.idea/libraries
    .DS_Store
    /build
    /app/build
    
  10. Compare the directories and copy any files from the new project that aren't in the old one back to it (usually gradle and idea files), then "git add" them. Don't forget the build.gradle file inside any newly-created subprojects (which will include at least one for your main app)

  11. Use git commands to delete any files from the original project that aren't present in the new one and aren't purposely put there by you (i.e. keep your README and LICENSE files, etc, but nuke things like the leftover eclipse project data)
  12. Make use of "git status" frequently during the above process to see what's matching/not matching, and git add or git rm as needed.
  13. Attempt to open the project in Android Studio, make sure it opens correctly, and that it builds. If everything checks out...
  14. close out of Android Studio
  15. git commit

It sounds like a long process, but it keeps your history intact while letting you take advantage of the new directory structure, and it worked pretty good for me.

like image 39
justdave Avatar answered Sep 25 '22 17:09

justdave