Being short - is there a way to keep flavor configs in separate .gradle files?
And for more details - I'd like to have per flavor .gradle files (like flavorGermany.gradle, flavorUkraine.gradle, flavorItaly.gradle etc.) that will be included with 'apply from:' directive into main gradle.
Each flavor .gradle will contain signing and build configs.
Sure you can. Just place those files in app folder and then in your app folders build.gradle import those.
Your flavorGermany.gradle would look like this:
android {
productFlavors {
flavorGermany {}
}
}
And then you import those in your build.gradle (app):
apply plugin: 'com.android.application'
apply from: 'flavorGermany.gradle'
apply from: 'flavorUkraine.gradle'
While configuring flavor signing in a separate .gradle file I got a sticky error telling:
flavorGermany.gradle: 1: unable to resolve class com.android.ide.common.signing.KeystoreHelper
In flavorGermany.gradle the KeystoreHelper is used this way:
android {
signingConfigs {
germany {
storeFile = file(KeystoreHelper.defaultDebugKeystoreLocation());
storePassword = "some_password";
keyAlias = "some_key";
keyPassword = "some_other_key";
}
}
// other configs ...
}
To fix this I had to add this before the 'android' definition:
buildscript {
repositories {
mavenCentral()
}
dependencies {
// Android gradle plugin
classpath 'com.android.tools.build:gradle:2.1.3'
}
}
With this change the import error has gone and the Android Studio was able to recognize the additional flavor described in a separate .gradle file.
Related
I am currently updating my project and as one of the steps I am changing gradle files to use the plugins { id 'xxx' } way instead of the legacy apply plugin 'xxx' approach. I was able to migrate most of the imports to the new format, however I cannot add some plugins, as I am unable to find their gradle plugin ids.
For example, here are my old gradle files:
settings.gradle file
include ':app'
project's build.gradle file
buildscript {
repositories {
google()
mavenCentral()
(...)
}
dependencies {
(...)
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.0'
classpath 'com.google.android.gms:oss-licenses-plugin:0.10.5'
}
}
(...)
module's build.gradle file
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'
(...)
And here are partially modified new gradle files:
settings.gradle file
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "xxxx"
include ':app'
project's build.gradle file
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.google.firebase.crashlytics' version '2.9.2' apply false
// DOESN'T WORK:
id 'com.google.android.gms.oss-licenses-plugin' version '0.10.5' apply false
}
(...)
module's build.gradle file
plugins {
id 'com.android.application'
id 'com.google.firebase.crashlytics'
// NEED TO SET SAME ID AS IN PROJECT'S GRADLE FILE PROBABLY:
id 'com.google.android.gms.oss-licenses-plugin'
(...)
}
Problem lays in how to get gradle plugin id for given plugin?
Many plugin installation instructions use the old apply plugin approach and I don't want to mix both of them.
For example in case of Crashlytics with classpath of com.google.firebase:firebase-crashlytics-gradle, the id is com.google.firebase.crashlytics - how was I supposed to know that? I found this in one of the answers on Stackoverflow, but without information about how someone knew that.
Currently I am trying to add the oss-licenses-plugin and I am completly clueless as about how to find its gradle plugin id...
Any suggestions?
Or maybe it is not guaranteed that every plugin added with use of classpath can be translated to the new plugins { } way? In this case, how can I tell it is this situation?
There's a certain pattern that plugin publishers need to follow in order for Gradle to find the plugin implementation. A properties file needs to be included in the JAR's META-INF/gradle-plugins directory. And the name of this file needs to be formatted in the following way:
<plugin-id>.properties
And inside this file, the plugin implementation is defined:
implementation-class=<com.example.SomePluginImpl>
So, to answer your question, you'll need to get a hold of the JAR & look at its contents to figure out the id: META-INF/gradle-plugins/<plugin-id>.properties.
For com.google.android.gms.oss-licenses-plugin, I checked here: Link. Alternatively, you can grab the JAR from maven, extract its contents & check the properties file: Link.
The other issue with your code is that for external plugin resolution, you need to define a resolutionStrategy under pluginManagement:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'com.google.android.gms.oss-licenses-plugin') {
useModule "com.google.android.gms:oss-licenses-plugin:${requested.version}"
}
}
}
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
Now you should be able to use id 'com.google.android.gms.oss-licenses-plugin' version '0.10.5' apply false in your project-level build.gradle.
according to gradle docs, you can find the plugin registered in here
not all gradle plugin available, because the dev need to publish their plugin to gradle first, here how to publish the plugin https://plugins.gradle.org/docs/publish-plugin
edit :
I tried to find oss-licenses-plugin, no luck with that. So you should use the old method to apply the plugin.
other info : What the difference in applying gradle plugin
I have a gradle project with a nested gradle project. How can I set a configuration on the root build.gradle and apply to all nested modules. The structure looks like:
project
build.gradle
settings.gradle
sub-project
build.gradle
inside settings.gradle, I includes the sub-project:
include ":sub-project"
in the root build.gradle, I have below settings:
android {
buildTypes {
staging {
initWith release
}
}
}
It works fine if I manually edit the build.gradle in sub-project. I wonder how I can pass this setting to the sub-project without updating its build.gradle
I tried to add below configuration on root build.gradle:
subprojects { pry ->
android {
buildTypes {
staging {
initWith release
}
}
}
}
but got this error:
Unable to find a matching configuration of project :sub-project:
- Configuration 'debugApiElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'staging' and found incompatible value 'debug'.
Manually add the settings on the nested build.gradle works great but subprojects way doesn't work. What is the different between these two ways?
inside project/build.gradle, use the allprojects as below:
allprojects { prj ->
if (!project.hasProperty("android")) {
return
}
// this condition can be used to skip some project
if (prj.name == "<your-project-name-to-skip>"){
return
}
android {
...
productFlavors {
staging {
initWith release
}
}
...
}
}
If you want the particular settings applied to sub-projects only. Replace the allprojects with subprojects.
I have an incorrect project structure. I need a top-level build-gradle, and a module on the same level that contains its own build.gradle.
See picture of how it is organized now. What you see is almost two different levels merged into on.e The build.gradle here is the only one in the project. The one you see has the note that it is the top-level version.
What is the correct way to straighten this out? Do I need to reimport or can I reorganize it?
Other info, I have Gradle 2.10 installed.
EDIT: MORE INFO
Usually I have my top-level Gradle file that contains something like this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
But in the setup above, without having that second Gradle file, where do I put the other info ... for example:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven {
url "https://jitpack.io"
}
}
android {
defaultConfig {
// edited
}
dependencies {
// edited
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apply plugin: 'com.google.gms.google-services'
When I run the program, I get this error:
Error:A problem was found with the configuration of task ':checkDebugManifest'.
> File 'C:\--\src\main\AndroidManifest.xml' specified for property 'manifest' does not exist.
Is this related?
This way is still assuming a flat hierarchy without the extra module asked by OP, but since it's based on my own Eclipse to AS migration I know it worked... for me.
To recognize eclipse defaults without moving the files you need this:
android {
defaultConfig {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
test.java.srcDirs = ['src/test/java', 'build/generated/source/debug']
}
This will most likely allow you to use both eclipse and Android Studio with the same folders in place.
The second way is about not changing gradle but moving folders so gradle finds things where it expects to.
move AndroidManifest.xml, it must go into src/main
move res into src/main/res
move src/com into src/main/java/com (can you confirm where is your com folder currently?
You can either move files or direct gradle to where they are, it's your choice - but don't do both. The only step I don't remember is the build/generated/source/debug for test, I can't remember if I used that because I use groovy or if it was another eclipse maven/AS gradle mismatch.
It's because Gradle looks for AndroidManifest in a default place --> App/src/main/AndroidManifest.xml
You can define where Gradle can search for your AndroidManifest.
How to tell Gradle to use a different AndroidManifest from the command line?
select "android" from the drop down menu instead of "project"
I am trying to migrate my android app from eclipse to Android studio. (0.5.4)
The project has several dependencies. (Sherlock etc)
I exported the app to Gradle and imported it in Android studio and managed to get the project to build successfully.
It appears however that only the dependencies are built.
Adding erronous lines in the app code does not trigger compile errors.
When I view project | packages, the package for my app does not show, Only the external libraries are shown.
My root level build.gradle only contains repositories and dependencies:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
I tried manually adding an android section with the intent to make gradle look at the source files but I had problems adding the section:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.2"
}
As i now get the following error
Could not find method android() for arguments xxx on root project 'zzz'.
If proceeded to add
apply plugin: 'android'
Just before the android section, but now I get
A problem occurred evaluating root project 'zzz'.
Plugin with id 'android' not found.
Can anyone shed some light as to why i cannot have an Android section at the root level.
If the problem lies somewhere else any help would also be much appreciated.
In case it is needed, here is my settings.gradle
include ':external:PullToRefresh:SmoothProgressBarLib'
include ':external:ActionBarSherlock'
include ':'
include ':external:pulltorefresh-abs'
include ':external:MyAwesomeLibrary'
include ':external:PullToRefresh:pulltorefresh'
include ':external:SherlockNavigationDrawer'
include ':external:sdk:MyAwesomeSDK'
You've included an android block inside a buildscript block in your top-level build file, but this is incorrect. Instead it should be structured like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.2"
}
dependencies {
//Your app dependencies go here
}
All this is assuming that you truly have an Android application module at your project root (meaning that at your project root directory there's a src directory that has Android sources in it). It seems to be that you're trying to set it up this way because you also have this in your settings.gradle file:
include ':'
If that's the case, then rearranging your top-level build file as indicated above should fix it.
If you don't have a module at the project root, then you should restore the top-level build file to its original condition (take out apply plugin and android), take out that include ':' line from settings.gradle, and add an include statement that points to your application module.
In your settings.gradle file, I don't see where you included your main module. You should add:
include ':mainmodule-directory'
Your project should, ideally, have two build.gradle files. One at the root level, one at the module level. In your main module, you specify that the module is an Android module by adding apply plugin: android in the module's build.gradle file. Then you specify that the entire project would need the Android plugin by using the following in the root build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
I had a project in Android studio and I needed to delete .idea directory. So I importet it again, but after this, all modules in libs/ can't resolve anything from android (but app module can).
Is there any setting which connects module with android?
Thanks very much
This is screen from stickyListHeaders library:
biuld.gradle from stickyList:
apply plugin: 'android-library'
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 18
}
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
And project's settings.gradle
include ':libs/bakuDroidLibrary'
include ':libs/photoView'
include ':libs/pullToRefresh'
include ':libs/slidingMenu'
include ':libs/spinnerWheel'
include ':libs/stickyListHeaders_lib'
include ':##PROJECTNAME##'
Some other screenns which may help:
Top level build.gradle:
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
First Check whether File » Project Structure » Android SDK is pointing to right SDK.
Than make sure below lines are added before apply plugin: 'android-library'inside build.gradle file of your stickyheader library or inside Project root's build.gradle file.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
My suggestion is to avoid downloading and adding libraries in to your project, use maven dependency instead.
For stickyheader which you have used can be used just by adding dependency like this in your module's build.gradle file
dependencies {
compile 'se.emilsjolander:stickylistheaders:2.1.3'
}
Nothing else needs to be done.