【问题标题】:Migrating from IDEA to Android Studio with Gradle使用 Gradle 从 IDEA 迁移到 Android Studio
【发布时间】:2014-02-05 10:43:58
【问题描述】:

我已将我的项目从 IDEA 迁移到 Android Studio 以使用 Gradle。 我已经成功构建了应用程序(还没有查看测试),现在想要包含产品风味(我搬家的原因)。

我现在可以在“构建变体”菜单中选择四个构建变体之一,使用新设置构建会在我的手机上呈现一个新应用,所以看起来一切正常。

但是,我应该在哪里更改源(使用原始目录结构)还是需要更改它?什么是最好的方法? (我没有/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
    }

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

【问题讨论】:

    标签: android gradle build.gradle


    【解决方案1】:

    我假设您问的是您将特定风味的资源放在哪里;如果您询问在哪里对主要来源和资源进行更改,那是在您在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
    }
    

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

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

    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]
    

    main 的目录是预期的——这是sourceSets.main 中明确设置的目录。根据sourceSets 块末尾的语句,我们看到的 debugrelease 类型已重新映射到 build-types 目录。对于 app1app2 风格,它在 src/app1src/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]
    

    【讨论】:

    • 看起来真不错。我将您的建议应用于我的项目并更新了 build.gradle。但是,它似乎并没有在控制台的输出中考虑帐户的更改。我也更新了上面的 build.gradle 列表并发布了输出(注意,这不是来自我的应用程序,所以现在是真实姓名,而不是 app1 app2)
    • 没关系。当然,我应该在更改根目录后插入评论!很好的答案!谢谢一百万!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多