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

时间:2013-10-17 08:13:12

标签: android gradle android-studio android-gradle

我有一个在Eclipse中创建的Android项目exported as Gradle build file,然后在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 个答案:

答案 0 :(得分:17)

尝试在build.gradle之后添加buildToolsVersion以下行:

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 18
}