如何让Android Studio正确读取AndroidManifest.xml中的(minSdkVersion)?

7
我有一个Android项目,它是在Eclipse中创建的, 导出为Gradle构建文件,然后在Android Studio中打开。(是的,在AS中创建一个干净的项目会更容易,但我需要支持当前项目结构。)。
否则大部分情况下都可以工作,但仍有一些问题需要解决。 在每个Activity类中,AS都显示这个错误:Class requires API level 1 (current min is -1): Activity

enter image description here

Alt+Enter提供了使用@TargetApi注释来修复它的方法... 但是为什么我要那样做呢?因为我们在AndroidManifest.xml中已经声明了:
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

AndroidManifest.xml位于项目根目录(Eclipse默认位置?)。看起来Android Studio没有正确读取其中的设置。虽然项目仍能正常编译。

有任何想法如何消除这个错误吗?

Eclipse生成的build.gradle如下所示:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')    
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.0"

    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')
    }
}
1个回答

19

请在buildToolsVersion之后添加以下几行到build.gradle中:

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 18
}

2
是的,在重新打开项目后,问题得到了解决。谢谢! - Jonik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接