Gracefully handle missing gradle definitions (use default values if definition is missing)
I usually do following in my projects:
1) define a versions.gradle file, e.g.:
ext {
setup = [
compileSdk: 28,
enableDataBinding: true,
minSdk : 16,
targetSdk : 28
]
androidx = [
supportv4: "1.0.0",
appcompat: "1.0.0",
cardview: "1.0.0",
viewpager: "1.0.0",
material: "1.0.0"
]
}
2) I add this versions file to my projects gradle file:
apply from: './versions.gradle'
3) I use the versions from the file in all my project gradle files for consistant library versions, like e.g.:
implementation "androidx.appcompat:appcompat:${androidx.appcompat}"
Question
How can I handle this gracefully in open source libraries I use? I want that if someone checks out my library and adds the library directly to his projects, that some default version is provided so that this line implementation "androidx.appcompat:appcompat:${androidx.appcompat}" won't throw any error (${androidx.appcompat} should be replaced by 1.0.0 e.g. in this case).
Is there some way to solve this easily? I want that the version file is used if available and a fallback version is used otherwise...
add a comment |
I usually do following in my projects:
1) define a versions.gradle file, e.g.:
ext {
setup = [
compileSdk: 28,
enableDataBinding: true,
minSdk : 16,
targetSdk : 28
]
androidx = [
supportv4: "1.0.0",
appcompat: "1.0.0",
cardview: "1.0.0",
viewpager: "1.0.0",
material: "1.0.0"
]
}
2) I add this versions file to my projects gradle file:
apply from: './versions.gradle'
3) I use the versions from the file in all my project gradle files for consistant library versions, like e.g.:
implementation "androidx.appcompat:appcompat:${androidx.appcompat}"
Question
How can I handle this gracefully in open source libraries I use? I want that if someone checks out my library and adds the library directly to his projects, that some default version is provided so that this line implementation "androidx.appcompat:appcompat:${androidx.appcompat}" won't throw any error (${androidx.appcompat} should be replaced by 1.0.0 e.g. in this case).
Is there some way to solve this easily? I want that the version file is used if available and a fallback version is used otherwise...
1
You can use elvis operator to replace default version if defined version not found like this :implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 1.0.0}"
– Jeel Vankhede
Nov 19 '18 at 7:05
Thanks, that's the perfect solution. Better than some if in my gradle file and defining all missing definitions. One thing though, the number must be put in'
– prom85
Nov 19 '18 at 7:11
Yah, just copy pasted it.. so missed that !
– Jeel Vankhede
Nov 19 '18 at 7:12
No problem, could find this out myself ;-) Just write it as an answer and I'll accept it
– prom85
Nov 19 '18 at 7:14
add a comment |
I usually do following in my projects:
1) define a versions.gradle file, e.g.:
ext {
setup = [
compileSdk: 28,
enableDataBinding: true,
minSdk : 16,
targetSdk : 28
]
androidx = [
supportv4: "1.0.0",
appcompat: "1.0.0",
cardview: "1.0.0",
viewpager: "1.0.0",
material: "1.0.0"
]
}
2) I add this versions file to my projects gradle file:
apply from: './versions.gradle'
3) I use the versions from the file in all my project gradle files for consistant library versions, like e.g.:
implementation "androidx.appcompat:appcompat:${androidx.appcompat}"
Question
How can I handle this gracefully in open source libraries I use? I want that if someone checks out my library and adds the library directly to his projects, that some default version is provided so that this line implementation "androidx.appcompat:appcompat:${androidx.appcompat}" won't throw any error (${androidx.appcompat} should be replaced by 1.0.0 e.g. in this case).
Is there some way to solve this easily? I want that the version file is used if available and a fallback version is used otherwise...
I usually do following in my projects:
1) define a versions.gradle file, e.g.:
ext {
setup = [
compileSdk: 28,
enableDataBinding: true,
minSdk : 16,
targetSdk : 28
]
androidx = [
supportv4: "1.0.0",
appcompat: "1.0.0",
cardview: "1.0.0",
viewpager: "1.0.0",
material: "1.0.0"
]
}
2) I add this versions file to my projects gradle file:
apply from: './versions.gradle'
3) I use the versions from the file in all my project gradle files for consistant library versions, like e.g.:
implementation "androidx.appcompat:appcompat:${androidx.appcompat}"
Question
How can I handle this gracefully in open source libraries I use? I want that if someone checks out my library and adds the library directly to his projects, that some default version is provided so that this line implementation "androidx.appcompat:appcompat:${androidx.appcompat}" won't throw any error (${androidx.appcompat} should be replaced by 1.0.0 e.g. in this case).
Is there some way to solve this easily? I want that the version file is used if available and a fallback version is used otherwise...
edited Nov 19 '18 at 7:14
prom85
asked Nov 19 '18 at 6:48
prom85prom85
5,282960146
5,282960146
1
You can use elvis operator to replace default version if defined version not found like this :implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 1.0.0}"
– Jeel Vankhede
Nov 19 '18 at 7:05
Thanks, that's the perfect solution. Better than some if in my gradle file and defining all missing definitions. One thing though, the number must be put in'
– prom85
Nov 19 '18 at 7:11
Yah, just copy pasted it.. so missed that !
– Jeel Vankhede
Nov 19 '18 at 7:12
No problem, could find this out myself ;-) Just write it as an answer and I'll accept it
– prom85
Nov 19 '18 at 7:14
add a comment |
1
You can use elvis operator to replace default version if defined version not found like this :implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 1.0.0}"
– Jeel Vankhede
Nov 19 '18 at 7:05
Thanks, that's the perfect solution. Better than some if in my gradle file and defining all missing definitions. One thing though, the number must be put in'
– prom85
Nov 19 '18 at 7:11
Yah, just copy pasted it.. so missed that !
– Jeel Vankhede
Nov 19 '18 at 7:12
No problem, could find this out myself ;-) Just write it as an answer and I'll accept it
– prom85
Nov 19 '18 at 7:14
1
1
You can use elvis operator to replace default version if defined version not found like this :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 1.0.0}"– Jeel Vankhede
Nov 19 '18 at 7:05
You can use elvis operator to replace default version if defined version not found like this :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 1.0.0}"– Jeel Vankhede
Nov 19 '18 at 7:05
Thanks, that's the perfect solution. Better than some if in my gradle file and defining all missing definitions. One thing though, the number must be put in
'– prom85
Nov 19 '18 at 7:11
Thanks, that's the perfect solution. Better than some if in my gradle file and defining all missing definitions. One thing though, the number must be put in
'– prom85
Nov 19 '18 at 7:11
Yah, just copy pasted it.. so missed that !
– Jeel Vankhede
Nov 19 '18 at 7:12
Yah, just copy pasted it.. so missed that !
– Jeel Vankhede
Nov 19 '18 at 7:12
No problem, could find this out myself ;-) Just write it as an answer and I'll accept it
– prom85
Nov 19 '18 at 7:14
No problem, could find this out myself ;-) Just write it as an answer and I'll accept it
– prom85
Nov 19 '18 at 7:14
add a comment |
1 Answer
1
active
oldest
votes
Trying Elvis operator for placing default value can do that trick, so replacing it with operator like below works :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 'default value here'}"
More from here.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369598%2fgracefully-handle-missing-gradle-definitions-use-default-values-if-definition-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Trying Elvis operator for placing default value can do that trick, so replacing it with operator like below works :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 'default value here'}"
More from here.
add a comment |
Trying Elvis operator for placing default value can do that trick, so replacing it with operator like below works :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 'default value here'}"
More from here.
add a comment |
Trying Elvis operator for placing default value can do that trick, so replacing it with operator like below works :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 'default value here'}"
More from here.
Trying Elvis operator for placing default value can do that trick, so replacing it with operator like below works :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 'default value here'}"
More from here.
answered Nov 19 '18 at 7:19
Jeel VankhedeJeel Vankhede
2,2502318
2,2502318
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369598%2fgracefully-handle-missing-gradle-definitions-use-default-values-if-definition-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can use elvis operator to replace default version if defined version not found like this :
implementation "androidx.appcompat:appcompat:${androidx.appcompat ?: 1.0.0}"– Jeel Vankhede
Nov 19 '18 at 7:05
Thanks, that's the perfect solution. Better than some if in my gradle file and defining all missing definitions. One thing though, the number must be put in
'– prom85
Nov 19 '18 at 7:11
Yah, just copy pasted it.. so missed that !
– Jeel Vankhede
Nov 19 '18 at 7:12
No problem, could find this out myself ;-) Just write it as an answer and I'll accept it
– prom85
Nov 19 '18 at 7:14