简体   繁体   English

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

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

I recently migrated a project into Android Studio 0.4.3.我最近将一个项目迁移到 Android Studio 0.4.3。 When compiling, it gave me errors on several lines of java, including the following:编译时,它给了我几行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.我正在运行 Android Studio .0.4.3。 I also downloaded Android 4.4.2 (API 19) through the SDK manager.我还通过 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.我做了一个“与 Gradle 文件同步项目”,清理并重建了项目。 I still see the same errors.我仍然看到相同的错误。

How do I fix this issue ?我该如何解决这个问题?

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

在此处输入图片说明

If you have generated your project on recent versions of Android Studio, the setting is in gradle config file.如果您在最新版本的 Android Studio 上生成了项目,则该设置位于 gradle 配置文件中。 Find build.gradle file in your application module.在您的应用程序模块中找到build.gradle文件。 There should be something like this.应该有这样的事情。

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

Starting from gradle build system all your setting related to sdk and compilation APIs will be overridden by what you define in your build.gradle file.从 gradle 构建系统开始,所有与 sdk 和编译 API 相关的设置都将被您在build.gradle文件中定义的内容覆盖。

So going forward if you want you can remove these all configurations from your AndroidManifest.xml and keep them in build.gradle files only to avoid confusions.因此,如果您愿意,可以从AndroidManifest.xml删除这些所有配置,并将它们仅保留在build.gradle文件中以避免混淆。

So check minSdkVersion in module's build.gradle file which is throwing the error.因此,请检查build.gradle错误的模块build.gradle文件中的minSdkVersion

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.gradle file because if the configurations are different in different modules you will be ended up with Manifest Merging error.我更喜欢在我的顶级项目级build.gradle文件中包含这些所有配置,因为如果不同模块中的配置不同,您最终会遇到 Manifest Merging 错误。

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)转到项目结构(Ctlr + Alt + Shift + S)
  3. Click on modules点击模块
  4. In the right pane, click on Default Config (Middle one)在右侧窗格中,单击默认配置(中间一个)
  5. Change Minimum SDK version更改最低 SDK 版本
  6. Apply and click OK应用并单击确定

Done!完毕!

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

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根据需要更改 ADK 平台
  4. Click Apply点击应用

That's all buddy, The gradle will rebuild the project automatically.哥们,就是这样,gradle 会自动重建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM