Java Android:安装 APK 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42781934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:23:50  来源:igfitidea点击:

Android: Error while installing APKs

javaandroidandroid-studiofailed-installation

提问by Lucie P.

I'm slowly trying to do some simple tasks in Android Studio. The following app is installed on emulator without any errors. But when I tried to install it on a real device Redmi 3S this error occured:

我正在慢慢尝试在Android Studio. 以下应用程序安装在模拟器上,没有任何错误。但是当我尝试将其安装在真实设备 Redmi 3S 上时,出现了此错误:

Unknown failure (Failure - not installed for 0)
Error while Installing APKs

I went through similar questions around here but in these cases the error was caused by not enabled debugging, or not accepitng the app instalation. However, I allowed debugging and I also tried to install some other appin Studio and it worked fine.

我在这里遇到了类似的问题,但在这些情况下,错误是由未启用调试或不接受应用程序安装引起的。但是,我允许调试并且我还尝试在 Studio 中安装其他一些应用程序并且它运行良好

So the question probably is, what's wrong with the code.

所以问题可能是,代码有什么问题。

MainActivity.java

主活动.java

package tlacitko.button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

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

    public void sendMessage(View view) {
        new Thread(new Runnable() {
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            URL url = new URL("http://147.32.186.51:8080");
                           // HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                            InputStream is = url.openStream();
                            BufferedReader br = new BufferedReader(new
                                    InputStreamReader(is));
                            String s = "";
                        }catch(MalformedURLException ex){

                        }catch(IOException e){
                        }
                    }
                });
            }
        }).start();
    }
}

And the xml code:

和 xml 代码:

activity_main.xml

活动_main.xml

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Try to connect the server."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="16dp"
        android:onClick="sendMessage"
        android:text="Conncect"
        app:layout_constraintLeft_toRightOf="@+id/editText"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

采纳答案by JPZ

Follow these steps to overcome the issue.

请按照以下步骤解决问题。

  1. Disconnect all devices connected to system, and close all emulators running on System.
  2. Turn off Instant Run feature from settings.
  3. Perform a clean build.
  4. Turn on Instant Run feature from settings.
  5. Perform a clean build.
  6. Connect your device/start your emulator and ensure it is online.
  7. Run the project by selecting the device/emulator.
  1. 断开连接到系统的所有设备,并关闭系统上运行的所有模拟器。
  2. 从设置中关闭 Instant Run 功能。
  3. 执行干净的构建。
  4. 从设置中打开即时运行功能。
  5. 执行干净的构建。
  6. 连接您的设备/启动您的模拟器并确保它在线。
  7. 通过选择设备/模拟器运行项目。

Note:

笔记:

1) You should not have different instances of Android Debug Bridge(adb) running on system.

1) 您不应该在系统上运行不同的 Android Debug Bridge(adb) 实例。

2) If using Genymotion then make sure that you use the custom sdk path mentioned in the Genymotion settings the which you mentioned in the settings of Android Studio.

2) 如果使用 Genymotion,请确保您使用在 Android Studio 设置中提到的 Genymotion 设置中提到的自定义 sdk 路径。

These steps are likely to solve your issue, however it may also be a problem with android versions.

这些步骤可能会解决您的问题,但也可能是 android 版本的问题。

回答by alijandro

It might be the compatibility with Xiaomidevices for Android studio version 2.3and gradle plugin version 2.3.0with instant run on.

这可能是与XiaomiAndroid studio 版本2.3和 gradle 插件版本的设备兼容2.3.0,即时运行。

adb multiple-installthe splited apks return error.

adb multiple-install拆分的 apk 返回错误。

You can turn off instant run, or in Settings-> Developer options, toggle Turn on MIUI optimizationoff if you want instant run also.

您可以关闭即时运行,或者在Settings-> 中Developer optionsTurn on MIUI optimization如果您也想要即时运行,则将其关闭。

回答by Mahesh Cheliya

For Redmi and Mi devices turn off MIUI Optimization and reboot your phone.

对于 Redmi 和 Mi 设备,请关闭 MIUI 优化并重新启动手机。

Settings > Additional Settings > Developer Options > MIUI Optimization

设置 > 其他设置 > 开发者选项 > MIUI 优化

Check Answer here

在这里检查答案

回答by Animesh Patra

Turn off the Instant Run in Android Studio. Follow the steps below:

在 Android Studio 中关闭 Instant Run。请按照以下步骤操作:

  1. File >> Settings >> Build, Execution, Deployment.

  2. Click on Instant Runand unckeck "Enable Instant Run to hot swap code/resource changes on deploy".

  3. Click on "Apply" and "OK".

  1. 文件>>设置>>构建、执行、部署。

  2. 单击“即时运行”并取消选中“在部署时启用即时运行以热交换代码/资源更改”。

  3. 单击“应用”和“确定”。

Try running the application again. It should install the app now. You can re enable Instant Run again anytime.

再次尝试运行该应用程序。它现在应该安装该应用程序。您可以随时重新启用 Instant Run。

EDIT (Android Studio 3.0)

编辑(Android Studio 3.0)

Disable Instant Run (Android Document)

禁用 Instant Run(Android 文档

To disable Instant Run:

要禁用即时运行:

  1. Open the Settingsor Preferencesdialog. (For Mac, Android Studio-> Preferences)
  2. Navigate to Build, Execution, Deployment> Instant Run.
  3. Uncheck the box next to Enable Instant Run.
  1. 打开设置首选项对话框。(对于 Mac,Android Studio->首选项
  2. 导航到构建、执行、部署>即时运行
  3. 取消选中Enable Instant Run旁边的框。

回答by Smiles

I'm now faced with this problem, I have already uninstalled the apk, now I use Android Studio 3.0 want to run apk to Xiaomi MIX 2(Android 7.1.1, API 25), but it failed.

我现在面临这个问题,我已经卸载了apk,现在我使用Android Studio 3.0想运行apk到小米MIX 2(Android 7.1.1,API 25),但它失败了。

$ adb shell pm install -t -r "/data/local/tmp/com.package"
Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]

And Android Studio tells me "It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing." but I've uninstalled this apk.

Android Studio 告诉我“这个问题可能通过卸载现有版本的 apk 来解决,如果它存在,然后重新安装。” 但我已经卸载了这个 apk。

Android Studio popup info

Android Studio 弹出信息

And I click "OK" button, but it also failed.

我单击“确定”按钮,但它也失败了。

$ adb shell pm uninstall com.package
Unknown failure (at android.os.Binder.execTransact(Binder.java:565))
Error while Installing APK

And finally I find how to solve this problem, I don't turn on "Verify apps over USB", because now this device cannot turn on it until you login xiaomi account, and this is a new device that has not been logged in.

最后我找到了解决这个问题的方法,我没有打开“通过USB验证应用程序”,因为现在这个设备必须登录小米帐户才能打开它,而且这是一个尚未登录的新设备。

Step 1: Go to “Setting” → find “Developer options” in System, and click.

第一步:在“设置”→“系统”中找到“开发者选项”,点击。

Step 2: TURN ON “Verify apps over USB”in Debbuging section.

第 2 步:在调试部分打开“通过 USB 验证应用程序”

Step 3: Try “Run app” in Android Studio again!

第 3 步:再次尝试在 Android Studio 中“运行应用”!

回答by Mohammed Salim

For me I just made a cleanfor the App then everything works fine.

对我来说,我只是为应用程序做了一个清理,然后一切正常。

回答by GauRav MisHra

It may be your device storage is full. If you are testing on a physical device. However, if you are testing in Emulator then check there also that storage space is available or not. Otherwise, you have to turn-off the Instant Run.

可能是您的设备存储空间已满。如果您在物理设备上进行测试。但是,如果您在模拟器中进行测试,那么还要检查那里的存储空间是否可用。否则,您必须关闭 Instant Run。

回答by arnav

I also experienced the same issue when I was installing an app in a new device. This error usually occurs when the app installation is not allowed by the os. This might be because you might not have the authorization to install the app on your phone from just any source.

在新设备上安装应用程序时,我也遇到了同样的问题。当操作系统不允许安装应用程序时,通常会发生此错误。这可能是因为您可能无权从任何来源在手机上安装该应用程序。

So try checking the allow install apps via usbin developer options. If this doesn't work then try revoking all the usb debugging authorizationsand then try installing and provide permission when asked.

所以尝试检查allow install apps via usb开发人员选项。如果这不起作用,请尝试撤销所有usb debugging authorizations,然后尝试安装并在询问时提供权限。

If this also doesn't cut it try disabling instant run as explained in one of the answers above.

如果这也不能解决问题,请尝试按照上述答案之一禁用即时运行。

回答by Catalin

I had the same problem but my issue was lack of space on the device.

我遇到了同样的问题,但我的问题是设备空间不足。

Try this On your device go to Settings -> Device maintenance -> Optimise now

试试这个 在您的设备上转到设置 -> 设备维护 -> 立即优化

Once that is done, try again.

完成后,再试一次。

回答by happy_Coding

Before try all the above suggestion, check enough memory space available in device. This will also reason this problem.

在尝试上述所有建议之前,请检查 device 中有足够的可用内存空间。这也会导致这个问题。

Error: android.os.ParcelableException: java.io.IOException: Requested internal only, but not enough space

错误:android.os.ParcelableException:java.io.IOException:仅内部请求,但空间不足

Happy coding :)

快乐编码:)