Android Studio:更改 targetSdkVersion 的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20902100/
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 03:40:13  来源:igfitidea点击:

Android Studio: What is the right way to change targetSdkVersion?

androidandroid-studio

提问by Edmund Tam

I want to change the targetSdkVersion from 19 to 18 in Android Studio (not Eclipse), but failed.

我想在 Android Studio(不是 Eclipse)中将 targetSdkVersion 从 19 更改为 18,但是失败了。

Android Studio complains the following after I changed the targetSdkVersion, and resync the project with Gradle files:

在我更改了 targetSdkVersion 并将项目与 Gradle 文件重新同步后,Android Studio 会抱怨以下内容:

Execution failed for task ':(project name):processDebugManifest'.
> Manifest merging failed. See console for more info.

From the console I found that it is the library I added to the gradle dependencies using the old targetSdkVersion value.

从控制台我发现它是我使用旧的 targetSdkVersion 值添加到 gradle 依赖项的库。

[(Code Directory)\main\AndroidManifest.xml, (Code Directory)\build\exploded-bundles\(Library Directory).aar\AndroidManifest.xml:2] Main manifest has <uses-sdk android:targetSdkVersion='18'> but library uses targetSdkVersion='19'

I learn that in Android Studio, the targetSdkVersion and minSdkVersion flags are defined in the build.gradle file. The project is created with Android Studio's "New project" wizard, and there is NO tag in my code's AndroidManifest.xml. So the problem should not be the two values out of sync. According to the above message, the targetSdkVersion value in the main manifest is what I want.

我了解到在 Android Studio 中,targetSdkVersion 和 minSdkVersion 标志是在 build.gradle 文件中定义的。该项目是使用 Android Studio 的“新建项目”向导创建的,我的代码的 AndroidManifest.xml 中没有标签。所以问题不应该是两个值不同步。根据上面的消息,主清单中的 targetSdkVersion 值是我想要的。

So the problem should be in the manifest file of the library. I open the library's AndroidManifest.xml file and found the following line:

所以问题应该出在库的清单文件中。我打开库的 AndroidManifest.xml 文件并找到以下行:

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

Obviously it isn't correct. The problem is that the file is not for manual modification. I tried changing the targetSdkVersion value, and even tried removing the declaration, but it comes back every time I build the project.

显然这是不正确的。问题是该文件不适用于手动修改。我尝试更改 targetSdkVersion 值,甚至尝试删除声明,但每次构建项目时它都会返回。

I tried cleaning the project, still no luck.

我尝试清理项目,仍然没有运气。

So my question is: Is there a "right" way to alter the targetSdkVersion in Android Studio which I am not aware of?

所以我的问题是:是否有一种“正确”的方法可以在我不知道的 Android Studio 中更改 targetSdkVersion?

回答by Mathias Asberg

How to change targetSdkVersionand minSdkVersionin Android studio

如何在 Android Studio 中更改targetSdkVersionminSdkVersion

Did have the same issue but mange to nail it down after a couple of hours.

确实有同样的问题,但几个小时后就解决了。

Step 1

第1步

Open build.gradle - *Path\Your_Application\app\build.gradle*

打开 build.gradle - *Path\Your_Application\app\build.gradle*

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

Change targetSdkVersion & minSdkVersion values to what you desire.

将 targetSdkVersion 和 minSdkVersion 值更改为您想要的值。

Step 2

第2步

In Android stuido menu find "Build" ( alt + b @ windows ) and choose "Clean project"

在 Android stuido 菜单中找到“Build”( alt + b @ windows )并选择“Clean project”

Hope this help others with the same issue.

希望这可以帮助其他有同样问题的人。

回答by Mueyiwa Moses Ikomi

Alternatively, click on File > Project Structure > Application > Flavours > Min Sdk Version and press ok on change. You can also change the Target Sdk Version as well. I think this is a neater approach to editing the gradle build file directly.

或者,单击 File > Project Structure > Application > Flavors > Min Sdk Version 并在更改时按确定。您也可以更改目标 SDK 版本。我认为这是直接编辑 gradle 构建文件的一种更简洁的方法。

回答by Saleem Kalro

in build.gradle Change these values according to current project Updation in sdk manager

在 build.gradle 中根据当前项目更新在 sdk manager 中更改这些值

compileSdkVersion 25 buildToolsVersion "25.0.2" minSdkVersion 15 targetSdkVersion 25

compileSdkVersion 25 buildToolsVersion "25.0.2" minSdkVersion 15 targetSdkVersion 25

in dependencies replace below code
compile 'com.android.support:appcompat-v7:25.1.0'

在依赖项中替换下面的代码
编译 'com.android.support:appcompat-v7:25.1.0'

and synchronise project

并同步项目