gpt4 book ai didi

android - 使用 Gradle 从 IDEA 迁移到 Android Studio

转载 作者:太空狗 更新时间:2023-10-29 14:16:24 26 4
gpt4 key购买 nike

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

我现在可以在“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
}

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

最佳答案

我假设您是在询问将特定口味的资源放在哪里;如果您询问在哪里对主要来源和资源进行更改,那就是您在 sourceSets.main 下映射的目录结构中。 .

您可以将这段脚本添加到您的 build.gradle 文件的顶层(例如在 android block 之后)以打印出它将查找资源的目录:

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 中明确设置的内容.我们看到的 debugrelease 类型已根据 sourceSets 末尾的语句重新映射到 build-types 目录堵塞。对于 app1app2 风格,它在 src/app1src/app2 中查找,这可能不是你想要什么。我建议为您的风格移动整个根目录,类似于您为构建类型所做的。我会在最后将该 block 更改为:

// 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]

关于android - 使用 Gradle 从 IDEA 迁移到 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21574994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com