Android Studio AndroidManifest.xml vs build.gradle [英] Android Studio AndroidManifest.xml vs build.gradle

查看:111
本文介绍了Android Studio AndroidManifest.xml vs build.gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If anyone could help me understand some things regarding Android Studio, it would be most enlightening.

因此,我已经在一个月前从Eclipse切换到Android Studio到目前为止,我们只处理迁移的应用程序。因此,我一直在修改Eclipse中常见的AndroidManifest.xml文件。

So, I have switched from Eclipse to Android Studio around a month ago and so far have only been working on my migrated Apps. As such, I have been tinkering around with only the AndroidManifest.xml file as was customary in Eclipse.

但是,最近,我已经开始创建一个新项目了从Eclipse开始学习Android Studio与Eclipse的不同之处。除了我遇到的非常烦人的appcompat_v7问题之外,我还对build.gradle的一些事情感到困惑。

However, recently, I have started creating a new project for the sake of learning Android Studio's differences from Eclipse from ground up. Aside from the very irritating appcompat_v7 issues I have encountered, I'm also confused with some things regarding build.gradle.

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

Below is a gradle code block from an app created from Android Studio:

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中的代码块:

On the other hand, below is a code block from build.gradle of a migrated Eclipse project:

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')
    }
}

我在读取的一些假设中是否正确?

Am I right in some of the assumptions I have taken from reading?


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

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

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

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

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

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

现在我对关于清单vs gradle的问题:

Now to my questions with regards to manifest vs gradle:


  1. 编译& buildtools版必须一样吗?它们可以不同吗?

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

如果我同时拥有AndroidManifest.xml和build.gradle,Android Studio如何知道哪个用于编译,min ,目标,构建工具版本?

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?

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

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?)

由于2之间存在重复属性,这是否意味着从Android Studio生成的应用开始,我根本不需要触摸AndroidManifest.xml?

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?

< activity>< / activity> 怎么样,所以应用程序不强制关闭它找不到活动? build.gradle会自动处理吗?或控制方向和其他更精细的功能(我只是修改Android Studio上的java文件?)

What about the <activity></activity> 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?)

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

(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?)

很抱歉,也许是缠绕,问题,但这对我来说真的很混乱。

Sorry for the long, maybe winding, questions but it really is quite confusing to me.

提前致谢。

更新:

阅读 Android Studio中的Gradle是什么? http://developer.android.com/tools/studio/index.html,我的新问题:


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

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

问题:Android Studio中仍然需要这样做,对吗?

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

编译& buildtools版本不必相同但是buildtools必须始终高于compileSdkVersion。

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

问题:这是因为Google为每个新的Sdk创建了一个新的buildtools版本,而较高版本是向后兼容的吗?因此,较高的buildtools会构建较低的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?


推荐答案

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

I will try to address as many questions as possible, but I will start by suggesting that you do not use the generated build.gradle from the eclipse migration. Create a new project in Android Studio and use the build.gradle that it generates as a template for what you should be using, ie copy it's contents to your real project and change the values that make sense. Spend the time understanding and getting the build.gradle right, it will save you time in the future. Also mimic the file structure of the new project as best you can. The great thing about gradle is that it (usually) gives you meaningful errors.


compileSdkVersion - 必须始终使用最高
与较新手机的兼容性?

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

targetedSdkVersion - 我自己喜欢的最佳运行条件
我的应用程序?

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

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

compile and targeted should, in most cases, be the same. The compile value obviously tells the compiler which version to compile against and the target version tells the runtime which compatibility features to use. For example if you are targeting v21 and the app is running on a phone running v23, it will enable some compatibility features to enable your app to run a little nicer.


buildToolsVersion - 我读过这个必须总是使用最新的
版本。有人可以解释原因吗?

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

您可以将构建工具视为编译器。如果您已设置compileSdkVersion 23,那么您将需要23. +版本的构建工具。但是,为了回答你的问题,让我们说版本23.0的构建工具存在一个错误(例如,它没有正确构建本机代码),那么谷歌将发布23.1版本的构建工具。现在,如果你的代码没有编译本机代码,那么更新并不真正适用于你 - 你不需要它,但是,嘿,更新你总是很好。此外,如果你的compileSdkVersion是23并且你有构建工具版本24,那么构建工具的第24版就能够构建版本23.

The build tools you can think of as the compiler. If you have set compileSdkVersion 23, then you will need the 23.+ version of the build tools. But, to answer your question, let's say there was a bug with version 23.0 of the build tools (eg. it wasn't building native code properly) then Google would release 23.1 of the build tools. Now if your code doesn't compile native code, then the update doesn't really apply to you - you don't need it, but hey, it's always good to update incase you do. Moreover, if your compileSdkVersion is 23 and you have build tools version 24, well version 24 of the build tools is quite capable of building version 23.


编译& buildtools版必须一样吗?它们可以是
不同吗?

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

希望这可以在上面得到解答,但答案是肯定的,它们可能会有所不同,但是构建工具主要版本必须始终大于compileSdkVersion。

Hopefully this is answered above, but the answer is yes they can be different, but the build tools major version must always be greater than the compileSdkVersion.


如果我同时拥有AndroidManifest.xml和build.gradle,那么
Android Studio知道哪些用于
编译,分钟,目标,构建工具版本?

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?

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

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中,并从清单中删除它们。这就是他们所属的地方。 build.gradle可以做一些非常酷的事情,它可以覆盖清单中的值,甚至合并两个清单文件。

The build.gradle will override values in the AndroidManifest.xml file, but to avoid confusion I would put all the above mentioned values into the build.gradle, and remove them from the manifest. That's where they belong. The build.gradle can do some really cool things, it can override values in the manifest and even merge two manifest files.


因为有2之间的重复属性,这是否意味着从Android Studio生成的应用开始的
,我根本不需要
触摸AndroidManifest.xml?

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?

如果应用程序无法在找不到活动时强行关闭
怎么办? build.gradle会自动处理
吗?或控制方向和其他更精细的功能(我只是修改了Android Studio上的java文件?)

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?)

(如果没有,那么控制应用程序的2个文件是多余的
,也许它们应该只停留在
AndroidManifest.xml?)

(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中做一些事情。例如,如果添加活动,则必须照常编辑AndroidManifest.xml。如果您想要更改活动的属性(旋转,主题等),这仍然在AndroidManifest.xml中完成。如果要从Maven Central开始使用新库,则必须将其添加到build.gradle。如果要更改用于在发布版本时对应用程序进行签名的密钥,或者更改应用程序的versionName:build.gradle。基本上build.gradle可以让你更高程度地控制你的构建,我真的建议你看看你能做些什么:
http://developer.android.com/tools/building/configuring-gradle.html

Definitely not. It just means that you have to do some things in the build.gradle and some in the AndroidManifest.xml. For example, if you add an activity, you must edit the AndroidManifest.xml as usual. If you want to alter the properties of the activity (rotation, theme etc) this is also still done in the AndroidManifest.xml. If you want to start using a new library from Maven Central, you will have to add it to the build.gradle. If you want to change the keys you use to sign the app with when you make a release build, or change the versionName of your app: build.gradle. Basically the build.gradle gives you a higher degree of control over your builds, I really recommend checking out what you can do here: http://developer.android.com/tools/building/configuring-gradle.html


仍然需要AndroidManifest.xml,如果它具有相同的属性,build.gradle只会覆盖
设置。

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

问题:所以两个在Android Studio中仍然需要,对吗?

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

正确。你仍然需要两者。

Correct. You still need both.


compile& buildtools版本不必相同但是构建工具
必须始终高于compileSdkVersion。

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

问题:这是因为Google创建了一个新的buildtools版本
每个新的Sdk和更高版本是向后兼容的?
因此,较高的buildtools会构建较低的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?

也正确!

这篇关于Android Studio AndroidManifest.xml vs build.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆