如何更改 Android 项目上的目标构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3342739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:48:55  来源:igfitidea点击:

How to change target build on Android project?

androidbuildtarget

提问by Donal Rafferty

I currently have an Android project in Eclipse.

我目前在 Eclipse 中有一个 Android 项目。

I created it with a target build of 1.5 (sdk 3).

我用 1.5 (sdk 3) 的目标版本创建了它。

Now I want to change it so that it has a minSdk of 3 and targetSdk of 8.

现在我想更改它,使其 minSdk 为 3,targetSdk 为 8。

To do this I see that I must build against the newest SDK (2.2)

为此,我发现我必须针对最新的 SDK (2.2) 进行构建

To do this in Eclipse I right click on my project, go to properties, click on Android and change the project build target to Android 2.2 and click apply and then ok.

要在 Eclipse 中执行此操作,我右键单击我的项目,转到属性,单击 Android 并将项目构建目标更改为 Android 2.2,然后单击应用,然后确定。

However this appears to have no affect and when I try it again the target build is set back at Android 1.5.

然而,这似乎没有影响,当我再次尝试时,目标版本被设置回 Android 1.5。

Am I missing a step or something?

我错过了一步吗?

回答by Ryan Conrad

Right click the project and click "Properties". Then select "Android" from the tree on the left. You can then select the target version on the right.

右键单击项目,然后单击“属性”。然后从左侧的树中选择“Android”。然后,您可以在右侧选择目标版本。

(Note as per the popular comment below, make sure your properties, classpath and project files are writable otherwise it won't work)

(请注意,根据下面的流行评论,请确保您的属性、类路径和项目文件是可写的,否则将无法工作)

回答by Alan Will

  1. You can change your the Build Target for your project at any time:

    Right-click the project in the Package Explorer, select Properties, select Android and then check the desired Project Target.

  2. Edit the following elements in the AndroidManifest.xml file (it is in your project root directory)

    In this case, that will be:

    <uses-sdk android:minSdkVersion="3" />
    <uses-sdk android:targetSdkVersion="8" />
    

    Save it

  3. Rebuild your project.

    Click the Project on the menu bar, select Clean...

  4. Now, run the project again.

    Right Click Project name, move on Run as, and select Android Application

  1. 您可以随时更改项目的构建目标:

    在 Package Explorer 中右键单击项目,选择 Properties,选择 Android,然后检查所需的 Project Target。

  2. 编辑 AndroidManifest.xml 文件中的以下元素(它在您的项目根目录中)

    在这种情况下,这将是:

    <uses-sdk android:minSdkVersion="3" />
    <uses-sdk android:targetSdkVersion="8" />
    

    保存

  3. 重建你的项目。

    单击菜单栏上的 Project,选择 Clean...

  4. 现在,再次运行该项目。

    右键单击项目名称,继续运行方式,然后选择 Android 应用程序

By the way, reviewing Managing Projects from Eclipse with ADTwill be helpful. Especially the part called Creating an Android Project.

顺便说一下,回顾使用 ADT 从 Eclipse 管理项目会很有帮助。特别是称为创建 Android 项目的部分。

回答by Scott C Wilson

Another way on the command line if you are using ant is to use the android.bat script (Windows) or android script (Mac). It's in $SDK_DIR/tools.

如果您使用 ant,命令行上的另一种方法是使用 android.bat 脚本 (Windows) 或 android 脚本 (Mac)。它在 $SDK_DIR/tools 中。

If you say,

如果你说,

android.bat update project --path .  --target "android-8"

it will regenerate your build.xml, AndroidManifest.xml, etc.

它将重新生成您的 build.xml、AndroidManifest.xml 等。

回答by Rank

There are three ways to resolve this issue.

有三种方法可以解决这个问题。

  1. Right click the project and click "Properties". Then select "Android" from left. You can then select the target version from right side.

  2. Right Click on Project and select "run as" , then a drop down list will be open.
    Select "Run Configuration" from Drop Down list.Then a form will be open , Select "Target" tab from "Form" and also select Android Version Api , On which you want to execute your application, it is a fastest way to check your application on different Target Version.

  3. Edit the following elements in the AndroidManifest.xml file

  1. 右键单击项目,然后单击“属性”。然后从左侧选择“Android”。然后,您可以从右侧选择目标版本。

  2. 右键单击项目并选择“运行方式”,然后将打开一个下拉列表。
    从下拉列表中选择“运行配置”。然后将打开一个表单,从“表单”中选择“目标”选项卡,然后选择 Android 版本 Api,您要在其上执行应用程序,这是检查您的应用程序的最快方法不同目标版本上的应用程序。

  3. 编辑 AndroidManifest.xml 文件中的以下元素

xml:

xml:

<uses-sdk android:minSdkVersion="3" />
<uses-sdk android:targetSdkVersion="8" />

回答by stealthcopter

Well I agree with Ryan Conrad on how to do it in eclipse, have you ensured you have changed your manifest.xml?

好吧,我同意 Ryan Conrad 关于如何在 eclipse 中做到这一点的看法,您是否确保已更改 manifest.xml?

 <uses-sdk android:minSdkVersion="3" />
 <uses-sdk android:targetSdkVersion="8" />

回答by notBanana

The file default.propertiesis by default read only, changing that worked for me.

该文件default.properties默认为只读,更改对我有用。

回答by Mike Way

The problem sometimes occurs when there are errors in the project.

该问题有时会在项目出现错误时出现。

For instance, if your project is configured with a target of 3.2 but the 3.2 libraries are not available, you will not be able to change the version to 4.0!

例如,如果您的项目配置目标为 3.2,但 3.2 库不可用,您将无法将版本更改为 4.0!

The usual (perhaps brutal) solution I use is to create a new project with the correct target and copy src, res and manifest into the new project.

我使用的通常(可能是残酷的)解决方案是创建一个具有正确目标的新项目,并将 src、res 和 manifest 复制到新项目中。

Update:

更新:

This seems to work:

这似乎有效:

  1. Change the selected through the build properties as normal
  2. Manually edit project.properties AND default.properties to make sure they both reflect the desired target.
  3. Close the project and re-open it
  1. 像往常一样通过构建属性更改选定项
  2. 手动编辑 project.properties 和 default.properties 以确保它们都反映了所需的目标。
  3. 关闭项目并重新打开它

I always run Android Tools | Fix Project Properties after making any changes to the build target.

我总是运行 Android 工具 | 在对构建目标进行任何更改后修复项目属性。

回答by javaProgrammer

You should not have multiple "uses-sdk" tags in your file. ref - docs

您的文件中不应有多个“uses-sdk”标签。参考 -文档

Use this syntax:
    <uses-sdk android:minSdkVersion="integer"
          android:targetSdkVersion="integer"
          android:maxSdkVersion="integer" />

回答by user1164035

As Mike way says. Change target BEFORE doing anything in your project that requires a higher target like android:installLocation="auto".

正如迈克方式所说。在你的项目中做任何需要更高目标的事情之前改变目标,比如 android:installLocation="auto"。

回答by Adukra

I had this problem too. What worked for me was to first un-check the previously selected SDK version before checking the new desired version. Then click okay.

我也有这个问题。对我有用的是在检查新的所需版本之前先取消选中以前选择的 SDK 版本。然后点击确定。