Jonik
Jonik

Reputation: 81812

How to make Android Studio read (minSdkVersion from) AndroidManifest.xml correctly?

I have an Android project which was created in Eclipse, exported as Gradle build file, then opened in Android Studio. (Yes, it would be far easier to create a clean project in AS but I need to support the current project structure.)

Otherwise things are now mostly working, but there's still some stuff to be ironed out. In every Activity class, AS shows this error: Class requires API level 1 (current min is -1): Activity

enter image description here

Alt+Enter offers to fix that with a @TargetApi annotation... but why should I have to do that, when in AndroidManifest.xml, we have:

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

AndroidManifest.xml is located at project root (Eclipse default?). Looks like Android Studio is not correctly reading the settings in it. The project still compiles fine though.

Any idea how to get rid of the error?

The Eclipse-generated build.gradle looks like this:

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

Upvotes: 7

Views: 10962

Answers (1)

Laser
Laser

Reputation: 6960

Try to add to the build.gradle following lines right after buildToolsVersion:

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 18
}

Upvotes: 19

Related Questions