Sign my android app when building with Android Studio - android

I just converted my Eclipse project to an Android Studio project.
One of the very important step in the building/running process is to sign the apk.
So I used the "generate signed apk" from the build menu and it worked fine.
Now, I just want that when I run the app on the USB device, the signing is done automatically. The Android Studio page state that it's possible using the "signing" tabs in the module settings but this tabs doesn't exist!! See the picture below:
I have the latest version of Android Studio, so I guess this feature moved somewhere else but I can't find it!
Thanks!

The menu is under File > Project Structure. Then select your module from the list on the left, switch to the signing tab, and add your signing information.
This will make two changes to your build.gradle:
First, it will add a signingConfigs {} section that looks something like this-
signingConfigs {
release {
storeFile file("your.keystore")
storePassword 'mySecreKystorePassword'
keyAlias 'myKeyAlias'
keyPassword 'mySecretKeyPassword'
}
}
Second, it will configure your buildTypes to use this signing config like so-
buildTypes {
release {
signingConfig signingConfigs.release
}

Usually there're good reasons to have separate debug and production builds (e.g. separate API endpoint, separate API KEYS on tracking, faster building times), so I'll leave a question here for you: Why do you want to debug using a signed (a.k.a. production) build?
on the other hand, to answer your question, there's an easy way to switch which build variant that will be used when you hit the play button on AndroidStudio.
On the several panels around the border there's one named BuildVariants with a little Android icon. Over there's a drop-down menu where you can change to use debug or release build variant.

Related

Why does Android Studio want the debug version of a library in a release build?

Why does a release build complain about a missing debug library?
In order to verify that a release version of an app uses only the release components, I deleted the debug variants of some externally-built libraries, and then tried building the release version of the app. The build variant 'release" is selected, yet the build stops immediately with this error: 'Expecting a file or a directory: theLibrary-debug.aar'.
When theLibrary-debug.aar is present, the release version of the app builds fine, and shows up in its correct place. The app behaves correctly, with release variants in behavior also, so it appears that the build completes correctly, and that the debug library isn't used.
So why does the release build complain about the missing debug library? Is this a normal build behavior in Android Studio (version 4.1.1) or does this require further investigation?
With the names changed, the relevant parts of the app's build.gradle look like this:
...
android {
...
buildTypes {
release {
...
}
debug {
...
}
}
...
}
dependencies {
debugImplementation files ('theLibrary-debug.aar')
releaseImplementation files ('theLibrary-release.aar')
}
P.S. The Module Settings view in Android Studio is awful, and it only makes sense to work in the gradle files, as far as I can see. Any one of the many Eclipse IDEs out there does this sort of settings work much better. Maybe some of the more recent Android Studio versions have some improvements...

How to create the SHA 1 finger print to generate the client id and api key? [duplicate]

Some time ago I created an example project (lets call it "example project") with Oauth2 client id for android application in Google APIs console. I also added SHA1 fingerprint and package name (for example com.package.name).
My mistake was that an application with same package name already existed. Now I need to create an Oauth2 client id for android application in the "valid project" with package name com.package.name and with SHA1 fingerprint which I added before. Obviously when I tried to add this fingerprint I got an error.
The signing fingerprint you specified is already used by another Android OAuth2 client.
After that I remembered about "example project" and deleted client id from this project. The problem is that I still not able to add this fingerprint for package name com.package.name. Client id is deleted but still I have the same error as above.
So do I have a possibility to use this fingerprint for the same package name in another project?
This error occurs when there is an existing SIGNING CERTIFICATE FINGERPRINT (SHA1) on Google Developers linked to a different account.
Solution is to delete the project that is using that SHA1 in console.developers.google.com for the old/incorrect project.
You cannot change the SHA1 fingerprint once it is set.
Remember that deleting the project takes 7 days to completely remove it.
Another option is to delete the debug.keystore and generate a new one with:
keytool -genkey -v -keystore debug.keystore -alias androiddebugkey
-storepass android -keypass android -keyalg RSA -validity 14000
Remember you have to uninstall the app otherwise you get the [INSTALL_FAILED_UPDATE_INCOMPATIBLE] error.
The signing fingerprint you specified is already used by another
Android OAuth2 client
I have to check every project and tried to find another Client ID configured with the SHA1 fingerprint and package name that i tried to configure, unsuccesfully.
At the end the solution was delete the client ID that i tried to edit and add again with the SHA1 fingerprint and package name, it worked for me:
https://console.developers.google.com/apis/credentials
It's late but worth. I have done the same mistake. I added a project to firebase in the wrong account then I deleted and try to add in another firebase account but I couldn't do it because the error indicates that the project is already registered. So it takes time to round about 5 to 7 days to completely delete.
Here is the step I followed to generate another sha1.
Go to .android in my pc located. C:\Users\shahz.android.
delete debug.keystore.
rebuild android project.
click on signing report (right side bar click gradle,
app/task/android/siginingReport)
you will get a new debug key with new sha1.
register your app.
If you are using each firebase project for each env as I am, the below approach might be helpful.
In your build gradle, create new application id for each env:
productFlavors {
dev {
applicationId "se.abc.dev"
}
stag {
applicationId "se.abc.stag"
}
prod
}
On each firebase project, add a new project with corresponding application id with the same finger print. Remember to download the new google service json file since the application id was changed. The package name remains the same so it would not be a problem when uploading into google play. But for sure, I leave the prod flavor empty, so the package name and application id will be the same for prod release to avoid trouble.
As someone could wanna use the same application through two or more firebase projects for many reasons and so get this error I here do address this particular scenario. The esiest way to run the same application upon two or more different firebase projects (let’s say production and staging) is to add to your Module level build.gradle file a build variant (let's say staging) like this one:
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.firebase-perf'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.mydomain.myapp"
minSdkVersion 19
targetSdkVersion 27
versionCode 18
versionName "2.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
staging {
initWith debug
applicationIdSuffix ".staging"
versionNameSuffix = "-staging"
}
}
}
dependencies {
[...]
}
// Firebase
apply plugin: 'com.google.gms.google-services'
In the build variant staging what's most important is the line:
applicationIdSuffix ".staging"
This will inject at build time a ".staging" suffix to your application ID so that you will automatically have
applicationId "com.mydomain.myapp.staging"
You than need to add this application ("com.mydomain.myapp.staging") to your firebase staging project and so you will be able to add the same "com.mydomain.myapp" SHA1 to this application, because it has a different application ID.
I have faced same problem, its solution is very simple
Go to Console.developer and you can see some auto generated key created with this package. delete that package and add again sha key. hope you will got success. follow this link
https://console.developers.google.com/apis/credentials?
Ran into this issue today. As the project takes a long time to get completely deleted, the easiest way is to create a new SHA1 by simply deleting the local debug.keystore. It get's auto generated when you run any app after deleting it. This is documented in the Android docs here.
I'll note that this if your app is listed in the play store, you probably don't want to change the package name and you probably don't want to replace the production key (as you'll be forced to make a new listing).
If you're absolutely sure that there are no currently active projects making use of the current package-name/fingerprint pair, one final thing to try: Make a completely new credential. You'll have to replace your credential file in the application.
I didn't expect this to help, but it did. Maybe Google is somehow caching the lookup? "This packagename/fingerprint/clientID lookup is a duplicate, so don't bother looking it up again."
Sometimes it occurs becuase you have linked the app with Google Cloud Platform project for an API like Google Signin. So firebase shows OAUTH2 issues. At one time either you can use Auth service by firebase or gcp.
Possible Solution: Try to remove the Google Cloud Platform or create a new Android Project in Android Studio.

Android Gradle Running Tests on Non-Debug Builds

I have a project with three different build types: debug, beta, and release. My test package is always created for debug builds, but QA uses the beta build and we want QA to run these tests on their vast array of devices.
I'm trying to create a testing apk for QA that is signed by the same key as the beta build. Looking through the Android-Gradle documentation, I don't see anything telling me that I can't do this, but I don't see anyway to configure this. Is there anyway I can configure which keystore is used when assembling a test apk? Or is there a way to create an unsigned test apk?
You can now point this to a different target, I don't know when this happened, but from the docs:
Currently only one Build Type is tested. By default it is the debug
Build Type, but this can be reconfigured with:
android {
...
testBuildType "staging"
}
This is an incomplete answer to your question in that it documents what you can't do, but the connectedAndroidTest task, which is what runs the androidTest tests in your project, is hardcoded to run against the debug build type, and I don't see a way to point it at a different build type.
Taking the advice from Is there a way to list task dependencies in Gradle? and examining the task dependency tree, if you run:
./gradlew tasks --all
you get this in your output:
Verification tasks
------------------
app:check - Runs all checks. [app:lint]
app:connectedAndroidTest - Installs and runs the tests for Build 'debug' on connected devices. [app:assembleDebug, app:assembleDebugTest]
app:connectedCheck - Runs all device checks on currently connected devices. [app:connectedAndroidTest]
app:deviceCheck - Runs all device checks using Device Providers and Test Servers.
The documentation for the connectedAndroidTest task claims it runs tests against debug, and the task dependencies (which you see with the -all flag) confirm that the task depends on assembleDebug.
Adding additional build types and flavors doesn't seem to affect the dependency on the built-in debug type.
It's possible that with greater Gradle-fu than mine, you could rewire the tasks to make the tests depend on a different build type, but doing this is likely to be fragile since it's bound to depend on things that aren't supported API in the Android Gradle plugin.
To answer your question most directly, though, if all you want is to run tests against a build with a different certificate, you could change the signing config on your debug build to use the beta certificate:
android {
signingConfigs {
beta {
keyAlias 'key'
keyPassword 'password'
storeFile file('/path/to/beta_keystore.jks')
storePassword 'password'
}
}
buildTypes {
debug {
signingConfig signingConfigs.beta
}
beta {
signingConfig signingConfigs.beta
}
}
}
I tested it and I am able to run androidTest targets against debug builds that use a custom keystore in this way. However, I doubt this solves your problem, because I suspect you want to run your tests against the beta build, not a debug build with the beta certificate.
To add a testing source set for your build variant, follow these steps:
In the Project window on the left, click the drop-down menu and
select the Project view.
Within the appropriate module folder,
right-click the src folder and click New > Directory.
For the directory name, enter "androidTestVariantName." For example,
if you have a build variant called "MyFlavor" then the directory name
shoulbe "androidTestMyFlavor." Then click OK.
Right-click on the new directory and click New > Directory. Enter
"java" as the directory name, and then click OK.
Now you can add tests to this new source set by following the steps above to add a new test. When you reach the Choose Destination Directory dialog, select the new variant test source set.
The instrumented tests in src/androidTest/ source set are shared by all build variants. When building a test APK for the "MyFlavor" variant of your app, Gradle combines both the src/androidTest/ and src/androidTestMyFlavor/ source sets.
Another way is to put following line your in default config.
Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:
android {
...
testBuildType "staging"
}

Installation failed with Android Studio, APK not signed

Recently change to Android Studio from Eclipse and I have also changed the JDK from java-open-jdk to jdk1.7.0_45.
Now I'm trying to run my first app and I get this message:
Installation failed since the APK was either not signed, or signed incorrectly.
If this is a Gradle-based project, then make sure the signing configuration
is specified in the Gradle build script
Edit:
When I'm running from Android Studio I get the error displayed above. When I'm running it from the command line I don't get an error (well the app is running and I get an error but nothing to do with gradle).
I got the code from here
You can check build.gradle here at google repo
UPDATE 2:
I added this code
signingConfigs {
release {
storeFile file("john.keystore")
storePassword "john"
keyAlias "johnkeystore"
keyPassword "john"
}
}
just above the buildTypes code block in the build.gradle file.
File john.keystore is on the root of my Project. I'm running gradlew assembleRelease and I'm getting a xxx-release-unsigned.apk.
If you indeed runing build with gradle you need to configure signingConfigs. Android can configure your debug signingConfig automatically. You can make release signingConfig in a next manner:
android {
signingConfigs {
release {
storeFile file('android.keystore')
storePassword "pwd"
keyAlias "alias"
keyPassword "pwd"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}
Check manual on this topic here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Signing-Configurations
If you still have issues, please update question with your build.gradle. Most likely issue is laying here. I'm using JDK 1.7 and gradle builds are working fine.
I had the same problem, i went to Build->Clean Project and re-execute the project and it worked.
I have been struggling for a while with Android Studio esp. gradle and build variants.
I have change this from the build types as defined:
debug
release
As I ran into the same problem (while developing and running tests), I went to Build -> Rebuild Project and then re-launched the emulator. That worked for me.
None of the answers worked for me, even after uninstalling app from phone, I could not deploy it. What worked was installing the app o a different device, and then when I tried to deploy it on the previous device, it miracleously worked.
If your goal is not to create a custom build for your application you might want to clean all the data about it from your test device, in case you are using emulator just wipe all the data from device. Go to -> Tools -> Android -> AVD Manager -> [Device you wanna wipe] (In actions tab) -> Wipe Data.
In case of actual device just uninstall the app and all related data.
Reason is that unsigned APK has a signature, so device sees you're trying to install something with the same package name that was generated not here.
Hope this saves you some time.
In case your device has multiple users, before installing signed APK, check if the same app is not installed for other users. If it is there remove it.
For me this was the root cause of rejecting installation of signed APK.
Just to Build -> Rebuild Project and then re-launched the emulator. That worked for me.

Android Studio "Project Structure" window not working?

Loads of tutorials for Android Studio need me to use the "Project Structure" window (File > Project Structure), but whenever I try to open it, I get an error "We will provide a UI to configure project setting later. Until then, please manually edit your build.gradle file(s.)".
Does anyone know if it's like this for everyone (in which case, what do those tutorials mean by File > Project Structure?), or just me? I've had the same error on Windows and Linux.
One of the tutorials: How do I add a library project to Android Studio?
Before Android Studio, IntelliJ Android plugin users used to use the Project Structure dialog to add/remove modules from their projects.
Android Studio aims to have a single model for building your application from both the command line and from the IDE. So if you have to modify the project structure, the correct way to do it is to modify your build.gradle (and/or settings.gradle) build scripts and reimport the project.
Eventually (within a few months), Android Studio will hook up the project structure dialog to automatically edit the gradle build scripts for you, or will provide a different UI, just like the error message says.
You'll find the gradle plugin user guide at http://tools.android.com/tech-docs/new-build-system/user-guide to be helpful in figuring out how to add libraries to your gradle build scripts.
There are currently some bugs with the UI in android studio project structure settings related to updating the Gradle. Right now whatever I enter into the project structure fields do not save or update the gradle files. What did work for me was writing the settings directly into the build.gradle
The gradle files are basically xml files that are generated (dynamic) instead of static.
I tried forever in the project structure UI to set up application signing for debug, eventually to get it to work I added,
android {
signingConfigs {
debug {
storeFile file("debug.keystore")
}
myConfig {
storeFile file("other.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
buildTypes {
foo {
signingConfig signingConfigs.myConfig
}
}
}
The android developers guide for configuring gradle will help you get up to speed.
http://developer.android.com/tools/building/configuring-gradle.html
refer to the Gradle documentation for the parameters you are looking for
http://tools.android.com/tech-docs/new-build-system/user-guide
There are currently some bugs with the UI in android studio project structure settings related to updating the Gradle. Right now whatever I enter into the project structure fields do not save or update the gradle files. What did work for me was writing the settings directly into the build.gradle
The gradle files are basically xml files that are generated (dynamic) instead of static.
I tried forever in the project structure UI to set up application signing for debug, eventually to get it to work I added,
android {
signingConfigs {
debug {
storeFile file("debug.keystore")
}
myConfig {
storeFile file("other.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
buildTypes {
foo {
signingConfig signingConfigs.myConfig
}
}
}
The android developers guide for configuring gradle will help you get up to speed.
http://developer.android.com/tools/building/configuring-gradle.html
refer to the Gradle documentation for the parameters you are looking for
http://tools.android.com/tech-docs/new-build-system/user-guide

Categories

Resources