How to inject Android configuration to each subproject with Gradle? - android

Rather than duplicating the android configuration block in each of the sub projects:
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
I would much rather put this in the top-level/root gradle build file like:
subprojects{
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
However, this doesn't work. :(
Error:
"..Could not find method android() for arguments..."

The solution to this turned out to be:
subprojects{
afterEvaluate {
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
}
As far as I know, this is because to process/use the android{...} at evaluation time means it must be present (ie. explicitly written or included as part of an 'apply plugin') as soon as it hits the subprojects in the root build file. And, more accurately, it means that the top-level project must have it defined (which it likely doesn't because it might not be an 'android' or 'android-library' build itself). However, if we push it off to after the evaluation then it can use what is available within each subproject directly.
This question + solution also assume that all subprojects are some form of android project (in my case true, but not necessarily for others).
The safer answer would be to use:
subprojects{
afterEvaluate {
if(it.hasProperty('android')){
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
}
}

Related

I've two different react native packages which require different android compileSdkVersion, how do I fix this?

I use react-native-music-control and react-native-navigation(by wix) , rnmc requires compilesdkversion 23 and buildToolsVersion '23.0.1' whereas rnn requires compilesdkversion 25 and buildToolsVersion '25.0.0', rnn does not work with 25.0.0/25 and rnn won't work with 23/23.0.1 . I don't have much experience with android so I'm not sure how to fix this, any guidance would be great.
Add the following to build.gradle in your projects root directory
subprojects {
afterEvaluate {
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
targetSdkVersion 25
}
}
}
}

How do I reference Build.VERSION_CODES in my build.gradle file?

My build.gradle file contains this line:
targetSdkVersion 26
I would prefer it to be like this:
targetSdkVersion Build.VERSION_CODES.o
Is that possible? It would seem much cleaner/safer, but this syntax doesn't work.
The target SDK version appears in multiple projects [...] It would be nice to define this in one place for all usage instances
So we're talking about defining project-wide accessible build time values.
Put this in your root project build.gradle:
ext {
compileSdkVersion = 26
buildToolsVersion = "26.0.0"
minSdkVersion = 17
targetSdkVersion = 26
supportLibVersion = "26.0.0"
playServicesVersion = "10.2.6"
}
And now you can reference these values in your module build.gradle like so:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
// ...
}
// ...
}
dependencies {
compile "com.android.support:design:$supportLibVersion"
compile "com.google.firebase:firebase-messaging:$playServicesVersion"
// ...
}
// ...
I think you can even omit the rootProject.ext. prefix as long the result name does not conflict with a DSL member name.
In Groovy you can use variables in strings as long as you use double quotes.
Is that possible?
No, sorry. Build is a Java class that exists in the Android framework. It is not available to Gradle.

Gradle build - if then else possible

In an open source library of mine, I use something like following:
android {
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
I don't want to force everyone to define those constants, but I want to use them and I use the same constants in all my projects and libraries. So I want to be able to use one code that works for me and for everyone else not defining those variables. I'm looking for something like following:
android {
// pseudo code line
if setup is defined
{
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
// default setup, if the user did not define global constants
else
{
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
}
}
}
Is something like that possible? Any alternative suggestions?
I imagine that your setup variable is stored in project extension
project.ext.setup = setup
This way, it can be accessible from your project and all your subprojects
You can test the existence of setup like this
if (project.hasProperty('setup'))
The idea is to create a default setup var if no one is provided
if (!project.hasProperty('setup')){
project.ext.setup = new Setup()
project.setup.compileSdk = 24
project.setup.buildTools = "24.0.2"
project.setup.minSdk = 16
project.setup.targetSdk = 24
}
android {
compileSdkVersion project.setup.compileSdk
buildToolsVersion project.setup.buildTools
defaultConfig {
minSdkVersion project.setup.minSdk
targetSdkVersion project.setup.targetSdk
}
}
Sure, I've tried this and it built successfully.
android{
...
if (someCondition){
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
}else {
compileSdkVersion 24
buildToolsVersion "24.0.3"
...
}
}

Importing OpenCV Sample to Android Studio

I am trying to import a OpenCV sample I got from http://ee368.stanford.edu/Android/OpenCV/ to Android Studio on a windows machine.
However, I'm getting the following error
* Project CVCamera MSER:C:\AndroidDevelopment\CVCamera_MSER\project.properties:
Library reference ..\..\android-jni could not be found
Path is C:\AndroidDevelopment\CVCamera_MSER\..\..\android-jni which resolves to C:\android-jni
Any ideas?
EDIT - more info:
I'm using the experimental gradle plugin
`classpath 'com.android.tools.build:gradle-experimental:0.6.0-alpha7'
And my gradle file is as follows:
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig.with {
applicationId "co.vispera.moka"
minSdkVersion.apiLevel 17
targetSdkVersion.apiLevel 23
versionCode 1
versionName "1.0"
}
}
android.ndk {
moduleName = "mymodule"
// ldLibs.addAll(['log'])
// cppFlags.add("-std=c++11")
// cppFlags.add("-fexceptions")
// stl = '
}
}
1.Maybe your sample's default API version codes are too low.
If you're using Android Studio, try changing build.grade Version codes to newer ones.
example:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
}
}
2.If same thing happens, try copying OpenCV libs folder to your project.
Copy OpenCV libs folder,(OpenCVPath/sdk/native/libs)
and paste it inside your app's 'main' folder.
Don't forget to rename it to 'jniLibs'.

How to force a gradle sub project to use root project's buildToolsVersion/compileSdkVersion?

Looking for some smart way of doing this only once e.g.
allprojects {
android {
buildToolsVersion '18.0.1'
compileSdkVersion 18
defaultConfig {
targetSdkVersion 18
}
}
}
Otherwise I need to up this thing in every subproject (e.g. in referenced library like volley or viewpagerindicator) every time there's a new SDK update available.
Typically the root project wouldn't contain code and hence you would use subprojects. Also you'll have to apply the Android plugin before the android {} block. Other than that, I'd expect this to work.
I answered in another question:
I found a solution here.
Will copy it here just in case:
In you root build.gradle add
ext {
compileSdkVersion = 20
buildToolsVersion = "20.0.0"
}
subprojects { subproject ->
afterEvaluate{
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
}
}
}
This will apply compileSdkVersion and buildToolsVersion to any android modules you have.
And in your main project's build.gradle change dependencies to this:
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
Basically you are defining them once and could use from anywhere.
Cheers.

Categories

Resources