Compile signed APK and install it directly from IDE? - android

Does anyone know the way or the trick to make signed APK and install it on device from inside the IDE? Just like when we sign the app via debug key and then automatically install it on the device from the IDE.
When I test in-app and have to make multiple changes, it's slowing me down the fact that each time I have to install the APK by coping it onto the device or via console.

You can have an artifact that will build a signed APK.
Then create an External Tool (batch file) that will upload the artifact to the device using adb install command. Optionally add the code to start the main activity. My install.bat looks like this:
adb install -r bin\ClockSync.apk
adb shell am start -n "ru.org.amip.ClockSync/ru.org.amip.ClockSync.view.Main"
Modify the commands if you want to deploy to the specific emulator/device.
External tool configuration is pretty straightforward, just run the bat file.
Now you can create a Run configuration with Before Launch section set to build the signed APK artifact and start the external tool to deploy and run it.
EDITED by sandalone
This approach is for IntelliJ IDEA users. After you set it all up, just launch the app as usual Shift + F10 and the app will be built as signed APK, installed on the device and started.
Since I am on Linux, I made the script install.sh with the following content
adb -s DEVICE_ID install -r ./com.package.name.apk
adb shell am start -n "com.package.name/com.package.name.MainActivity"
made it executable and this was it. Of course, the script should be located in the same place as APK - otherwise fix the path accordingly.

Related

Update AOSP built-in system app

Lets say we have downloaded, built and flashed AOSP source code. Now a change to a system app is made (e.g. changed a constant in the packages/apps/Nfc package).
The next step is to build it, and there are two ways to do it:
cd packages/apps/Nfc; mm
or
mmm Nfc
This will create out/target/product//system/app/NfcNci/NfcNci.apk file
Which is the proper way to update the system app?
I tried using adb install NfcNci.apk but no success:
~/android/aosp-7.1.2-3.10-v2/out/target/product/kugo/system/app/NfcNci$ adb install NfcNci.apk
Failed to install NfcNci.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install com.android.nfcnci without first uninstalling.]
~/android/aosp-7.1.2-3.10-v2/out/target/product/kugo/system/app/NfcNci$ adb install -r NfcNci.apk
Failed to install NfcNci.apk: Failure [INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.android.nfcnci-1: Package /data/app/com.android.nfcnci-1/base.apk code is missing]
Found a way to easily 're-install' the app itself (let's put Settings app as an example):
mmm packages/apps/Settings # Build the module
adb root ; adb remount # Restart adbd as root and mount /system as writable
adb push out/target/product/<device_name>/system/priv-app/Settings /system/priv-app # Push the built files to the device
Then force-close and restart the app (by swiping it from Recents). No need to reboot device in order to take changes
NOTE: Depending on the app, the path may be on system/app instead of /system/priv-app
Another way is to:
copy the new apk to the sdcard of the device with adb push
mount /system read write: mount -o rw,remount,rw /system
copy your new .apk from /sdcard over your old .apk in /system/app
remove the .odex file of your old .apk
reboot the device
For development you can use a simple script for this steps.
Check build/envsetup.sh file - there's also commands mmp and mmmp to build and push a module to connected device. Also adb sync can be used to sync whole image, so if you updated a module, changed files will be pushed to device.
Also you can put the changed files via regular `adb push' and reboot device.
I do not know if system apps can be updated via adb install, probably yes, but I think you need to increment build number in the manifest file.
I do not think you can uninstall a system app with adb install -r, as apps can't be removed from system partition, only from data. I do not know why you are getting INSTALL_FAILED_INVALID_APK in that case, may be because app manager can't uninstall the base system apk indeed.

Manually installing an updated APK fails with "signatures do not match the previously installed version"

I've built a silly app to share among a few friends. No need to put it up on the app-store.
I built the first apk (signed), uploaded it to a web-server and all worked well.
A small issue arose, I fixed it, re-built, signed with the same keystore and uploaded it again. It now seems that I am unable to install from the new apk. The debugger tells me:
signatures do not match the previously installed version
So I uninstalled the old version by opening the app drawer and dragging it onto the "uninstall" button. When opening "Settings -> Apps", I don't see it anywhere anymore. I don't see any traces of the app on my device.
Yet I still get the above error message.
Is it possible some information still lingers on the device somewhere? Is there any way I can verify that?
Yes It is possible if somehow your old application is not removed 100% or its data is not removed.
Try:
adb uninstall "com.yourapp.yourapp"
If you don't know exactly what to put as replacement for "com.yourapp.yourapp", then you just open Android studio, Run your app while it is connected to a device and then look at Debug window.
It says:
Waiting for device.
Target device: samsung-sm_t531-xxxxxxxxx
Uploading file
local path: C:\Users\myapp\app\build\outputs\apk\myapp.apk
remote path: /data/local/tmp/com.myapp.myapp
Installing com.myapp.myapp
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.myapp.myapp"
pkg: /data/local/tmp/com.myapp.myapp
Success
com.myapp.myapp in this case is the name of the package you must use to uninstall.
I had the same issue and the adb uninstall solution did not work for me.
What worked was
On your device go to to Settings->Apps
Select your app, and in the menu select "Uninstall for all users"
Even if I had previously uninstalled the app it was still in the list there.
To me, if the app is meant to be distributed, the adb solution is a no-go: you can't ask one's friend to have the android sdk installed on their machine !
The way to go here is to edit the AndroidManifest.xml and to increment the android:versionCodeattribute in the <manifest>tag (which is the root element).
This would update your installed application
If you are seeing this while conducting connected tests, make sure to include .test when uninstalling via adb because uninstalling via app -> settings does not get rid of the test package
adb uninstall your.broken.package.test
if you just uninstall via
adb uninstall your.broken.package
your test package will still be there. This was only something i noticed while using the gradle command line, haven't come across this problem within android studio
Uninstall the old app from your phone or emulator and try to run again.
I got that error while trying to install release while signing it's certificate.
fixed with the :app Gradle task uninstallRelease and then installRelease again
If you are going in install the same app with a different signature, you may want to uninstall but keep the app's data.
adb -d shell pm uninstall -k <packageName>
adb -d install -r -t -d app.apk
For Unity users who come to this question, the best answer is indeed the one above by #Ehsan
adb uninstall "com.yourapp.yourapp"
I had already installed a previous version on my Android device then selected Development Build in Unity > Build Settings which caused the APK to use a different signature. If you install through the Android GUI it doesn't actually remove everything so you have to use ADB.
I had face same problem With POCO Mobile and Moto G30 Mobile while developing application in flutter
My Solution is:
I have open android project which automatically create in your project, in android studio and run project in connected device. It will automatically ask to uninstall old app with same package name and different signature. After click ok button it will install app.

Android Studio run app/test directly if nothing is changed

If nothing is changed since last compile and run, and then I hit run again (green start button), it complies and then run.
How can I make it run directly without compiling the same thing?
I might be wrong, I don't think but it's possible when you pass by Android Studio, but by using the build apk and install it with Android Debug Bridge (ADB), you should get the desired behaviour.
When the project compiles, AS creates an output apk. Usually, this should be named app-debug.apk and located in build/outputs/apk/ folder:
Your .apk file (signed with either a release or debug key) is in your module build/ directory after you build your application.
cf. Running on emulator
By using adb, you'll be able to install this apk previously generated (and I believe, without compiling again) on the device with the command install:
$ adb install -r path/to/app-debug.apk
Once installed, you should retrive the command to launch your application. A little research bring me to "How to start an Android application from the command line?":
$ adb shell
$ adb am start -n my.package.name/my.package.name.MyActivity
And then, you could combined them on same line to launch the apk just right after its installation. This looks like:
$ adb install -r path/to/app-debug.apk && adb shell am start -n my.package.name/my.package.name.MyActivity
Therefore, you application will run without compiling.
First try right click on the project and then select Run As Android Application. This will change you app default run configuration.
If this does't work then try to change it's run configuration from right click on project in project explorer and select Run as -> Run configurations.
Always run your project if you have opened that projects java class tab in workplace. You cannot run your project directly by clicking Green play button if you opened a XML file of that project. However you can run a project if you have opened manifesto of that project.

How to force install of Android App in Development Environment?

I'm tired of uninstalling and than reinstalling in development.
Is there a way to do adb -force install some.apk ? I mean this would save a lot of time. Each time going to system ->apps->uninstall every time a signature is different.
Obviously debug is a dev setting so I don't see why one constantly needs to uninstall the app manually, what a waste of time? Anything that can make development easier is going to result in better code, and better apps so whats up with this?
I have hot deployed to server side Java EE servers in less time. This is less an issue on command line, since one could do an uninstall or replace the adb script with an uninstall followed by an install.
However in eclipse its still a pain. It has been pointed out that this is largely about signature changes. Yes, it is. But why should signatures change in development, and debug mode, and cause the reinstallation issue?
adb has [-r] parameter to reinstall.
adb install -r some.apk
A web search you could have done reveals that the answer to your adb question is:
adb uninstall some.package.name
Note that it's not the file.apk name you use with adb install, but rather the actual package name it ends up installed as.
The normal build process uses the android debug keystore that can be found in your android folder in your home directory (path depends on your OS).
With this debug keystore your app will be signed. This debug signature can be installed on every android device in the world as long as there isn't your app with another signature already installed.
When you say you only do code checkouts, rebuild and your signature is different, than probably your build process is broken. Your debug keystore is valid for at least a year and as long as you are on the same PC while building the generated APK should never have a different signature.
For further checking you should bring some light in your development process. What do you use? Eclipse?
If you work on different developing machines, pick one keystore from one machine and put it into your version control and use this to sign your apk with the debug signature.
adb install -r -d path/to/file.apk
-r to replace
-d to force downgrade
You can install an app forcefully by passing -r parameter.
-r parameter lets adb to reinstall the app.
adb install -r app-release.apk
The debug keystore can be set in Eclipse in Preferences/Android/Build/Custom debug keystore, which is very helpful when working in a team. Every member should set up the same keystore there, and then it is no problem any longer to share devices.
for uninstall
adb uninstall app_package_name
for install
adb install app_package_name
into the command prompt in windows
and terminal in linex/Macos

How to test what will happen when you publish an update to your app to the Market

I'm about to publish a new version of an app to the Market. In order to avoid any potential problems once its been pushed to the Market and people get notified of an update, I'd like to simulate that process on my phone using the .apk for the new version of the app I'll be publishing.
For instance, it has an update to the SQLite DB it's using.
The closest I can find is using the Android Debug Bridge (adb) using the command:
adb install C:\myApplication.apk
with my phone attached to my PC via the usb cable.
(the parameter represents wherever your apk file is on your PC).
When I do this, if the app is already installed on my phone, I get an error message:
Failure INSTALL FAILED ALREADY EXISTS.
If I delete the existing app from my phone, the adb install command works fine.
So, it looks like this can only be used to install an app that doesn't currently exist on your phone.
Is there any way to simulate the update process?
It'd be nice if there was an adb update command, but I don't see that.
Try using option -r to adb install:
adb install [-l] [-r] [-s] -
push this package file to the device
and install it
('-l' means forward-lock the app)
('-r' means reinstall the app, keeping its
data)
('-s' means install on SD card instead of
internal storage)
The closest I think you can get is to put the APK somewhere, and download it on a phone. That is about the closest you can get to the real market situations, with the only difference being that you need to add the "unknown sources" option.
(on a sidenote: you can get an error installing an apk with the same package-name, but a different signing.)
I think your error is because you have the market signed version installed and your trying to install a debug signed version. If you sign it with your market key it should install fine.

Categories

Resources