Android 中的 execv(/system/bin/dex2oat) 失败

Posted

技术标签:

【中文标题】Android 中的 execv(/system/bin/dex2oat) 失败【英文标题】:Failed execv(/system/bin/dex2oat ) in Android 【发布时间】:2017-09-01 04:57:00 【问题描述】:

activity_main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_menu_layout"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fyp.hkust.facet.activity.MainMenuActivity"
android:background="@drawable/background_dark_circle">


<ImageButton
    android:layout_
    android:layout_
    android:background="@mipmap/ic_location_on_white_36dp"
    android:id="@+id/store_button"
    android:layout_below="@+id/photo_camera"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageButton
    android:layout_
    android:layout_
    android:background="@mipmap/ic_favorite_white_36dp"
    android:id="@+id/fav_button"
    android:layout_below="@+id/photo_camera"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/location_text"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="@string/store_text"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/store_button"
    android:layout_alignEnd="@+id/store_button"
    android:layout_below="@+id/store_button" />

<TextView
    android:id="@+id/fav_text"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="@string/favourite_text"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:layout_below="@+id/fav_button"
    android:layout_alignLeft="@+id/fav_button"
    android:layout_alignStart="@+id/fav_button" />

<TextView
    android:id="@+id/account_text"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="@string/my_account_text"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:layout_below="@+id/account_button"
    android:layout_alignLeft="@+id/account_button"
    android:layout_alignStart="@+id/account_button" />

<TextView
    android:id="@+id/photo_camera_text"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="@string/facet_match_text"
    android:layout_marginLeft="5dp"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:layout_above="@+id/fav_text"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="58dp" />

<ImageButton
    android:layout_
    android:layout_
    android:background="@mipmap/ic_photo_camera_white_36dp"
    android:id="@+id/photo_camera"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

<ImageButton
    android:layout_
    android:layout_
    android:background="@mipmap/ic_account_circle_white_36dp"
    android:id="@+id/account_button"
    android:layout_alignTop="@+id/shopping_button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

<TextView
    android:id="@+id/product_text"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="@string/all_products_text"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignBottom="@+id/account_text"
    android:layout_below="@+id/account_button"
    android:layout_alignRight="@+id/shopping_button"
    android:layout_alignEnd="@+id/shopping_button" />

<ImageButton
    android:layout_
    android:layout_
    android:background="@mipmap/ic_shopping_basket_white_36dp"
    android:id="@+id/shopping_button"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="5dp"
    android:layout_above="@+id/photo_camera"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

MainMenuActivity.java

public class MainMenuActivity extends AppCompatActivity 

private static final String TAG = MainMenuActivity.class.getSimpleName();
private static final int CAM_REQUEST = 3;
private static String[] PERMISSIONS_REQ = 
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA
;
private static final int REQUEST_CODE_PERMISSION = 2;
private View activity_main_menu_layout;
private ImageButton shoppingBtn;
private ImageButton photoCameraBtn;
private ImageButton accountBtn;
private ImageButton storeBtn;

private static final int GALLERY_REQUEST = 1;
private static int OVERLAY_PERMISSION_REQ_CODE = 3;
private ImageButton favBtn;
private int buttonNumber = 0;
private String captureImageFullPath = null;

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

    activity_main_menu_layout = (RelativeLayout) findViewById(R.id.activity_main_menu_layout);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
        Window window = getWindow();
        // Translucent status bar
        window.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
    Typeface fontType = FontManager.getTypeface(getApplicationContext(), FontManager.APP_FONT);
    FontManager.markAsIconContainer(findViewById(R.id.activity_main_menu_layout), fontType);

    photoCameraBtn = (ImageButton) findViewById(R.id.photo_camera);
    shoppingBtn = (ImageButton) findViewById(R.id.shopping_button);
    accountBtn = (ImageButton) findViewById(R.id.account_button);
    storeBtn = (ImageButton) findViewById(R.id.store_button);
    favBtn = (ImageButton) findViewById(R.id.fav_button);

    photoCameraBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 

            showAlertDialog();
        
    );

    shoppingBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Intent Intent = new Intent(MainMenuActivity.this, MainActivity.class);
            startActivity(Intent);
        
    );

    accountBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 

            Intent accountIntent = new Intent(MainMenuActivity.this, ProfileActivity.class);
            startActivity(accountIntent);

        
    );

    storeBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            showMakeUpDialog();
        
    );

    favBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 

            Intent storeIntent = new Intent(MainMenuActivity.this, ShopListActivity.class);
            startActivity(storeIntent);

        
    );


    // Just use hugo to print log
    isExternalStorageWritable();
    isExternalStorageReadable();

    // For API 23+ you need to request the read/write permissions even if they are already in your manifest.
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;

    if (currentapiVersion >= Build.VERSION_CODES.M) 
        verifyPermissions(this);
    

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
        if (!Settings.canDrawOverlays(this.getApplicationContext())) 
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
        
    

    if (!isMyServiceRunning(MyService.class))
        startService(new Intent(this, MyService.class));




private void showAlertDialog() 

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose the way to get your selfie");

    builder.setIcon(R.mipmap.app_icon);
    builder.setCancelable(true);

    final String[] items = new String[]"From Gallery", "Take Photo";
    final Integer[] icons = new Integer[]R.mipmap.app_icon, R.mipmap.app_icon;
    ListAdapter adapter = new ArrayAdapterWithIcon(getApplication(), items, icons);

    builder.setAdapter(adapter, new DialogInterface.OnClickListener() 
        public void onClick(DialogInterface dialog, int item) 
            switch (item) 
                case 0: 
                    buttonNumber = 1;
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, GALLERY_REQUEST);
                    break;
                
                case 1: 
                    Intent cameraViewIntent = new Intent(MainMenuActivity.this, ShowCameraViewActivity.class);
                    startActivity(cameraViewIntent);
                    break;
                
            

        
    ).show();


private void showMakeUpDialog() 

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose the way to get your selfie");

    builder.setIcon(R.mipmap.app_icon);
    builder.setCancelable(true);

    final String[] items = new String[]"From Gallery", "Take Photo";
    final Integer[] icons = new Integer[]R.mipmap.app_icon, R.mipmap.app_icon;
    ListAdapter adapter = new ArrayAdapterWithIcon(getApplication(), items, icons);

    builder.setAdapter(adapter, new DialogInterface.OnClickListener() 
        public void onClick(DialogInterface dialog, int item) 
            switch (item) 
                case 0: 
                    buttonNumber = 2;
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, GALLERY_REQUEST);
                    break;
                
                case 1: 
                    Intent cameraViewIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = getFile();
                    cameraViewIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                    startActivityForResult(cameraViewIntent, CAM_REQUEST);
                    break;
                
            

        
    ).show();


private boolean isMyServiceRunning(Class<?> serviceClass) 
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
        if (serviceClass.getName().equals(service.service.getClassName())) 
            return true;
        
    
    return false;


private File getFile() 

    File folder = new File("sdcard/FaceT");

    if (!folder.exists()) 
        folder.mkdir();
    

    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    captureImageFullPath = folder + "/makeup_" + currentDateTimeString;
    File imageFile = new File(captureImageFullPath);
    return imageFile;


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
            if (!Settings.canDrawOverlays(this.getApplicationContext())) 
                Snackbar snackbar = Snackbar
                        .make(activity_main_menu_layout, "CameraActivity\", \"SYSTEM_ALERT_WINDOW, permission not granted...", Snackbar.LENGTH_SHORT);
                snackbar.show();
             else 
                Intent intent = getIntent();
                finish();
                startActivity(intent);
            
        
    

    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) 
        Uri pickedImage = data.getData();
        Log.d(TAG, "selected!!!" + " : " + pickedImage.getPath());
        // Let's read picked image path using content resolver
        String[] filePathColumn = MediaStore.Images.Media.DATA;

        Cursor cursor = getContentResolver().query(pickedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        Log.d(TAG + "Path:", picturePath);
        Intent intent = new Intent();
        if (buttonNumber == 1)
            intent.setClass(MainMenuActivity.this, CaptureActivity.class);
        else if (buttonNumber == 2)
            intent.setClass(MainMenuActivity.this, ColorizeFaceActivity.class);
        intent.putExtra("path", picturePath);
        //intent.putExtra("color" , "" + mBlobColorHsv);
        startActivity(intent);
     else if (requestCode == CAM_REQUEST) 
        Intent intent = new Intent();
        intent.setClass(MainMenuActivity.this, ColorizeFaceActivity.class);
        intent.putExtra("path", captureImageFullPath);
        startActivity(intent);
    



/**
 * Checks if the app has permission to write to device storage or open camera
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
private static boolean verifyPermissions(Activity activity) 
    // Check if we have write permission
    int write_permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int read_persmission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int camera_permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);

    if (write_permission != PackageManager.PERMISSION_GRANTED ||
            read_persmission != PackageManager.PERMISSION_GRANTED ||
            camera_permission != PackageManager.PERMISSION_GRANTED) 
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_REQ,
                REQUEST_CODE_PERMISSION
        );
        return false;
     else 
        return true;
    


/* Checks if external storage is available for read and write */
private boolean isExternalStorageWritable() 
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) 
        return true;
    
    return false;


/* Checks if external storage is available to at least read */
private boolean isExternalStorageReadable() 
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
            Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
        return true;
    
    return false;



logcat 中的消息:

04-06 16:47:17.794 934-934/? I/art: 后期启用 -Xcheck:jni 04-06 16:47:17.945 934-934/fyp.hkust.facet W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --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 --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/fyp.hkust.facet-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik -cache/arm/data@app@fyp.hkust.facet-2@split_lib_dependencies_apk.apk@classes.dex) 因为非 0 退出状态 04-06 16:47:18.685 934-934/fyp.hkust.facet W/art: /data/app/fyp.hkust.facet-2/split_lib_dependencies_apk.apk 有超过 100 个 dex 文件。请考虑合并和缩小数字以避免运行时开销。

04-06 16:47:18.851 934-934/fyp.hkust.facet W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --指令集=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime- arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/fyp.hkust.facet-2/split_lib_slice_0_apk.apk --oat-file=/ data/dalvik-cache/arm/data@app@fyp.hkust.facet-2@split_lib_slice_0_apk.apk@classes.dex) 因为非0退出状态

04-06 16:47:18.919 934-934/fyp.hkust.facet W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --指令集=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime- arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/fyp.hkust.facet-2/split_lib_slice_1_apk.apk --oat-file=/ data/dalvik-cache/arm/data@app@fyp.hkust.facet-2@split_lib_slice_1_apk.apk@classes.dex) 因为非0退出状态

04-06 16:47:18.995 934-934/fyp.hkust.facet W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --指令集=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime- arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/fyp.hkust.facet-2/split_lib_slice_2_apk.apk --oat-file=/ data/dalvik-cache/arm/data@app@fyp.hkust.facet-2@split_lib_slice_2_apk.apk@classes.dex) 因为非0退出状态

。 . .

04-06 16:47:19.590 934-934/fyp.hkust.facet W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --指令集=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime- arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/fyp.hkust.facet-2/split_lib_slice_9_apk.apk --oat-file=/ data/dalvik-cache/arm/data@app@fyp.hkust.facet-2@split_lib_slice_5_apk.apk@classes.dex) 因为非0退出状态

当我进入这个活动时,我的手机需要加载很多我不知道它们是什么的东西(在我的 logcat 中)。我不知道他们是什么意思。任何人都可以解决这个问题吗?非常感谢。

【问题讨论】:

/data/app/fyp.hkust.facet-2/split_lib_dependencies_apk.apk has in excess of 100 dex files. Please consider coalescing and shrinking the number to avoid runtime overhead. 我可以采取什么措施来合并和缩小 dex 文件? dex文件在哪里?谢谢 我实际上没有得到这个问题的答案。只是给了进一步研究的提示 @ng2b30,你有没有找到解决这个错误的方法? @Hemang no =[。这让我很烦。我认为这应该是我的资源造成的,我应该更多地关注这一点。 【参考方案1】:

这基本上是说你有太多的代码/资源作为你的编译输出。

有两个与此相关的主题,如果我看到你的build.gradle(特别是依赖项)会更容易回答,但这里是:

    通常情况下,您的应用程序会引入大量依赖项,但实际上并未使用其中的每一段代码,因此可以安全地删除未使用的内容。 这里的答案是Proguard

    如果仍然没有帮助,您需要Multidex 将您的应用拆分为多个输出文件。

话虽如此,请不惜一切代价避免使用 Multidex:它会减慢构建过程并为您的应用带来更多复杂性。它适用于那些您没有任何其他选择的极少数情况。

【讨论】:

以上是关于Android 中的 execv(/system/bin/dex2oat) 失败的主要内容,如果未能解决你的问题,请参考以下文章

如何在不脱离控制台的情况下在 Windows 中的 Python 中执行 os.execv()?

execv() 和 const-ness

execv() 和 fork() 的时间浪费

execl vs execv,参数大小未知

如何将字符串向量传递给execv

如何使用 execv 生成后台程序