Silent install APK programmatically - java

Sorry for my poor English. I want to figure out how to install (or delete) APK file silently without root programmatically.
First of all I added android:sharedUserId="android.uid.system" to manifest, and permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" tools:ignore="ProtectedPermissions"/>
Code of installing and deleting
public void installApp(File file){
try {
final String command = "pm install " + file.getPath();
Process proc = Runtime.getRuntime().exec(new String[] {command });
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteApp(String appPackage){
try {
final String command = "pm uninstall " + appPackage;
Process proc = Runtime.getRuntime().exec(new String[] {command });
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
And as I know I need manufacturer keys to sign my App. I didn't found keys for Android Studio emulators, so for example I downloaded an image of Android 4.4 r2 from here http://www.android-x86.org/releases/releasenote-4-4-r2 (and mount it in Oracle VM) and got keys from here https://sourceforge.net/p/android-x86/build/ci/android-x86-4.4-r2/tree/target/product/security/ .As I understood platform.x509.pem and platform.pk8 are keys what I needed.
I signed my App with signapk.jar something like this java -jar signapk.jar platform.x509.pem platform.pk8 app.apk signapp.apk.
But it doesn't work. Some of attempts ended with error:
Uninstall
07-30 04:42:46.050 1477-1477/com.jinga.jihome W/System.err: java.io.IOException: Error running exec(). Command: [pm uninstall ru.bogdanov.mom] Working Directory: null Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
07-30 04:42:46.060 1477-1477/com.jinga.jihome W/System.err: at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:128)
at com.jinga.jihome.updater.packageInstaller.PackageInstallerHelper.deleteApp(PackageInstallerHelper.java:59)
at com.jinga.jihome.MainActivity.onCreate(MainActivity.java:102)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-30 04:42:46.070 1477-1477/com.jinga.jihome W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.IOException: Permission denied
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
Install
07-30 04:42:49.420 1477-1477/com.jinga.jihome W/System.err: java.io.IOException: Error running exec(). Command: [pm install /storage/sdcard/app-debug.apk] Working Directory: null Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
at java.lang.Runtime.exec(Runtime.java:173)
07-30 04:42:49.430 1477-1477/com.jinga.jihome W/System.err: at java.lang.Runtime.exec(Runtime.java:128)
at com.jinga.jihome.updater.packageInstaller.PackageInstallerHelper.installApp(PackageInstallerHelper.java:49)
at com.jinga.jihome.MainActivity$2.onSuccess(MainActivity.java:135)
at com.jinga.jihome.MainActivity$2.onSuccess(MainActivity.java:126)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.IOException: No such file or directory
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
Some of signed apks doesn't want to install with error like App conflicts with existing package by the same nameor what my device not incompatible for this Apk, but without signing all ok.
I've tried with different images, key pairs and emulators, but not succeed, what I did wrong?

I too have an app which installs and updates apks on client devices.
It is also signed with the platform signing keys which means we have permission to install this on their players!
On Android 8.x I've had to set
android:sharedUserId="android.uid.shell"
in the to make it work. I'm using the command:
File localApk = new File("apkName.apk");
String[] Commands = {"pm", "install", "-r", localApk.getAbsolutePath()};
Runtime.getRuntime().exec(Commands);
Without the shareUserId being set I always got permission errors.

Permissions defined in app's manifest have nothing to do with shell commands. They guard the Java API.
DELETE_PACKAGES permission guards the PackageManager#deletePackage method. It's not part of the public SDK so you'll have to access it using reflection (or compile your app against unhidden android.jar).
The reflection path
You'll need to copy this interface to your project:
package android.content.pm;
interface IPackageDeleteObserver {
void packageDeleted(String packageName, int returnCode);
}
Then call the deletePackage method using reflection. Here's a sample in Kotlin:
private val deletePackageMethod = PackageManager::class.java.getDeclaredMethod(
"deletePackage",
String::class.java,
IPackageDeleteObserver::class.java,
Int::class.javaPrimitiveType
)
#RequiresPermission(Manifest.permission.DELETE_PACKAGES)
fun PackageManager.deletePackage(
packageName: String,
observer: IPackageDeleteObserver,
flags: Int
) {
deletePackageMethod.invoke(this, packageName, observer, flags)
}
You can find the flags and return codes in PackageManager source code as constants prefixed with DELETE_.
So far this is all tested and verified.
Notes: Reflection is extra work, so we do the method lookup only once. The method is hidden but public so you don't need to call setAccessible(true).
Warning: Please test this on Android 9, I can't guarantee this works with the ban on accessing hidden API. I think since your app is signed with the system signature and runs as the system user, it should have no problem.
The unhide path
If you compile against the modified android.jar you can directly reference types mentioned above.
The only problem I encountered was that gradle task mockableAndroidJar is not compatible with modified android.jar. You'll have to exclude that task from execution by adding -x mockableAndroidJar to gradle command line in the IDE.
I haven't actually tried it, I can't tell you if the IDE will let you any further.

Related

Attempt to invoke virtual method 'java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)' on a null object reference

I updated my android studio from 3.1.2 to 3.1.3 and updated my project sdk version to 28 and when i run the application it crashes as soon as it open and throws the following exception,
FATAL EXCEPTION: main
Process: com.google.android.apps.nexuslauncher, PID: 10288
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)' on a null object reference
at android.os.Parcel.createException(Parcel.java:1948)
at android.os.Parcel.readException(Parcel.java:1865)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4541)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1660)
at android.app.Activity.startActivityForResult(Activity.java:4574)
at com.android.launcher3.Launcher.startActivityForResult(SourceFile:1394)
at android.app.Activity.startActivity(Activity.java:4889)
at com.android.launcher3.Launcher.startActivitySafely(SourceFile:2000)
at com.android.launcher3.Launcher.startAppShortcutOrInfoActivity(SourceFile:1850)
at com.android.launcher3.Launcher.onClick(SourceFile:1703)
at android.view.View.performClick(View.java:6579)
at android.view.View.performClickInternal(View.java:6556)
at android.view.View.access$3100(View.java:777)
at android.view.View$PerformClick.run(View.java:25660)
at android.os.Handler.handleCallback(Handler.java:819)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: android.os.RemoteException: Remote stack trace:
at android.app.LoadedApk.createAppFactory(LoadedApk.java:224)
at android.app.LoadedApk.<init>(LoadedApk.java:152)
at android.app.ActivityThread.getPackageInfo(ActivityThread.java:2043)
at android.app.ActivityThread.getPackageInfo(ActivityThread.java:1998)
at android.app.ActivityThread.getPackageInfo(ActivityThread.java:1970)
It is a problem of API P emulator. On API 28, 27 it works right.
As CoolMind said, It is a problem in API P emulator, you can solve it by updating your Google APIs Intel System Image in Android SDK.
Add these two lines in your gradle.properties file and then build project. After i added these lines, Android Studio showed stacktrace in Logcat.I fixed those issues and then my app started working.
android.enableD8.desugaring = true
android.enableR8 = true

java.lang.NoClassDefFoundError: com.google.android.gms.common.internal.zzbo

I know this question may be duplicate but I have tried many solutions but I am unable to make my app work. It should be noted that I have enabled Multidex but still getting below error. I am creating a react native app.
FATAL EXCEPTION: main
Process: com.xxxxxx.yyyyyyyy, PID: 3978
java.lang.NoClassDefFoundError: com.google.android.gms.common.internal.zzbo
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5049)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4623)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4563)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1402)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
If you are developing a react native application and above error came , just make sure that all of your dependencies's Gradle are using same version of firebase servces and play service base.
For example :- my app level gradle file contains
compile "com.google.android.gms:play-services-base:11.4.0"
compile "com.google.firebase:firebase-core:11.4.0"
compile "com.google.firebase:firebase-messaging:11.4.0"
compile "com.google.firebase:firebase-analytics:11.4.0"
compile "com.google.firebase:firebase-crash:11.4.0"
compile "com.google.firebase:firebase-ads:11.4.0"
and one react-native library is using
compile "com.google.android.gms:play-services-base:11.8.0"
and other one library is using
compile "com.google.android.gms:play-services-base:+"
so what i did , i changed all services version to 11.8.0.
BTW you can see gradles of all libraries or dependencies when you open your react-native-project/android folder in Android studio
I hope this will solve your issue

Didn't find class "com.google.android.gms.common.app.GmsApplication"

I have got a problem when running my app on Android 4.4 with Google Play Services version 100884030. I get the following error:
FATAL EXCEPTION: main
Process: com.google.android.gms, PID: 8678
java.lang.RuntimeException: Unable to instantiate application com.google.android.gms.common.app.GmsApplication: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.app.GmsApplication" on path: DexPathList[[zip file "/system/framework/com.android.media.remotedisplay.jar", zip file "/system/framework/com.android.location.provider.jar", zip file "/system/framework/com.google.android.ble.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:507)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.app.GmsApplication" on path: DexPathList[[zip file "/system/framework/com.android.media.remotedisplay.jar", zip file "/system/framework/com.android.location.provider.jar", zip file "/system/framework/com.google.android.ble.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newApplication(Instrumentation.java:975)
at android.app.LoadedApk.makeApplication(LoadedApk.java:502)
... 11 more
My Gradle settings:
compileSdkVersion 25
buildToolsVersion "25.0.2"
minSdkVersion 14
targetSdkVersion 22
....
compile 'com.google.android.gms:play-services-analytics:10.2.1'
compile 'com.google.android.gms:play-services-ads:10.2.1'
So you see what is the problem?
Thanks !!!
I had an identical problem, I think, and it turned out that the .apk on the device was corrupted. Maybe by an internal flash storage (hardware) problem?
This is what I did:
Turn on Developer Settings on the device (by tapping the Build Number seven times in the About screen of Settings).
From the Developer Settings menu that appeared, turn on USB Debugging.
Connect to a PC with adb installed.
Find the path of the existing Google Play Services apk on the device, with adb shell pm path com.google.android.gms
Use that path to pull the existing apk from the device, to check it, with adb pull /data/app/com.google.android.gms-1/base.apk (replace the path here with what you got at point 4).
Check if the apk is broken, by changing its extension to .zip, and doing a zip integrity test. In my case it said classes2.dex bad CRC.
Download a good apk from somewhere trusted. Another identical device would be perfect, but I didn't have one, so I got mine from apkmirror.com (make sure you pick the right variant, there are a number of them for the same version number). The one I downloaded had the same number of bytes as the one pulled from the phone, but a segment of data inside was different.
Install the downloaded apk with adb install -r <downloaded file>.
Reboot the device.
That's it. Everything worked after this.

java.lang.NoClassDefFoundError: android.app.usage.UsageStatsManager in android

Hi i am using UsageStatsManager api to show some usage list to user. But i am facing some issue. i am getting this error in many device, in some device it's working fine. i am still surfing about this issue. can you guys please help me to solve this.
Error:
java.lang.NoClassDefFoundError: android.app.usage.UsageStatsManager
at com.yourapp.fragment.AppUsageStatisticsFragment.onViewCreated(AppUsageStatisticsFragment.java:64)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5463)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
at dalvik.system.NativeStart.main(Native Method)
Code i have used:
private UsageStatsManager mUsageStatsManager;
//i am getting error at below line.
mUsageStatsManager = (UsageStatsManager) getActivity()
.getSystemService("usagestats"); //Context.USAGE_STATS_SERVICE
Manifest:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions"/>
Check my app.
UsageStatsManager Added in API level 21 this is not a part of your appcompact API. So that's why before API level 21 you will get the ClassNotFoundException
UsageStatsManager was Added in API level 21 so you will get this ClassNotFoundException on preLollipop.
Also some samsung and other devices throws ClassNotFoundException on lollipop too as they haven't included that class, You can only safely use the class in API 22

Android/Adobe air crash report: java.lang.UnsatisfiedLinkError

i am getting this error inside Google's "Crashes & ANRs". The application built with Adobe Air and packaged as captive runtime.
java.lang.UnsatisfiedLinkError: nativeSurfaceCreated
at com.adobe.air.AIRWindowSurfaceView.nativeSurfaceCreated(Native Method)
at com.adobe.air.AIRWindowSurfaceView.surfaceCreated(AIRWindowSurfaceView.java:719)
at android.view.SurfaceView.updateWindow(SurfaceView.java:545)
at android.view.SurfaceView.access$000(SurfaceView.java:81)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:693)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1731)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2583)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4508)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Does anybody know what this means?
The UnsatisfiedLinkError for Java usually means that you tried to load a library called nativeSurfaceCreated. Basically your code called a method that called a method and so on, and some code somebody else wrote called a native method. This native method loaded a library called nativeSurfaceCreated, which you don't have. This is usually packaged in a dll in Windows or a so file in Linux. If you can find this dll/so file, you can probably run whatever you're running.

Categories