java - Android Studio AndroidManifest.xml 与 build.gradle

标签 java android xml eclipse android-studio

如果有人能帮助我了解有关 Android Studio 的一些事情,那将是最有启发性的。

所以,我大约一个月前从 Eclipse 切换到 Android Studio,到目前为止,我一直在处理迁移后的应用程序。因此,我一直在修改 AndroidManifest.xml 文件,这在 Eclipse 中很常见。

但是,最近,为了从头开始学习 Android Studio 与 Eclipse 的区别,我开始创建一个新项目。除了我遇到的非常恼人的 appcompat_v7 问题外,我还对一些关于 build.gradle 的事情感到困惑。

下面是一个来自 Android Studio 创建的应用程序的 gradle 代码块:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:mediarouter-v7:22.2.0'
}

另一方面,下面是迁移后的 Eclipse 项目的 build.gradle 中的代码块:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':google-play-services_lib')
}

android {
    compileSdkVersion 21
    buildToolsVersion '22.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')
    }
}

我从阅读中得出的一些假设是否正确?

  1. compileSdkVersion - 必须始终使用最高版本才能最大程度地兼容新手机?

  2. targetedSdkVersion - 我自己对应用程序最佳运行条件的偏好?

  3. buildToolsVersion - 我读到必须始终使用最新版本。有人可以解释为什么吗?

现在回答我关于 manifest vs gradle 的问题:

  1. compile 和 buildtools 版本必须相同吗?它们可以不同吗?

  2. 如果我同时拥有 AndroidManifest.xml 和 build.gradle,Android Studio 如何知道将哪个版本用于 compile、min、targeted、buildtools 版本?

  3. 作为问题 1 的扩展,当两个文件之间存在差异时会发生什么(如果有人出于某种原因忘记并决定在其中一个文件中添加内容?)

  4. 由于两者之间有重复的属性,这是否意味着从 Android Studio 生成的应用程序开始,我根本不需要触及 AndroidManifest.xml?

    <activity></activity>呢?所以应用程序在找不到 Activity 时不会强制关闭? build.gradle 会自动处理吗?或者控制方向和其他更精细的功能(我是否坚持只修改 Android Studio 上的 java 文件?)

    (如果没有,那么用 2 个文件来控制应用程序有点多余,也许它们应该坚持使用 AndroidManifest.xml?)

很抱歉,问题很长,也许很曲折,但它确实让我很困惑。

提前致谢。

更新:

看完What is Gradle in Android Studio?http://developer.android.com/tools/studio/index.html ,我的新问题:

  1. AndroidManifest.xml 仍然需要,build.gradle 只是覆盖具有相同属性的设置。

    问题:那么在 Android Studio 中仍然需要两者,对吗?

  2. 编译和构建工具版本不必相同,但构建工具必须始终高于 compileSdkVersion。

    问题:这是因为Google为每个新的Sdk创建了一个新的buildtools版本并且更高版本向后兼容吗?因此,更高的构建工具将构建更低的 compileSdkVersion 而反之则不然,对吗?

最佳答案

我会尝试解决尽可能多的问题,但我会首先建议您不要使用从 eclipse 迁移中生成的 build.gradle。在 Android Studio 中创建一个新项目并使用它生成的 build.gradle 作为您应该使用的模板,即将其内容复制到您的真实项目并更改有意义的值。花时间了解并正确使用 build.gradle,这会在将来节省您的时间。还要尽可能模仿新项目的文件结构。 gradle 的伟大之处在于它(通常)会给你有意义的错误。

compileSdkVersion - must always use the highest for maximum compatibility to newer phones?

targetedSdkVersion - my own preference on the optimal run conditions of my app?

在大多数情况下,compile 和 targeted 应该是相同的。编译值显然告诉编译器要针对哪个版本进行编译,而目标版本则告诉运行时要使用哪些兼容性功能。例如,如果您的目标是 v21,并且该应用在运行 v23 的手机上运行,​​它将启用一些兼容性功能,使您的应用运行得更好。

buildToolsVersion - I read this must ALWAYS be using the latest version. Can someone explain why?

您可以将其视为编译器的构建工具。如果您设置了 compileSdkVersion 23,那么您将需要 23.+ 版本的构建工具。但是,为了回答您的问题,假设 23.0 版的构建工具存在错误(例如,它没有正确构建 native 代码),然后 Google 将发布 23.1 版的构建工具。现在,如果您的代码不编译 native 代码,那么更新并不真正适用于您——您不需要它,但是嘿,更新总是好的,以防万一。此外,如果您的 compileSdkVersion 是 23,并且您拥有 24 版的构建工具,那么 24 版的构建工具完全有能力构建 23 版。

Do compile & buildtools version have to be the same? Can they be different?

希望上面已经回答了这个问题,但答案是肯定的,它们可以不同,但​​是构建工具的主要版本必须始终大于 compileSdkVersion。

If I have both an AndroidManifest.xml and a build.gradle, how does Android Studio know which to use for the compile,min,targeted,buildtools versions?

As an extension to question 1, what happens when there is a discrepancy between the 2 files (if someone forgot for some reason and decided to add stuff in one of them?)

build.gradle 将覆盖 AndroidManifest.xml 文件中的值,但为了避免混淆,我会将上述所有值放入 build.gradle 中,并将它们从 list 中删除。那是他们属于的地方。 build.gradle 可以做一些非常酷的事情,它可以覆盖 list 中的值,甚至可以合并两个 list 文件。

Since there are duplicate attributes between the 2, does this mean that starting with apps generated from Android Studio, I don't need to touch AndroidManifest.xml at all?

What about the so the app doesn't force close when it can't find the activity? Does build.gradle take care of that automatically? Or controlling orientation and other finer features (am I stuck with modifying only the java files on Android Studio?)

(if it doesn't, then having 2 files to control the app is kind of redundant and maybe they should have just stuck to AndroidManifest.xml?)

绝对不是。这只是意味着您必须在 build.gradle 中做一些事情,在 AndroidManifest.xml 中做一些事情。例如,如果您添加一个 Activity ,则必须照常编辑 AndroidManifest.xml。如果您想更改 Activity 的属性(旋转、主题等),这仍然在 AndroidManifest.xml 中完成。如果你想开始使用来自 Maven Central 的新库,你必须将它添加到 build.gradle 中。如果您想在发布版本时更改用于签署应用程序的 key ,或者更改您的应用程序的版本名称:build.gradle。基本上 build.gradle 可以让你更好地控制你的构建,我真的建议你看看你能在这里做什么: http://developer.android.com/tools/building/configuring-gradle.html

AndroidManifest.xml is still needed, build.gradle just overrides settings if it has the same attributes.

Question: So BOTH are still needed in Android Studio, correct?

正确。你仍然需要两者。

compile & buildtools version DON'T have to be the same BUT buildtools must always be higher than compileSdkVersion.

Question: Is this because Google creates a new buildtools version for each new Sdk and the higher version are backward compatible? Therefore, higher buildtools will build lower compileSdkVersion while the reverse is not true, correct?

也正确!

关于java - Android Studio AndroidManifest.xml 与 build.gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421902/

相关文章:

java - 正则表达式如何获取子字符串?

java - Hibernate 和 MappingNotFoundException

android - 当 ArrayAdapter.isEnabled 返回 false 时,ListView 中的分隔符消失

android - 添加带有子菜单项的图标

c++ - 比较时返回节点信息

用于将文件发送到 https Web 服务的所有信任的 java 类

java - JPA - @OneToMany 与 JoinTable

java - 触摸事件 : I want to recognize touches while moving with an another finger on the screen

java - 强制转义 Spring 中 XML 编码中的特殊字符

xml - Hive - XML- Serde - 键值对 - 创建表