在 Android Studio 中升级 API 级别

Upgrade API Level in Android Studio

我在尝试将我的应用程序更新到 Marshmallow 6.0.1 时遇到了一些问题。我之前在 5.1.1 上工作,我为更新 Android 腾出了一些空间,所以我确保它与 APIs 21-24 兼容。现在,6.0.1 在 API 23 上运行,但由于某种原因,应用程序在启动时崩溃了。 我是 运行 最新的 Android Studio 3.0.1,我只希望它能在 Android M 上运行。 我有两个模块,其中之一是相机的外部库。我分享我的 gradle 设置:

对于'app'模块是:

apply plugin: 'com.android.application'

 android {
    compileSdkVersion 24
    buildToolsVersion '26.0.2'
    useLibrary  'org.apache.http.legacy'


    defaultConfig {
        applicationId "XXXXXXXXX"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 332
        versionName "1.7.3d"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions{
        disable 'MissingTranslation'
    }
 }
  repositories {
     jcenter()
     maven { url "https://dl.bintray.com/drummer-aidan/maven" }
  }

 dependencies {
    compile project(':cam-library')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v13:25.3.0'
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support:design:25.3.0'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.android.volley:volley:1.0.0'
    compile 'org.apache.httpcomponents:httpmime:4.2.3'
    compile 'com.jcraft:jsch:0.1.54'
    compile 'com.jcraft:jzlib:1.1.3'}

另一个,Follestad material 相机模块:

 apply plugin: 'com.android.library'

 ext {
    PUBLISH_GROUP_ID = 'com.afollestad'
    PUBLISH_ARTIFACT_ID = 'material-camera'
    PUBLISH_VERSION = '0.3.2'
    PUBLISH_VERSION_CODE = 21
    SUPPORT_LIB_VERSION = '25.3.0'
    VIDEO_PLAYER_VERSION = '0.2.12'
    MD_VERSION = '0.9.0.1'
    TARGET_SDK = 24
    BUILD_TOOLS = "24.0.1"
 }

android {
    compileSdkVersion TARGET_SDK
    buildToolsVersion BUILD_TOOLS

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion TARGET_SDK
        versionCode PUBLISH_VERSION_CODE
        versionName PUBLISH_VERSION
    }
    lintOptions {
        abortOnError false
        checkReleaseBuilds false
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '25.0.0'
 }

repositories {
    jcenter()
    maven { url "https://dl.bintray.com/drummer-aidan/maven" }
 }

dependencies {
    compile "com.android.support:appcompat-v7:$SUPPORT_LIB_VERSION"
    compile "com.afollestad:easyvideoplayer:$VIDEO_PLAYER_VERSION"
    compile "com.afollestad.material-dialogs:core:$MD_VERSION"
    compile 'com.android.support:recyclerview-v7:25.3.0'
 } 

//apply from: 'https://raw.githubusercontent.com/afollestad/aidanfollestad.com/master/android-lib-release.gradle'

最后 gradle 版本设置:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这有什么问题,为什么它在 5.1.1 上运行而不在 6.0.1 上运行?

编辑:这是日志

    03/05 11:49:51: Launching app
$ adb shell am start -n "XXXXXXXXXXXXXX/XXXXXXXXXXXXXX.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: XXXXXXXXXXXXXX.test | XXXXXXXXXXXXXX
Waiting for application to come online: XXXXXXXXXXXXXX.test | XXXXXXXXXXXXXX
Waiting for application to come online: XXXXXXXXXXXXXX.test | XXXXXXXXXXXXXX
Waiting for application to come online: XXXXXXXXXXXXXX.test | XXXXXXXXXXXXXX
Waiting for application to come online: XXXXXXXXXXXXXX.test | XXXXXXXXXXXXXX
Connecting to XXXXXXXXXXXXXX
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8820', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1486)
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --compiler-filter=interpret-only --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=verify-at-runtime --instruction-set-variant=cortex-a9 --instruction-set-features=default --dex-file=/data/app/XXXXXXXXXXXXXX-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@XXXXXXXXXXXXXX-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
W/System: ClassLoader referenced unknown path: /data/app/XXXXXXXXXXXXXX-1/lib/arm
I/InstantRun: starting instant run server: is main process
W/art: Verification of void XXXXXXXXXXXXXX.MainActivity.injectIdleStatusFragment() took 2.665s
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/nanodlp.log: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:58)
W/System.err:     at XXXXXXXXXXXXXX.utils.Util.writeToLogFile(Util.java:113)
W/System.err:     at XXXXXXXXXXXXXX.MainActivity.onCreate(MainActivity.java:222)
W/System.err:     at android.app.Activity.performCreate(Activity.java:6251)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5422)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.Posix.open(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err:   ... 16 more
V/XXXXXXXXXXXXXX.MainActivity: Permission is revoked
W/Activity: Can reqeust only one set of permissions at a time
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: XXXXXXXXXXXXXX, PID: 5426
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{XXXXXXXXXXXXXX/XXXXXXXXXXXXXX.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5422)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                      at com.afollestad.materialcamera.internal.BaseCaptureActivity.onRequestPermissionsResult(BaseCaptureActivity.java:336)
                      at android.app.Activity.requestPermissions(Activity.java:3823)
                      at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49)
                      at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372)
                      at XXXXXXXXXXXXXX.MainActivity.isStoragePermissionGranted(MainActivity.java:930)
                      at XXXXXXXXXXXXXX.MainActivity.onCreate(MainActivity.java:223)
                      at android.app.Activity.performCreate(Activity.java:6251)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5422) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

call the askForMarshmallowPermissions method inside splash screen on create method.
to grant danger premissions at run time
you have to added External store read and camera premission.


 Add premissions in Manifest.xml
 <uses-permission
        android:name="android.permission.CAMERA"
        android:required="false" />

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

askForMarshmallowPermissions(); call method in onCreate()

 private void askForMarshmallowPermissions() {
        List<String> permissionsNeeded = new ArrayList<String>();

        final List<String> permissionsList = new ArrayList<String>();


        if (!addPermission(permissionsList, android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
            permissionsNeeded.add("Write to storage");

        if (!addPermission(permissionsList, Manifest.permission.CAMERA))
            permissionsNeeded.add("Camera");

        if (permissionsList.size() > 0) {
            if (permissionsNeeded.size() > 0) {
                // Need Rationale
                String message = "You need to grant access to " + permissionsNeeded.get(0);
                for (int i = 1; i < permissionsNeeded.size(); i++)
                    message = message + ", " + permissionsNeeded.get(i);
                showMessageOKCancel(message,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(SplashScreen.this,
                                        permissionsList.toArray(new String[permissionsList.size()]),
                                        CommonUtils.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                            }
                        });
                return;
            }
            ActivityCompat.requestPermissions(SplashScreen.this, permissionsList.toArray(new String[permissionsList.size()]),
                    CommonUtils.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            return;
        }
        callAfterPermission();


    }

       private boolean addPermission(List<String> permissionsList, String permission) {
        if (ContextCompat.checkSelfPermission(SplashScreen.this, permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (!ActivityCompat.shouldShowRequestPermissionRationale(SplashScreen.this, permission))
                return false;
        }
        return true;
    }


        private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(SplashScreen.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CommonUtils.showAlertDialogWithFinishActivity(SplashScreen.this,
                                "Please grant all required permissions to " +
                                        "continue running the app.",
                                CommonUtils.ALERT_TITLE, true, CommonUtils.NO_RESULT, null);
                    }
                })
                .create()
                .show();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case CommonUtils.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
                Map<String, Integer> perms = new HashMap<String, Integer>();

                perms.put(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                perms.put(android.Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED); 
                perms.put(android.Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);

                // Fill with results
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for ACCESS_FINE_LOCATION
                if (perms.get(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    // All Permissions Granted
                    //insertDummyContact();
                    callAfterPermission();
                } else {


                    // todo display alert when user  Denied Permission

                }
            }
            break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }