Override Android versionCode from applied Gradle script - android

I want to override the Android versionCode property from an applied script file (as to keep the app script clean). This is what i am doing currently (i omitted anything unrelated):
build.gradle:
apply from: 'ci.gradle'
android {
defaultConfig {
versionCode 1
}
}
ci.gradle:
project.afterEvaluate {
project.android.defaultConfig.versionCode = 3434
}
But i still end up with versionCode being 1... What i am doing wrong?

I learned that it can be solved by moving apply from: 'ci.gradle' after the android block:
build.gradle:
android {
defaultConfig {
versionCode 1
}
}
apply from: 'ci.gradle'
ci.gradle:
android.defaultConfig.versionCode = 3434

Related

Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File

I imported a project downloaded from GitHub into my Android Studio project as module.
The "Import module..." wizard worked fine, but when the Adroid Studio tried to rebuild the project, it returned me this error:
Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File
The error is related to this line in the "build.gradle" file of the imported module:
compileSdkVersion rootProject.compileSdkVersion
I tried to add "ext" section in the project "build.gradle" like this:
ext {
compileSdkVersion 26
}
But in this way I receive a new error:
Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ...
In your top-level file use:
ext {
compileSdkVersion = 26
}
In your module/build.gradle file use:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
...
}
Another way:
Your build.gradle in top-level module
ext {
minSdk = 21
targetSdk = 29
compileSdk = 29
buildTools = '29.0.3'
}
Your build.gradle in app module
android {
def buildConfig = rootProject.extensions.getByName("ext")
compileSdkVersion buildConfig.compileSdk
buildToolsVersion buildConfig.buildTools
defaultConfig {
minSdkVersion buildConfig.minSdk
targetSdkVersion buildConfig.targetSdk
}
// ...
}
In build.gradle you need to write compilesdkversion under android tag as in this example:
android {
..
compileSdkVersion 26 // 26 is an example
..}
By the way. You can build that module as library then import it into your project as .aar file.
Change your .gradle android part to this
android {
compileSdkVersion 26
defaultConfig {
applicationId "your App id"
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

Android, Gradle to allow certain buildtypes with certain productflavors

I have a basic gradle file below for android project. Wtih this file I have 4 build variants: fav1-debug, fav2-debug, fav1-release, fav2-release. How can I assign release to build with only fav2; debug to build with anything except fav2.
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.example.alock"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
productFlavors {
fav1 {
// Something
}
fav2 {
// Something
}
}
buildTypes {
debug {
// Something
}
release {
// Something
}
}
}
You can't forbid this. However you can throw an exception when irrelevant build variant chosen. There is many ways to detect buildType and productFlavor during the gradle build. Check this and this.

Gradle copy file from resources to assets on compilation

I am looking for a way to copy src/main/res/values/* to /src/main/assets/www/xml/
I would like this to happen on compile, so I've been trying to get gradle to do such a thing, but with no success.
I appreciate any help with this :)
I have added my current gradle file:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 21
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "name"
minSdkVersion 1
targetSdkVersion 21
versionCode 123
versionName "123"
}
}
dependencies {
A huge bunch
}
Based on the copy task given from Enrico I made this work:
task myCopy(type: Copy) {
from 'src/main/res/values'
into 'src/main/assets/www/xml'
}
project.afterEvaluate {
prepareDebugDependencies.dependsOn myCopy
}
Create a copy task and make it a dependency of the compile task
task myCopy(type: Copy) {
from 'src/main/res/values'
into 'src/main/assets/www/xml'
}
assemble.dependsOn myCopy
EDIT: I assumed you had a compile task. Use assemble instead

Android Studio 1.0 and error "Library projects cannot set applicationId"

After updating Android Studio to 1.0, I see this error:
Error: Library projects cannot set applicationId. applicationId is set
to 'com.super.app' in default config.
I updated the Gradle plugin as suggested but I did not understand how to fix this.
Based on this info:
ApplicationId in Library Projects
You cannot use applicationId to customize the package of a library project. The package name has to be fixed in library projects (and specified as packageName in the manifest). The Gradle plugin did not enforce this restriction earlier.
Removing applicationId variable from the library's build.gradle file should resolve the issue.
Thanks to Joel for his correct answer: I need to remove only 1 line from te .gradle file:
defaultConfig {
applicationId "com.super.app" <---- remove this line
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
becomes
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
and my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.super.app">
...
This is the right solution if you don't need to rename the package name of your app. To rename it you need to use "flavours":
android {
...
productFlavors {
flavor1 {
applicationId 'com.super.superapp'
}
}
Libraries can't set applicationId and if you are working in a multi-module project and picking up flavors from a separate file , none of the above answers will work. For a modularized app, you need the following steps -
Create a flavors.gradle file in project root directory
ext.flavorConfig = { // 1
flavorDimensions "pricing"
productFlavors {
free {
dimension "pricing"
ext.myApplicationIdSuffix = '.free' // 2
}
paid {
dimension "pricing"
ext.myApplicationIdSuffix = '.paid'
}
}
productFlavors.all { flavor -> // 3
if (flavor.hasProperty('myApplicationIdSuffix') && isApplicationProject()) {
flavor.applicationIdSuffix = flavor.myApplicationIdSuffix
}
}
}
def isApplicationProject() { // 4
return project.android.class.simpleName.startsWith('BaseAppModuleExtension')
}
In 1 we export a closure so that we can use it in our modules’ build.gradle files.
In 2 we define a custom myApplicationIdSuffix property. We cannot simply have applicationIdSuffix as it is not possible to use it in library modules (build would fail if you did).
In 3 we iterate over created flavors and set applicationIdSuffix if we detect that it’s an application module only.
4 is a way to check where this closure is being used.
All that’s left is to use this closure in our modules’ build.gradle files. E.g. in application module this would look like this:
apply plugin: 'com.android.application'
apply from: "${rootProject.projectDir}/flavors.gradle"
android {
// other config...
with flavorConfig
}
If this isn't clear, you can check out this article for better understanding.
Just incase it helps some one :
When i imported an eclipse project into android studio,i got an error ::
"Error:Application and test application id cannot be the same"
Strange though,but i looked into the build.gradle and found the two placeholders,one for the application and other for testapplication.
I removed the testApplicationId from that as is suggested in this post and this helped me resolve the issue.
Note: This explaination is not related to the errors posted in this question,but might help someone who is getting a similar error.
You cannot define applicationId for your lib.
But incase you want to use an identifier in your build file, which will give you, your library package name, you can define a variable for the module and then use the value as required.
eg : Library's build.gradle
apply plugin: 'com.android.library'
def libraryGroupId = 'com.google.example'
def libraryArtifactId = project.getName()
def libraryVersion = '1.1'
Also, you can use the value below as needed in your build file itself in lib.
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "$libraryVersion"
resValue "string", "Library", libraryGroupId"
}
}

Set Android app version using Gradle

I'm trying to use Gradle to set app's name. Please, look at this snippet of build.gradle:
android {
...
defaultConfig {
...
versionCode getVersionCode()
versionName getVersionName()
...
}
...
}
...
int getVersionCode() {
return 1
}
def getVersionName() {
return "1.0"
}
Android Studio says
'versionCode' cannot be applied to 'java.lang.Integer'
'versionName' cannot be applied to 'java.lang.String'
and when I install the app on a device it has no versionCode and versionName at all.
The problem is clear to me but I don't know how to solve it.
Please advice.
It doesn't resolve your issue, but it can be a different solution.
You can use gradle.properties inside your root project and define:
VERSION_NAME=1.2.1
VERSION_CODE=26
Then in your build.gradle you can use:
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
EDITED
In order to define your app version dynamically, specify a custom method with def and call it, as such:
def computeVersionName() {
return "2.0"
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
versionCode 12
versionName computeVersionName()
minSdkVersion 16
targetSdkVersion 16
}
}
See here for more.
Make sure not to use function names that could conflict with existing getters in the given scope. For instance, defaultConfig { ... } calling getVersionName() will automatically use the getter defaultConfig.getVersionName() instead of the custom method.
Here is a build.gradle that I am using based on ideas from JakeWharton :
apply plugin: 'com.android.application'
def versionMajor = 1
def versionMinor = 2
def versionPatch = 0
def gitVersion() {
def counter = 0
def process = "git rev-list master --first-parent --count".execute()
return process.text.toInteger()
}
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId 'my.project.com'
minSdkVersion 14
targetSdkVersion 19
versionCode gitVersion()
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
}
....
}
First of all - you are calling the inner getter., hence you have nulls.
versionCode property is accessed by getVersionCode().
versionName property is accessed by getVersionName()
So you get null and assign null... - remember, you are inside defaultConfig scope, hence Gradle will use the closest gerVersionCode() function.
There is a cleaner way to make it in newer Android Gradle (4.1.2).
Usually, those values are separated to the new file.
Firstly, you should use closures, not functions.
Fuction:
def myFunction() {
}
Closure:
myClosure = {
}
Also, you can extend ext scope (it is used to store all custom project-wide Gradle properties, this is a great place for it!) with your closure!
Remember not to name your functions 'getVersionName' 'getVersionCode'
Create a file config.gradle:
ext {
appSetup = [
majorVersion : 1,
minorVersion : 0,
patchVersion : 0
]
getAppVersionCode = { appSetup.majorVersion * 10000 + appSetup.minorVersion * 100 + appSetup.patchVersion }
getAppVersionName = { "${appSetup.majorVersion}.${appSetup.minorVersion}.${appSetup.patchVersion}" }
In build.gradle:
// applies bloc
apply from: 'config.gradle'
android {
versionCode getAppVersionCode()
versionName getAppVersionName()
If you use a gradle.properties file to store shared build versions, make sur to use the toInteger() function for the version code, compiled/min and target sd versions.
See this post for more information: https://medium.com/#ali.muzaffar/gradle-configure-variables-for-all-android-project-modules-in-one-place-5a6e56cd384e#.yl1x0ziqe

Categories

Resources