无法解析符号RequiresApi

时间:2017-08-17 17:51:38

标签: java android android-gradle

我一直在"无法解决符号' RequiresApi'"错误。我在这里做错了什么?

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.fsck.k9.service.MailService;
//import timber.log.Timber;


 @RequiresApi(api = Build.VERSION_CODES.M)
class DeviceIdleReceiver extends BroadcastReceiver {
    private final PowerManager powerManager;
    private String TAG = DeviceIdleReceiver.class.getSimpleName();


    DeviceIdleReceiver(PowerManager powerManager) {
        this.powerManager = powerManager;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean deviceInIdleMode = powerManager.isDeviceIdleMode();
        //Timber.v("Device idle mode changed. Idle: %b", deviceInIdleMode);
        Log.e(TAG,"Device idle mode changed. Idle: %b");

        if (!deviceInIdleMode) {
            MailService.actionReset(context, null);
        }
    }
}

这是gradle文件:

apply plugin: 'com.android.application'

    if (!rootProject.optimizeForDevelopment) {
        apply from: '../gradle/plugins/checkstyle-android.gradle'
        apply from: '../gradle/plugins/findbugs-android.gradle'
    }

    if (rootProject.testCoverage) {
        apply plugin: 'jacoco'
    }

    repositories {
        jcenter()
    }

    //noinspection GroovyAssignabilityCheck
    configurations.all {
        resolutionStrategy {
            force "com.android.support:support-annotations:${androidSupportLibraryVersion}"
        }
    }

    dependencies {

        compile project(':plugins:Android-PullToRefresh:library')
        compile project(':plugins:HoloColorPicker')


        compile "com.squareup.okio:okio:${okioVersion}"
        //compile "com.android.support:support-v4:${androidSupportLibraryVersion}"


        testCompile "org.robolectric:robolectric:${robolectricVersion}"
        testCompile "junit:junit:${junitVersion}"
        testCompile "org.mockito:mockito-core:${mockitoVersion}"
        compile 'commons-io:commons-io:2.4'
        compile 'com.android.support:support-core-ui:25.2.0'
        compile 'com.android.support:appcompat-v7:25.2.0'
        compile 'com.android.support:design:25.2.0'
        compile 'com.android.support:support-v13:25.2.0'
        compile 'com.android.support:cardview-v7:25.2.0'
        compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.18'
        compile 'de.cketti.library.changelog:ckchangelog:1.2.1'
        compile 'com.github.bumptech.glide:glide:3.6.1'
        compile 'com.splitwise:tokenautocomplete:2.0.7'

        compile 'com.android.support.constraint:constraint-layout:1.0.0-beta3'
        testCompile 'org.jsoup:jsoup:1.10.2'
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    }


    def renameAPK(variant, defaultConfig, buildType) {
        variant.outputs.each { output ->


            def file = output.packageApplication.outputFile
            def fileName = "MYapp" + "_V" + defaultConfig.versionName + ".apk"


            output.packageApplication.outputFile = new File(file.parent, fileName)
        }
    }

    android {
        compileSdkVersion androidCompileSdkVersion.toInteger()
        buildToolsVersion androidBuildToolsVersion

        defaultConfig {
            applicationId "xxx.xxx.xxx"
            testApplicationId "xxx.xxx.xxx.tests"

            versionCode 1000010
            versionName '1.0.010'

            minSdkVersion 21
            targetSdkVersion 23

            generatedDensities = ['mdpi', 'hdpi', 'xhdpi']

            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }

        signingConfigs {
            release
        }

        buildTypes {
            release {
                if (project.hasProperty('storeFile')) {
                    signingConfig signingConfigs.release
                }
                applicationVariants.all { variant ->
                    renameAPK(variant, defaultConfig, 'R')
                }

                buildConfigField "boolean", "DEVELOPER_MODE", "false"
                buildConfigField "String", "OPENKEYCHAIN_PROVIDER_AUTHORITY", "\"xxx.xxx.xxx.provider\""
                resValue "string", "provider_permission", "xxx.xxx.xxx.provider.permission.CONTENT"
            }

            debug {
                applicationIdSuffix ".debug"
                testCoverageEnabled rootProject.testCoverage

                buildConfigField "boolean", "DEVELOPER_MODE", "true"
                buildConfigField "String", "OPENKEYCHAIN_PROVIDER_AUTHORITY", "\"xxx.xxx.xxx.debug.provider\""
                resValue "string", "provider_permission", "xxx.xxx.xxx.debug.provider.permission.CONTENT"
            }
        }

        // Do not abort build if lint finds errors
        lintOptions {
            disable "ResourceType"
            abortOnError false
            lintConfig file("$rootProject.projectDir/config/lint/lint.xml")
        }

        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'LICENSE.txt'
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
        buildToolsVersion '25.0.2'
    }

    if (project.hasProperty('keyAlias')) {
        android.signingConfigs.release.keyAlias = keyAlias
    }
    if (project.hasProperty('keyPassword')) {
        android.signingConfigs.release.keyPassword = keyPassword
    }
    if (project.hasProperty('storeFile')) {
        android.signingConfigs.release.storeFile = file(storeFile)
    }
    if (project.hasProperty('storePassword')) {
        android.signingConfigs.release.storePassword = storePassword
    }

看起来我无法导入" RequiresApi"方法,我检查并按照下面的链接步骤无济于事: https://developer.android.com/studio/write/annotations.html#adding-library

1 个答案:

答案 0 :(得分:0)

导入

androidx.annotation.RequiresApi

插入 import android.support.annotation.RequiresApi;