简体   繁体   中英

Error when Android Studio tries to install apk

I'm pretty new to Android and was trying out a simple app where you take a picture and send it to another activity for editing. I am using Android Studio and OpenCV 3.1 for this. Everything was working. But then as soon as I started writing the code for the second activity, it fails to install the apk on my tablet with the following error:

The currently selected variant "x86-debug" uses split APKs, but none of the 1 split apks are compatible with the current device with density "-1" and ABIs "". Error while Installing APK

Strange thing is, the activity was being called just before I modified the onCreate method. Thinking I messed up something, I disabled everything, cleaned and tried again. Now the app refuses to install, even though it is in the same state as it was when everything was working.

My second activity is as follows:

public class SecondaryActivity extends Activity{

    public static final String PHOTO_MIME_TYPE = "image/png";
    public static final String EXTRA_PHOTO_URI = "com.example.morpheus.myapplication.SecondaryActivity.extra.PHOTO_URI";
    public static final String EXTRA_PHOTO_DATA_PATH = "com.example.morpheus.myapplication.SecondaryActivity.extra.PHOTO_DATA_PATH";
    private Uri mUri;
    private String mDataPath;
    private Mat photo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = getIntent();
        long addr = intent.getLongExtra("img_addr", 0);
        photo = new Mat(addr);

        mUri = intent.getParcelableExtra(EXTRA_PHOTO_URI);
        mDataPath = intent.getStringExtra(EXTRA_PHOTO_DATA_PATH);

        final ImageView imageView = new ImageView(this);
        imageView.setImageURI(mUri);

        setContentView(imageView);

    }
}

The place where the above activity is being called is:

        final ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, photoPath);
        values.put(MediaStore.Images.Media.MIME_TYPE, SecondaryActivity.PHOTO_MIME_TYPE);
        values.put(MediaStore.Images.Media.TITLE, appName);
        values.put(MediaStore.Images.Media.DESCRIPTION, appName);
        values.put(MediaStore.Images.Media.DATE_TAKEN, currentTime);

        File album = new File(albumPath);
        if(album.isDirectory() && !album.mkdirs()){
            Log.e(TAG, "Failed to create album directory at " + albumPath);
            onTakePhotoFailed();
            return;
        }
        Imgproc.cvtColor(rgba, mBGR, Imgproc.COLOR_RGBA2BGR, 3);
        if(!Imgcodecs.imwrite(photoPath, mBGR)){
            Log.e(TAG, "Failed to save photo to " + photoPath);
            onTakePhotoFailed();
        }
        Log.d(TAG, "Photo saved successfully to " + photoPath);

        Uri uri;
        try{
            uri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } catch (final Exception e){
            Log.e(TAG, "Failed to insert photo into MediaStore");
            e.printStackTrace();
            File photo = new File(photoPath);
            if(!photo.delete()){
                Log.e(TAG, "Failed to delete non-inserted photo!");
            }

            onTakePhotoFailed();
            return;
        }

        final Intent intent = new Intent(this, SecondaryActivity.class);
        intent.putExtra(SecondaryActivity.EXTRA_PHOTO_URI, uri);
        intent.putExtra(SecondaryActivity.EXTRA_PHOTO_DATA_PATH, photoPath);
        long addr = mBGR.getNativeObjAddr();
        intent.putExtra("img_addr", addr);

        startActivity(intent);
    }

The strangest thing is, even after reverting the codes to the point where it worked, it is not installing. I tried manually uninstalling the app from the tablet. But that doesn't help either.

I am using an Asus ZenPad 8 with an Intel Atom processor.

Any help would be highly appreciated as my entire project is stuck because of this.

This could be related with NDK and Build Variants :

构建变体

People at JUCE community has a way to solve this, read more at Android Studio tips, tricks & known issues .

As side note, try to splitting your apk to their respective variants with the following code in your app build.gradle:

apply plugin: 'com.android.application'

android {

  compileSdkVersion 23
  buildToolsVersion "23.0.1"
  ...
  ...

  // Split APK Start here
  splits {
    abi {
      enable true
      reset()
      include 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips'
      universalApk true
    }
  }

  project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]

  android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
      output.versionCodeOverride =
          project.ext.versionCodes.get(output.getFilter(
              com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode
    }
  }
  // Split APK End here
  ...
  ...
}

It will give you apk for each architecture and also will make your apk smaller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM