How do I set configuration using Gradle for build flavors - android

Existing system (Maven)
I have an Android project which has two modules: app and api.
Currently, I use Maven to build it, and I have a configuration.properties in the app resources directory which has properties in it such as exampleProperty=${property}
I use the command line for my various builds like so mvn clean install -Dproperty=customString, and this swaps out the correct property so that it is exampleProperty=customString.
This property is read from by both app and api modules - and works fine.
New system (Gradle)
The Question: Can I use defaultConfig to set a property in my configuration.properties file?
I'm trying to use the defaultConfig (and eventually productFlavors) to create a setup where the build.gradle swaps out the properties instead of using the command line.
I've tried a number of ways to make changes to the gradle.properties file from the defaultConfig closure in the build.gradle file like so:
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
applicationId "com.example.editconfigproperties"
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
System.setProperty('exampleProperty', 'customString')
project.properties['exampleProperty']='customString'
extensions.add('exampleProperty', 'customString')
ext.exampleProperty = 'customString' // This one seems to work
}
}
I think the last one adds it to the gradle.properties file, but I can't figure out how to make the configuration.properties file read from it (or if that's even possible). I tried setting exampleProperty=#exampleProperty# in the `configuration.properties' file, but to no avail.

Related

my app is not running in android KITKAT [duplicate]

I want to change the minimum SDK version in Android Studio from API 12 to API 14. I have tried changing it in the manifest file, i.e.,
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
and rebuilding the project, but I still get the Android Studio IDE throwing up some errors. I presume I have to set the min SDK in 'project properties' or something similar so the IDE recognizes the change, but I can't find where this is done in Android Studio.
When you want to update your minSdkVersion in an existing Android project...
Update build.gradle (Module: YourProject) under Gradle Script and
make sure that it is NOT build.gradle (Project: YourProject.app).
An example of build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
defaultConfig {
applicationId "com.stackoverflow.answer"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
androidTestCompile 'junit:junit:4.12'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Sync gradle button (refresh all gradle projects also works).
or
Rebuild project
After updating the build.gradle's minSdkVersion, you have to click on the button to sync gradle file ("Sync Project with Gradle files"). That will clear the marker.
Updating manifest.xml, for e.g. deleting any references to SDK levels in the manifest file, is NOT necessary anymore in Android Studio.
Update 2022
For Android Studio users:
Right click the App directory and
Choose the "Open Module Settings" (F4) option
Change the "Min SDK Version" in the Default Config tab
NOTE:
You might also want to change;
the "Target SDK Version" in the Default Config tab and
the "Compile SDK Version" in the Properties tab
Click Apply, then OK, and Gradle should automatically be synced
For users of older Android Studio versions:
Right click the App directory and
Choose the "Module Setting" (F4) option
Change the ADK Platform to what you need
Click OK and Gradle should automatically be synced
As now Android Studio is stable, there is an easy way to do it.
Right click on your project file
Select "Open Module Settings"
Go to the "Flavors" tab.
Select the Min SDK Version from the drop down list
PS: Though this question was already answered but Android Studio has changed a little bit by its stable release. So an easy straight forward way will help any new answer seeker landing here.
In android studio you can easily press:
Ctrl + Shift + Alt + S.
If you have a newer version of android studio, then press on app first.
Then, continue with step three as follows.
A window will open with a bunch of options
Go to Flavors and that's actually all you need
You can also change the versionCode of your app there.
In build.gradle change minSdkVersion 13 to minSdkVersion 8 Thats all you need to do. I solved my problem by only doing this.
defaultConfig {
applicationId "com.example.sabrim.sbrtest"
minSdkVersion 8
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
According to this answer, you just don't include minsdkversion in the manifest.xml, and the build system will use the values from the build.gradle file and put the information into the final apk.
Because the build system needs this information anyway, this makes sense. You should not need to define this values two times.
You just have to sync the project after changing the build.gradle file, but Android Studio 0.5.2 display a yellow status bar on top of the build.gradle editor window to help you
Also note there at least two build.gradle files: one master and one for the app/module. The one to change is in the app/module, it already includes a property minSdkVersion in a newly generated project.
For the latest Android Studio v2.3.3 (October 11th, 2017) :
1. Click View on menu bar
2. Click Open Module Settings
3. Open Flavors tab
4. Choose Min Sdk version you need
6. Click OK
If you're having troubles specifying the SDK target to Google APIs instead of the base Platform SDK just change the compileSdkVersion 19 to compileSdkVersion "Google Inc.:Google APIs:19"
As well as updating the manifest, update the module's build.gradle file too (it's listed in the project pane just below the manifest - if there's no minSdkVersion key in it, you're looking at the wrong one, as there's a couple). A rebuild and things should be fine...
In Android studio open build.gradle and edit the following section:
defaultConfig {
applicationId "com.demo.myanswer"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
here you can change minSdkVersion from 12 to 14
File>Project Structure>Modules
you can change it from there
When you want to change minimum SDK you should take care of minSdkVersion[About] in module build.garadle
android {
defaultConfig {
minSdkVersion 21
}
}
Changing the minSdkVersion in the manifest is not necessary. If you change it in the gradle build file, as seen below, you accomplish what you need to do.
defaultConfig {
applicationId "com.demo.myanswer"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
To change the minimum SDK version in Android Studio just...
1). Right click on "app" in the "Project" panel
2). Choose the new "Min SDK Version" on the "Default Config" tab
3). Click on "OK" and the project will now resync with the new Gradle settings.
For me what worked was: (right click)project->android tools->clear lint markers. Although for some reason the Manifest reverted to the old (lower) minimum API level, but after I changed it back to the new (higher) API level there was no red error underline and the project now uses the new minimum API level.
Edit: Sorry, I see you were using Android Studio, not Eclipse. But I guess there is a similar 'clear lint markers' in Studio somewhere and it might solve the problem.
Gradle Scripts ->
build.gradle (Module: app) ->
minSdkVersion (Your min sdk version)

Android Studio - Change applicationId in build.gradle file

How can I change the applicationId field of my build.gradle file?
defaultConfig {
applicationId "com.oldname.appname"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
I've already renamed the package structure to com.newname.appname and all the imports and things like that have updated, but applicationId hasn't. I can't refactor it and just trying to type in a new name brings up a ton of errors where Android Studio will tell me my main FragmantActivity "is not applicable to android.app.Activity" and so on.
Any idea what to do? Thanks in advance.
You can create different flavours for one application in Gradle file. This will increase the code reusability.
An example for an application with Paid and Free flavors:
productFlavors {
paid {
applicationId "com.oldname.appname.paid"
}
free {
applicationId "com.oldname.appname.free"
}
}
So these two flavours will act as two products. You can upload it to play store as different applications.
You can also get four build variant for this two flavours.
com.oldname.appname.free- debug
com.oldname.appname.free- release
com.oldname.appname.paid- debug
com.oldname.appname.paid- release
Apart from the usual java files and manifest that had to be renamed I had to also change the settings under Project Structure:
SO I went to File > Project Structure > Modules/app > Flavours
I then changed the Application ID and all worked great.
Hope this helps for other MAC users like myself. (Did not test on Windows)

How to read latest property in property file that is updated earlier in gradle execution

For my Android project I have configured the defaultConfig such that it gets the versionName for the AndroidManifest.xml in the generated apk from a version property in ./gradle.properties. This works great. Here is snippet from build.gradle:
android {
....
defaultConfig {
minSdkVersion 16 //4.1.2
targetSdkVersion 19 //4.4 KitKat
versionName = version //Read from gradle.properties
...
}
The problems is that when I use the townsfolk/gradle-release plugin that plugin updates the property in ./gradle.properties (bumps it up to the next release name) earlier in the execution.
In this scenario the versionName that is read is from the original ./gradle.properties file before it was modified by gradle-release plugin.
How can I force the reading of the property file just in time to pick up the latest property settings? TIA for your help

In Android Studios build.gradle, where do I find the 'project' file?

I just imported build.grade file of the facebook module in android studio and it contain below code:
defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}
My question is Which file does 'project' point to ?
In the root of your project you should have a gradle.properties file. In there you should see (or can set yourself) these attributes.
ANDROID_BUILD_MIN_SDK_VERSION=19
ANDROID_BUILD_TARGET_SDK_VERSION=19
If you need to use a version number like 19.0.2 instead of 19, you need to quote the value.
ANDROID_BUILD_MIN_SDK_VERSION="19.0.2"
ANDROID_BUILD_TARGET_SDK_VERSION="19.0.2"

How to change Android version and code version number?

How to change Android version and code version number Android Studio? I want to change apk file (app) on Google Play and I need to change Android version and code version number. I tried with this in AndroidManifest.xml file in Android Studio:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bestsoftcorporation.circle.app"
android:versionCode="101"
android:versionName="2.0">
But it does not work. When I tried to publish it on Google Play it display that I must to change Android version name and code.
Go in the build.gradle and set the version code and name inside the defaultConfig element
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
The easiest way to set the version in Android Studio:
1. Press SHIFT+CTRL+ALT+S (or File -> Project Structure -> app)
Android Studio < 3.4:
Choose tab 'Flavors'
The last two fields are 'Version Code' and 'Version Name'
Android Studio >= 3.4:
Choose 'Modules' in the left panel.
Choose 'app' in middle panel.
Choose 'Default Config' tab in the right panel.
Scroll down to see and edit 'Version Code' and 'Version Name' fields.
You can define your versionName and versionCode in your module's build.gradle file like this :
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
.... //Other Configuration
}
Press Ctrl+Alt+Shift+S in android studio or go to File > Project Structure...
Select app on left side and select Flavors tab on right side on default config change version code, name and etc...
You can manage your application versioning wisely by using the Advanced Build Version Plugin for Gradle.
You just need to include the plugin in yout build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.moallemi.gradle.advanced-build-version:gradle-plugin:1.5.0'
}
}
apply plugin: 'org.moallemi.advanced-build-version'
And then you can use the versioning functions (and, obviously, customize them):
advancedVersioning {
nameOptions { }
codeOptions { }
outputOptions { }
}
def appVersionName = advancedVersioning.versionName
def appVersionCode = advancedVersioning.versionCode
For more information, take a look at the official documentation.
Open your build.gradle file and make sure you have versionCode and versionName inside defaultConfig element. If not, add them. Refer to this link for more details.
The official documentation on this is here.
The short answer is do it in Gradle not the manifest, Gradle overwrites the manifest.
The File | Project Structure | Modules | Default Config answer given above just updates Gradle via a UI rather than by directly editing XML.
The longer answer is:
If your app defines the app version directly in the element, the version values in the Gradle build file will override the settings in the manifest. Additionally, defining these settings in the Gradle build files allows you to specify different values for different versions of your app. For greater flexibility and to avoid potential overwriting when the manifest is merged, you should remove these attributes from the element and define your version settings in the Gradle build files instead.
Go in the build.gradle and set the version code and name inside the defaultConfig element
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
After updating the manifest file, instead of building your project, go to command line and reach the path ...bld\Debug\platforms\android. Run the command "ant release". Your new release.apk file will have a new version code.
I didn't get the other answers to work in Android Studio 1.4. But this worked:
click on your app name to the left below the main ribbon.
It will show a list of files.
Open AndroidManifest.xml and change the version code and version number there.
Android version and code version number change on Android Studio >= 3.6:
Two ways to achieve this:
Direct on the Android project by Open Flutter project and modify the file
local.properties
change the following values. Example:
flutter.buildMode=release
flutter.versionName=3.0.0
flutter.sdk=C\:\\src\\flutter
sdk.dir=C\:\\Users\\vgonza\\AppData\\Local\\Android\\sdk
flutter.versionCode=30
Open pubspec.yaml
Change the
version: 2.0.0+8
Meaning:
The version name is 2.0.0
The version code is 8
See example by Suragch in:
How to set build and version number of Flutter app
You can easily auto increase versionName and versionCode programmatically.
For Android add this to your gradle script and also create a file version.properties with VERSION_CODE=555
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
def versionPropsFile = file('version.properties')
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
def code = versionProps['VERSION_CODE'].toInteger() + 1
versionProps['VERSION_CODE'] = code.toString()
versionProps.store(versionPropsFile.newWriter(), null)
defaultConfig {
applicationId "app.umanusorn.playground"
minSdkVersion 29
targetSdkVersion 30
versionCode code
versionName code.toString()

Categories

Resources