7

我有一个在 Eclipse 中创建的 Android 项目,导出为 Gradle 构建文件,然后在 Android Studio 中打开。(是的,在 AS 中创建一个干净的项目会容易得多,但我需要支持当前的项目结构。)

否则事情现在大部分都在工作,但仍有一些问题需要解决。在每个 Activity 类中,AS 都会显示此错误:Class requires API level 1 (current min is -1): Activity

在此处输入图像描述

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

1 回答 1

19

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

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 18
}
于 2013-10-17T08:24:45.437 回答