如何在 Android Studio 中为项目设置最低 api 级别?

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

How do I set the minimum api level for projects in Android Studio?

androidgradleandroid-studio

提问by JohnB

I recently migrated a project into Android Studio 0.4.3. When compiling, it gave me errors on several lines of java, including the following:

我最近将一个项目迁移到 Android Studio 0.4.3。编译时,它给了我几行java的错误,包括以下内容:

FragmentManager fm = getFragmentManager();

fm.findFragmentById()

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

getResources().getStringArray(R.array.course_descriptions); 

I see the following error message:

我看到以下错误消息:

Call requires API level 11 (current min is 7)

I'm running Android Studio .0.4.3. I also downloaded Android 4.4.2 (API 19) through the SDK manager.

我正在运行 Android Studio .0.4.3。我还通过 SDK 管理器下载了 Android 4.4.2 (API 19)。

I added the following line to my manifest:

我在清单中添加了以下行:

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

I did a "Sync Project with Gradle files", cleaned and rebuilt the project. I still see the same errors.

我做了一个“与 Gradle 文件同步项目”,清理并重建了项目。我仍然看到相同的错误。

How do I fix this issue ?

我该如何解决这个问题?

回答by AsfK

I have 0.8.6 version and it's can be done like this:

我有 0.8.6 版本,可以这样做:

In the menu: File -> Project Structure.

在菜单中:文件 -> 项目结构。

In the open menu, navigate to app, then in the tabs choose Flavors.

在打开的菜单中,导航到app,然后在选项卡中选择Flavors

enter image description here

在此处输入图片说明

回答by Yuichi Araki

If you have generated your project on recent versions of Android Studio, the setting is in gradle config file. Find build.gradlefile in your application module. There should be something like this.

如果您在最新版本的 Android Studio 上生成了项目,则该设置位于 gradle 配置文件中。build.gradle在您的应用程序模块中查找文件。应该有这样的事情。

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

回答by pyus13

Starting from gradle build system all your setting related to sdk and compilation APIs will be overridden by what you define in your build.gradlefile.

从 gradle 构建系统开始,所有与 sdk 和编译 API 相关的设置都将被您在build.gradle文件中定义的内容覆盖。

So going forward if you want you can remove these all configurations from your AndroidManifest.xmland keep them in build.gradlefiles only to avoid confusions.

因此,如果您愿意,可以从您的所有配置中删除这些配置AndroidManifest.xml并将它们保存在build.gradle文件中,以避免混淆。

So check minSdkVersionin module's build.gradlefile which is throwing the error.

因此,请检查引发错误的minSdkVersion模块build.gradle文件。

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'

    defaultConfig {
        minSdkVersion 7      // Change 7 to 11 will solve your problem
        targetSdkVersion 19
    }

}

I prefer to have these all configurations in my top project level build.gradlefile because if the configurations are different in different modules you will be ended up with Manifest Merging error.

我更喜欢将这些所有配置都放在我的顶级项目级build.gradle文件中,因为如果不同模块中的配置不同,您最终会遇到 Manifest Merging 错误。

回答by Shivaraj Patil

Full app/build.gradle minSdkVersion, targetSdkVersion

完整的 app/build.gradle minSdkVersion, targetSdkVersion

apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

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

packagingOptions {
    exclude 'LICENSE.txt'
}
}

dependencies {
androidTestCompile fileTree(dir: 'libs', include: '*.jar')
androidTestCompile('com.squareup.assertj:assertj-android:1.0.0'){
    exclude group: 'com.android.support', module:'support-annotations'
    }
}

回答by Yasin

Simply follow the steps (For android studio users):

只需按照以下步骤操作(适用于 android studio 用户):

  1. right click the App directory
  2. choose the "module setting" option
  3. change the ADK Platform as what you need
  4. Click Apply
  1. 右键单击应用程序目录
  2. 选择“模块设置”选项
  3. 根据需要更改 ADK 平台
  4. 点击应用

That's all buddy, The gradle will rebuild the project automatically.

哥们,就是这样,gradle 会自动重建项目。

回答by Vani Gupta

In the latest version as of dated 2020, perform the following steps:

在截至 2020 年的最新版本中,执行以下步骤:

  1. Click on Files (top left)
  2. Go to Project Structure (Ctlr + Alt + Shift + S)
  3. Click on modules
  4. In the right pane, click on Default Config (Middle one)
  5. Change Minimum SDK version
  6. Apply and click OK
  1. 点击文件(左上角)
  2. 转到项目结构(Ctlr + Alt + Shift + S)
  3. 点击模块
  4. 在右侧窗格中,单击默认配置(中间一个)
  5. 更改最低 SDK 版本
  6. 应用并单击确定

Done!

完毕!