构建失败 - Android Marshmallow SMS_SEND错误:Gradle:java.io.FileNotFoundException :(访问被拒绝)

时间:2016-09-28 06:10:27

标签: java android android-6.0-marshmallow access-denied

我正在学习开发Android移动应用程序。我做了一个简单的应用程序发送短信。我正在使用运行marshmallow的ADB模拟器。我已经阅读了有关新权限here的内容,并且发现了很多问题answers我的问题无法解决。

当我做一个CLEAN和BUILD时,我会收到以下消息:

Error:Gradle: java.io.FileNotFoundException: C:Examples\MySMSApplication\app\build\intermediates\res\resources-debug.ap_ (Access is denied)

注意:我删除uses-permission android:name="android.permission.SEND_SMS后,我能够成功构建并运行应用。但是,在尝试发送短信说出另一个模拟器号5554时,我将始终收到我的消息Permission denied

我的Android清单中设置了两个用户权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fisher.app">
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/mysmsapp"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

然后在mainActivity

public class MainActivity extends AppCompatActivity {

    Button buttonSend;
    EditText textPhoneNo;
    EditText textSMS;

    private static final int PERMISSION_REQUEST = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
        textSMS = (EditText) findViewById(R.id.editTextSMS);

        buttonSend.setOnClickListener(new View.OnClickListener() {
            //this is the onclick listener of send button
            @Override
            public void onClick(View v) {
                String phoneNo = textPhoneNo.getText().toString();
                String sms = textSMS.getText().toString();

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                        if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
                            Snackbar.make(findViewById(R.id.myView), "You need to grant SEND SMS permission to send sms",
                                    Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
                                }
                            }).show();
                        } else {
                            requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
                        }
                    } else {
                        sendSMS(phoneNo, sms);
                    }
                } else {
                    sendSMS(phoneNo, sms);
                }
            }
        });
    }

    private void sendSMS(String phoneNo, String message) {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNo, null, message, null, null);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            Snackbar.make(findViewById(R.id.myView), "Permission Granted",
                    Snackbar.LENGTH_LONG).show();
            sendSMS(textPhoneNo.getText().toString(), textSMS.getText().toString());

        } else {

            Snackbar.make(findViewById(R.id.MyView), "Permission denied",
                    Snackbar.LENGTH_LONG).show();

        }
    }
}

以下是ref:

的Gradle Build
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.fisher.myapp"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.1.1'
}

1 个答案:

答案 0 :(得分:0)

好的发现问题不是编译器或整体代码。这是AndroidManifest

我看到和阅读的方式通常是将AndroidManifest.xml中的权限定义为:

<uses-permission android:name="android.permission.SEND_SMS"/>

然而,在稍微阅读了关于影响SDK 23及以上的Marshmallow权限后,我发现它需要:

<uses-permission-sdk-23 android:name="android.permission.SEND_SMS"/>

请注意 -sdk-23 。这样做解决了构建问题,应用示例验证了SEND_SMS权限是否已启用,或者如果没有则请求。