美文网首页
将Eclipse工程转成Android Studio工程的方法(

将Eclipse工程转成Android Studio工程的方法(

作者: Whyn | 来源:发表于2016-11-09 11:43 被阅读392次

    我们知道,Eclipse工程的目录结构跟Android Studio的有很大的不同,采用Android Studio的导入Eclipse工程方法,却也会导致原本的目录结构改变。如果想要用Android Studio编译Eclipse工程,但却不想改变原本的目录结构,那么,只需要Eclipse工程的根目录下,新建一个build.gradle,更改下studio默认的工程目录即可。具体做法如下:

    • 1.在根目录创建一个build.gradle,对当前工程(模块)进行配置.
          // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
          buildscript {
              repositories {
                  jcenter()
              }
          dependencies {
              classpath 'com.android.tools.build:gradle:2.0.0'
    
              // NOTE: Do not place your application dependencies here; they   belong
              // in the individual module build.gradle files
            }
          }
    
          allprojects {
              repositories {
                  jcenter()
              }
          }
    
          task clean(type: Delete) {
              delete rootProject.buildDir
          }
    
          //this is module build.gradle
           apply plugin: 'com.android.application'
    
          android {
              compileSdkVersion 23
              buildToolsVersion "23.0.2"
    
              defaultConfig {
                  applicationId "com.example.eclipse2studio"
                  versionCode 1
                  versionName "1.0"
              }
    
              packagingOptions {
                  exclude 'META-INF/NOTICE.txt'
                  exclude 'META-INF/LICENSE.txt'
              }
    
              sourceSets {
                  main {
                     manifest.srcFile 'AndroidManifest.xml'
                      aidl.srcDirs = ['src']
                      java.srcDirs = ['src']
                      resources.srcDirs = ['src']
                      res.srcDirs = ['res']
                      assets.srcDirs = ['assets']
                      jniLibs.srcDirs = ['libs']
                      renderscript.srcDirs = ['src']
                  }
           // 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')
              }
          }
    
          //signHelper.setNeedSign()
          dependencies {
              //compile fileTree(dir: 'libs', include: ['*.jar'])
              compile 'com.android.support:appcompat-v7:23.2.0' exclude module: 'support-v4'
              compile files('../libs/bugly_crash_upgrade_v1.1.2.jar')
              compile project(':lib01')
              compile (project(':lib02')) {   
                     exclude module: 'support-v4'
                }
          }
    

    注:关键的代码部分是sourceSets部分,该部分就将studio的目录结构更改为跟eclipse的相匹配,路径都是相对应该build.gradle的。

    • 2.如果该工程还加载了其他工程作为库文件,那么则要在工程根目录下创建一个settings.gradle,来对库工程进行配置。
    include ':lib01'
    project(':lib01').projectDir = new File('../lib01')
    include ':lib02'
    project(':lib02').projectDir = new File('E:/android/lib02')
    

    经过以上步骤,eclipse工程就已经顺利转换程Android Studio工程了,同时,它也仍然还是eclipse工程。

    更多详细配置,请参考官方文档

    相关文章

      网友评论

          本文标题:将Eclipse工程转成Android Studio工程的方法(

          本文链接:https://www.haomeiwen.com/subject/liwzuttx.html