Android + Gradle: Best place to set dynamic archivesBaseName - android

In our Android project we want the filename of our APK to contain information such as the date, git branch name and short commit hash.
We've been setting this as part of our defaultConfig
android {
defaultConfig {
setProperty("archivesBaseName", "$projectName.$branchName.$date.$buildNumber.$versionCode-$versionName-$versionSha")
}
}
This works most of the time but our build sometimes fails with errors pointing to the value being stale (for example after switching branches). Forcing Gradle Sync in Android Studio usually fixes it.
So is there a better, more robust way of making sure that property stays up to date and forces a gradle sync, if necessary?

try to call clean.execute() before
android { ... }
it is the cheapest 'sync' task I can think of :)
clean.execute()
android {
defaultConfig {
setProperty("archivesBaseName", "$projectName.$branchName.$date.$buildNumber.$versionCode-$versionName-$versionSha")
}
}

I was having same issue, they were not always uptodate with the build.
I solved it by using:
project.ext.set("archivesBaseName", "myAppName");
under the defaultConfig block, and I stopped having the problem.
Not sure it will work for the branch, build number or date, but it's worth giving it a shot.
Here's my configs, including the defaultConfig Block. For my case I was only using a hard coded versionName and build Number (I call it versionCode because I set it in the manifest dynamically). But maybe you can tweak it to your needs:
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode System.getenv("BUILD_NUMBER") as Integer ?: 0
versionName "6.2.5." + versionCode
}
project.ext.set("archivesBaseName", "myAppname" + defaultConfig.versionName);

If an error occurs during the build process, the Messages window appears to describe the issue. Gradle may recommend some command-line options to help you resolve the issue, such as --stacktrace or --debug. To use command-line options with your build process:
Open the Settings or Preferences dialog: On Windows or Linux, select
File > Settings from the main menu.
Navigate to Build, Execution,Deployment > Compiler.
In the text field next to Command-line Options, enter your command-line options.
Click OK to save and exit.
Gradle will apply these command-line options the next time you try building your app.
I have taken the information from this page.

Related

Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk] in android

I am using Android Studio 3.0.1.
When i am trying to run app
INSTALL_FAILED_USER_RESTRICTED: Invalid apk
error occurs.
I also disable Instant Run.
again i am run app but same error occurs.
04/04 10:59:08: Launching app
$ adb push G:\Android\Fundraiser\BuyForFund\app\build\outputs\apk\debug\app-debug.apk /data/local/tmp/com.android.buyforfund
$ adb shell pm install -t -r "/data/local/tmp/com.android.buyforfund"
Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk]
$ adb shell pm uninstall com.android.buyforfund
DELETE_FAILED_INTERNAL_ERROR
Error while Installing APK
None of the other answers worked for me using Xiaomis MIUI 10 on a Mi 9 phone.
Besides the usual (like enabling USB debugging and Install via USB in the developer options) answers from other questions suggested turning off MIUI optimization. While this did the trick, I wasn't happy with it. So I did some digging and came to the following conclusions:
The described error only occurred the second time you deploy your app and after that keeps occurring every other time when deploying the same app to that phone.
To solve this I could either hit Run / press Shift + F10 again or unplug and plug in that phone again. None of this seems viable. So I did some more digging and it turns out when you are increasing the versionCode in your build.gradle file every time you build your app, MIUI 10 will not complain and let you install your app just like you would expect. Even Android Studios Instant Run works. Though doing this manually is just as annoying.
So I took some ideas to auto-increment the versionCode from this question and modified build.gradle (the one for your module, NOT the one for your project). You can do the same following these easy steps:
Replace
defaultConfig {
applicationId "your.app.id" // leave it at the value you have in your file
minSdkVersion 23 // this as well
targetSdkVersion 28 // and this
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
with
def versionPropsFile = file('version.properties')
def value = 0
Properties versionProps = new Properties()
if (!versionPropsFile.exists()) {
versionProps['VERSION_MAJOR'] = "1"
versionProps['VERSION_MINOR'] = "0"
versionProps['VERSION_PATCH'] = "0"
versionProps['VERSION_BUILD'] = "0"
versionProps.store(versionPropsFile.newWriter(), null)
}
def runTasks = gradle.startParameter.taskNames
if ('assembleRelease' in runTasks) {
value = 1
}
if (versionPropsFile.canRead()) {
versionProps.load(new FileInputStream(versionPropsFile))
versionProps['VERSION_PATCH'] = (versionProps['VERSION_PATCH'].toInteger() + value).toString()
versionProps['VERSION_BUILD'] = (versionProps['VERSION_BUILD'].toInteger() + 1).toString()
versionProps.store(versionPropsFile.newWriter(), null)
// change major and minor version here
def mVersionName = "${versionProps['VERSION_MAJOR']}.${versionProps['VERSION_MINOR']}.${versionProps['VERSION_PATCH']}"
defaultConfig {
applicationId "your.app.id" // leave it at the value you have in your file
minSdkVersion 23 // this as well
targetSdkVersion 28 // and this
versionCode versionProps['VERSION_BUILD'].toInteger()
versionName "${mVersionName} Build: ${versionProps['VERSION_BUILD']}"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
else {
throw new GradleException("Could not read version.properties!")
}
Now each time you build your app by hitting Run or Instant Run your versionCode / VERSION_BUILD increases.
If you build a release your VERSION_PATCH increases as well changing your versionName from x.y.z to x.y.z+1 (i.e. 1.2.3 turns to 1.2.4). To change VERSION_MAJOR (the x) and VERSION_MINOR (the y) edit the version.properties file which you can find in your module folder. If you didn't change your modules name it's called app so this file is located at app/version.properties.
Make sure you have enabled the following options:
Settings > Additional Settings > Developer options
USB Debugging
Install via USB
USB Debugging (Security settings)
I have the same problem, I sent a feedback to Google on my phone, would say it's a bug. In my case the best solution is to cancel that dialog and then re-run, it works always on second attempt after cancelling.
Depending on what phone you are using, I would assume the problem is on the phone (Google) side. Not sure yet if a general Google problem or specific hardware phones, I use "Xiaomi Redmi 5".
Disabling instant run actually worked in my case, but that's not the purpose of it, that's just a dirty workaround.
Edit:
Make sure that you don't have something like
android:debuggable="false"
in your manifest.
I ran into this same error while the underlying issue was different.
Mine was that I was trying to install my app on Android 12 device while the AndroidManifest.xml file didn't have all the android:exported properties explicitly set. This error is explained further here: https://developer.android.com/about/versions/12/behavior-changes-12#exported
If your app targets Android 12 or higher and contains activities,
services, or broadcast receivers that use intent filters, you must
explicitly declare the android:exported attribute for these app
components.
Warning: If an activity, service, or broadcast receiver uses intent
filters and doesn't have an explicitly-declared value for
android:exported, your app can't be installed on a device that runs
Android 12 or higher.
After I added the required android:exported properties into AndroidManifest.xml file, the error was resolved.
In your Androidmanifest.xml file at path
app/src/main/Androidmanifest.xml
add android:exported="true"` in activity tag.
Sample:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
<application
android:label="example"
android:icon="#mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:exported="true">
For those who still might get this error if you have done everything from enabling to flutter clean and all.
I solved this error by checking the manifest and adding proper value because i changed something in the manifest which was not supported and later forgot to change it.
so if you have changed something in theme, drawable or any resource file check that out first.
INSTALL_FAILED_USER_RESTRICTED: Invalid apk
Steps for MIUI 9 and Above:
Settings -> Additional Settings -> Developer options ->
step 1: scroll down side - Turn off "MIUI optimization" and Restart your device.
step 2: Turn On "USB Debugging"
step 3: Turn On "Install via USB"
step 4: show push notification and just click USB - Set USB Configuration to Charging (MTP(Media Transfer Protocol) is the default mode.
Works even in MTP in some cases).
please try.
If you are targeting android 'P' then your build.gradle file should look like this
android {
compileSdkVersion 'android-P'
defaultConfig {
applicationId "xyz.com"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
targetSdkVersion must be 27 to run your app below android 'P', otherwise the app can only run on 'P' and above.

Change versionCode of an App created in a gradle script

in an Android app I did not develop, but I have to edit, I found this gradle script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.ajoberstar:grgit:1.5.0'
}
}
import org.ajoberstar.grgit.Grgit
ext {
git = Grgit.open(currentDir: projectDir)
gitVersionName = git.describe()
gitVersionCode = git.tag.list().size()
gitVersionCodeTime = git.head().time
}
task printVersion() {
println("Version Name: $gitVersionName")
println("Version Code: $gitVersionCode")
println("Version Code Time: $gitVersionCodeTime")
}
The "gitVersionCode" variable is used as "versionCode" of the app in the build.gradle file.
This is the build.gradle file:
// gradle script
apply from: "$project.rootDir/tools/script-git-version.gradle"
//...
defaultConfig {
applicationId "..."
minSdkVersion 17
targetSdkVersion 25
versionCode gitVersionCode
versionName gitVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
When I generate signed APK and try to add to beta version, I get the google error that "Version code already exists", but I do not know how to edit the version code of the app.
My questions are:
what this code actually does?
how to edit the version code of the app?
Git is a version control system. Your project appears to be stored in a Git repository.
Grgit is a Groovy library that works with Git. Your Gradle script is loading this library.
git is an instance of Grgit. git.tag.list() returns a list of the Git tags used in this repository. git.tag.list().size() returns the number of tags in that list.
So, this Gradle script is setting the versionCode to be the number of Git tags used in the project's Git repository.
how to edit the version code of the app?
Either:
Create another Git tag, such as by tagging the version that you are trying to release, or
Change versionCode gitVersionCode to stop using gitVersionCode and use some other numbering system
It appears to be using this library to query git, in order to generate the version name and version code. In terms of the version code, it looks like it's setting it depending on the number of git tags it finds. So, to bump the version code, do a git tag on your release commit.
If that doesn't suit, you could just do it manually and replace
versionCode gitVersionCode
with
versionCode 99
replacing "99" with whatever your version code should be.

Android Gradle 'versionCode' not read from environment variable

I have an Android that I deploy to a Google Play alpha track trough a CI server. For the Android versionCode I make use of the CI build number that I inject into the Gradle script through an environment variable.
This used to work fine; but currently Google Play is not accepting any builds. When I manually trigger a alpha upload build (using the gradle-play-publisher plugin) for Gradle I eventually end up with the following error:
APK has an invalid version code.
So when looking (using aapt dump badging apk-path) at the generated APK I see an empty value for the version code (versionCode='').
The relevant code from the build script:
def appVersionCode = System.getenv("BUILD_NUMBER") as Integer ?: 0
defaultConfig {
...
versionCode appVersionCode
...
}
It seems the variable is not read correctly; however it reads System.getenv("KEY_PASS") correctly to use for signing.
The variable is also set:
❯ echo $BUILD_NUMBER
1234
Does anyone have an idea why this specific variable doesn't seem to be read (anymore)?
Gradle version: 3.5 with Android Gradle plugin 2.3.1.
Try to change your code to this:
def appVersionCode = Integer.valueOf(System.env.BUILD_NUMBER ?: 0)
defaultConfig {
...
versionCode appVersionCode
...
}

How to sync Gradle/Android versionCode between build server and workstation

Currently we have Bamboo listening to a Git repository on any changes. When a change occur the build process starts and increases the (Bamboo) build number by one.
I thought it would be nice to use this same build number for the Android project (versionCode) so that the user of the app can always refer to the actual build he/she received. This way the build number goes from 1 to 2 on the build server. Only the workstation doesn't know about this and still uses version 1.
Is there any way to sync this build number?
Tried:
One possible solution a friend of mine suggested was to use a git command to get the commit number: git rev-list HEAD --count which is awesome. The only downside of this is that you cannot properly change build number within Bamboo. So the build number of Bamboo should be leading.
Btw, I'm using Android Studio with Gradle
Bamboo can be configured to set the versionCode value in the manifest to the build number when building, so propagating this change back to where the development is happening should be unnecessary. To configure Bamboo to do this, add a Script task to the build plan (before the actual build task) with a body of:
sed -i 's/android:versionCode="[[:digit:]]*"/android:versionCode="${bamboo.buildNumber}"/' AndroidManifest.xml
Yes, it is quite easy with gradle. You have to retrieve the latest build result in the gradle script and put it in the versionCode.
Here it is what you need (remember to change the variables to refer your server):
def getBambooNumber(){
def url = "https://bambooServer:bambooPort/rest/api/latest/result/PROJECT-BAMBOO-ID/latest.json?os_authType=basic".toURL()
def authValue = "USER:PASSWORD".bytes.encodeBase64().toString()
def json = new JsonSlurper().parseText(url.getText(requestProperties : ["Authorization" : "Basic " + authValue]))
return json.buildNumber + 1 // +1 to Get the new build number
}
def bambooBuild = bambooBuildNumber()
android {
...
defaultConfig {
...
versionCode bambooBuild
//This allows you to access in BuildConfig to the bamboo build
buildConfigField "Integer", "BAMBOO_BUILD", "${bambooBuild}"
...
}
...
}
Let me know if this works for you.

Unable to open debugger port : java.net.SocketException “Socket closed”

I'm using Android Studio, with multiple flavors using Gradle, each with a Debug and Release type, organized as described here, on the bottom half.When I try to start the debugger, I get this error:
Error running androidRecover [installAppDebug]: Unable to open debugger port : java.net.SocketException "Socket closed
I'm also unable to attach the debugger to my device once it's running (it only displays the name of my phone, not the app).
All 3 flavors install on my phone just fine. I just can't get it to let me debug them. I also tested attaching the debugger on a Nexus tablet, and I got the same result.
It's not Gradle specifically as a whole because I can run other Gradle-based apps and attach the debugger just fine so I wonder if it's something with how I've setup my Gradle project and settings.
Here's my build.gradle:
apply plugin: 'android'
apply from: 'signing.gradle'
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile
('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
flav1 {
packageName "com.ex.flav1"
versionCode 32
versionName "1.0.5"
signingConfig signingConfigs.flav1
}
flav2 {
packageName "com.ex.flav2"
versionCode 33
versionName "1.0.6"
signingConfig signingConfigs.flav2
}
flav3 {
packageName "com.ex.flav3"
versionCode 27
versionName "1.0.0"
signingConfig signingConfigs.flav3
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/httpmime-4.2.5.jar')
}
I really have no idea what else to try. Android Studio is completely up-to-date. I've restarted Android Studio, my phone, and my computer.
Also, last week I was having this problem, but it was a specific socket that was blocked, from trying to run the emulator and my phone at the same time. I also noticed I had multiple Gradle processes running, because it wasn't killing them on its own, which I often had to kill Android Studio to kill them. Once that was fixed, it was working off and on.
Let me know if you need any other info.
Thanks,
Devin
Edit I finally know why #hasanaydogar's answer works and why it probably would have solved my problem if we had known it then. See my 2nd comment on it to know why, but in short, you have to select in that dropdown the name that matches your app's root directory.
Just Click the button (left side the RUN button).
Select Android. Then Run.
It will connect to your device.
And dont forget to change build variant
I finally understand why I was getting that error so I'm going to explain how I Debug now. Note that I use Gradle (build multiple apk's using the same code), which might influence some how you use the third part of this answer.
For these to work, in the dropdown next to the debug (icon in #1) and run buttons in the top toolbar, you have to have the one selected with the following icon next to it because that's the name of the root directory of your app where all your code lives:
To start debugging from the beginning, I run the app in Debug mode from the start, using this button in your toolbar:
To attach the debugger to the app when it's already running as #scottyab mentioned, I click the Attach Debugger button in your toolbar:
To run the release version of my app in debug mode, I've started changing my strings in the Debug version of strings.xml in the file path myApp/src/appNameDebug(verses appNameRelease)/res/values/strings.xml, more easily seen here, on the bottom half. When I say change, I really mean that I have two versions of all the strings (3 in my case) necessary to change from using the debug server to using the release server. It might not be completely kosher, but it takes about 5 seconds to go the file, and hold down Cmd+/ and uncomment and comment all of the appropriate lines.
My Release version is just there for when I'm ready to build an apk for release.
Doing things in this way has eliminated that error popping up anymore. I think the Release version is just not made for debugging, and I haven't found an easy way to turn the debug flags on when in Release mode.
I managed to get this working by attaching the debugger after a build see Unable to open debugger port : java.net.SocketException "Socket closed"
I managed to get rid of this problem by killing & restart the adb process,hope this would help :]
I have solved this question with reference to the following SO Answer
The "Select Run/Debug Configuration" button in android studio 2.3.2
Change the Debug type to Native

Categories

Resources