2014-02-05 71 views
3

我已将我的项目从IDEA迁移到Android Studio以使用Gradle。 我已经成功地构建了应用程序(还没有看过测试),现在想包括产品口味(我的理由移动)。使用Gradle从IDEA迁移到Android Studio

我现在可以在“Build Variants”菜单中选择四种构建变体之一,并使用新设置构建一个新的应用程序,这样看起来一切正常。

但是,我在哪里对源文件进行更改(使用原始目录结构)还是需要更改它?什么是最好的方法? (我没有结构/ src/xxx/res,只是src/org/bla/bla)

主要的改变是像字符串文件和图形这样的资源。 谢谢!

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

repositories { 
    mavenCentral() 
} 

dependencies { 
    compile 'com.google.android.gms:play-services:4.1.+' 
    compile 'org.apache.commons:commons-lang3:3.2.+' 
    compile 'org.apache.httpcomponents:httpmime:4.1.+' 
    compile 'com.android.support:appcompat-v7:+' 
} 

android { 

    compileSdkVersion 19 
    buildToolsVersion "19.0.1" 

    defaultConfig { 
     minSdkVersion 8 
     targetSdkVersion 19 
     versionCode 1 
     //versionName "0.5" 
    } 

    productFlavors { 
     iDomsPortalDev { 
      packageName 'org.idoms.iDomsAndroid.iDomsPortalDev' 
     } 
     iDomsPortal { 
      packageName 'org.idoms.iDomsAndroid.iDomsPortal' 
     } 
    } 

    buildTypes { 
     debug { 
      packageNameSuffix ".debug" 
      //versionNameSuffix "-debug" 
     } 
     release { 
      packageNameSuffix ".release" 
      //versionNameSuffix "-release" 
     } 
    } 

    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') 
     iDomsPortal.setRoot('product-flavors/iDomsPortal') 
     iDomsPortalDev.setRoot('product-flavors/iDomsPortalDev') 

} 
println "main: " + android.sourceSets.main.res.srcDirs 

    android.applicationVariants.all { variant -> 
     println variant.name + ": " + android.sourceSets[variant.name].res.srcDirs 
    } 

    android.productFlavors.all { flavor -> 
     println flavor.name + ": " + android.sourceSets[flavor.name].res.srcDirs 
    } 

    android.buildTypes.all { buildType -> 
     println buildType.name + ": " + android.sourceSets[buildType.name].res.srcDirs 
    } 

[!编辑:更新后的数据上面,并把代码打印更改后的输出,现在,它的工作原理]

回答

1

我想你问,你把特定的风味reources;如果您要求更改主要来源和资源的位置,则在sourceSets.main下映射的目录结构中。

您可以脚本的此位添加到您的的build.gradle文件的顶层(如android块之后)打印出来的目录在那里将寻找资源:

println "main: " + android.sourceSets.main.res.srcDirs 

android.applicationVariants.all { variant -> 
    println variant.name + ": " + android.sourceSets[variant.name].res.srcDirs 
} 

android.productFlavors.all { flavor -> 
    println flavor.name + ": " + android.sourceSets[flavor.name].res.srcDirs 
} 

android.buildTypes.all { buildType -> 
    println buildType.name + ": " + android.sourceSets[buildType.name].res.srcDirs 
} 

这输出特定于风味的资源目录以及构建变体(flavor +构建类型目录)。

当我在你的构建文件运行它们,我得到:

main: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/res] 
app1: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1/res] 
app2: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2/res] 
debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/debug/res] 
release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/release/res] 
app1Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Debug/res] 
app1Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Release/res] 
app2Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Debug/res] 
app2Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Release/res] 

主要目录预计 - 这就是在sourceSets.main明确设置。 调试版本类型我们根据sourceSets块末尾的语句重新映射到构建类型目录。对于app1app2风格,它正在寻找 src/app1 src/app2,这可能不是你想要的。我建议移动整个根目录为您的口味,类似于你如何为构建类型。我会改变该块在年底:

// 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') 
app1.setRoot('product-flavors/app1') 
app2.setRoot('product-flavors/app2') 

至于变种,app1Debug等人,你也许不必担心他们,除非你正在做的太过具体,你的东西真的需要在详细的基础上重写。无论如何,现在当我运行并查看诊断输出时,我看到:

main: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/res] 
app1: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/product-flavors/app1/res] 
app2: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/product-flavors/app2/res] 
debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/debug/res] 
release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/release/res] 
app1Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Debug/res] 
app1Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Release/res] 
app2Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Debug/res] 
app2Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Release/res] 
+0

这看起来确实不错。我将你的建议用于我的项目并更新了build.gradle。但是,它似乎并未在控制台中进行帐户中的更改。我更新了上面的build.gradle的列表,并发布了输出(注意,这不是来自我的应用程序,所以真实姓名现在在那里,而不是app1 app2) –

+0

没关系。当然,我应该在更改根后插入注释!很好的答案!太感谢了! –