It's a highly discussed problem on SO, but I couldn't find an up-to-date solution after hours of searching. Sadly I can't add comments anywhere.
I'm setting the versionName and versionCode in my build.gradle using the gradle-android-git-version Plugin. I now want to alter the name of the build outputfile from my-sdk-release.aar to my-sdk-release-versionName.aar or similar.
However, any solutions presented did not work, because properties were not found. The documentation on that matter seems to be outdated as well, OutputFile is long deprecated.
Going with the newest one I could find:
afterEvaluate {
android.libraryVariants.all { variant ->
variant.variantData.outputFactory.apkDataList.each { apkData ->
if (apkData.outputFileName.endsWith('.aar')) {
apkData.outputFileName = "${project.name}-${buildType.name}-anything-you-want.aar"
}
}
}
}
which is giving me the error No such property: outputFactory for class: com.android.build.gradle.internal.variant.LibraryVariantData on Gradle 7.3.3
My build.gradle goes as follows:
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'com.gladed.androidgitversion' version '0.4.14'
}
android {
compileSdk 33
defaultConfig {
minSdk 24
targetSdk 33
versionName androidGitVersion.name()
versionCode androidGitVersion.code()
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
kotlinOptions {
freeCompilerArgs += [
'-opt-in=kotlin.RequiresOptIn',
'-Xexplicit-api=strict'
]
}
afterEvaluate {
android.libraryVariants.all { variant ->
variant.variantData.outputFactory.apkDataList.each { apkData ->
if (apkData.outputFileName.endsWith('.aar')) {
apkData.outputFileName = "${project.name}-${buildType.name}-anything-you-want.aar"
}
}
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'no.nordicsemi.android:ble-ktx:2.5.1'
implementation 'no.nordicsemi.android.support.v18:scanner:1.5.1'
implementation 'commons-codec:commons-codec:1.13'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2'
testImplementation 'junit:junit:4.13.2'
}
What did I do wrong?
Related
Does anyone know of a way to template build.gradle files as modules are created? Right now the issue I have is that I want to be able to have the module's build.gradle contain only a small amount of information since I'd be setting the shared configuration within the top level build.gradle file. I have a buildSrc folder setup to centralize the versioning as well.
Top-level build.gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath Deps.tools_gradleandroid
classpath Deps.tools_kotlin
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
}
}
subprojects {
afterEvaluate { project ->
if (project.hasProperty('android')) {
android {
buildToolsVersion Config.buildTools
compileSdkVersion Config.compileSdk
defaultConfig {
minSdkVersion Config.minSdk
targetSdkVersion Config.targetSdk
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility Config.javaVersion
targetCompatibility Config.javaVersion
}
kotlinOptions {
jvmTarget = Config.javaVersion.toString()
}
}
}
}
}
A module's expected build.gradle file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
defaultConfig {
applicationId "com.company.expectedmodule"
}
}
dependencies {
implementation Deps.androidx_core
implementation Deps.androidx_appcompat
implementation Deps.androidx_material
testImplementation Deps.testlib_junit
androidTestImplementation Deps.testandroidx_junit
androidTestImplementation Deps.testandroidx_espressocore
}
What the build.gradle file looks like when I create a 'New Module':
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.company.newmodule"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
EDIT: For sake of simplicity I've moved the dependencies bracket to the top-level build.gradle file and also changed the modules to be libraries instead and reduced the code of a library module to below:
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
}
recommended way of sharing build logic in Gradle is by using convention plugins. Like in this sample.
Also precompiled script plugins can be used to write plugins in syntax similar to regular build scripts.
I am working on a dynamic feature module for the first time. I am getting issues in minify gradle task. It says ListenableFuture file exists in base.jar and alt_acco.jar
below is my project configuration:
mobile.gradle
dynamicFeatures = [':alt_acco']
alt_acco.gradle. feature module
apply plugin: 'com.android.dynamic-feature'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
tasks.lint.enabled = false
}
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
missingDimensionStrategy 'react-native-camera', 'general'
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
incremental true
}
buildTypes {
release {
}
releaseStaging {
matchingFallbacks = ['release']
}
debug {
}
}
productFlavors {
flavorDimensions "type"
standard {
dimension "type"
buildConfigField 'Boolean', 'Automation', 'false'
}
standardBasicOptimized {
dimension "type"
buildConfigField 'Boolean', 'Automation', 'false'
}
standard_charles {
dimension "type"
buildConfigField 'Boolean', 'Automation', 'false'
}
automation_charles {
dimension "type"
buildConfigField 'Boolean', 'Automation', 'true'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
}
dependencies {
// Import the Firebase BoM (see: https://firebase.google.com/docs/android/learn-more#bom)
implementation platform('com.google.firebase:firebase-bom:28.2.1')
// Firestore (Java)
implementation 'com.google.firebase:firebase-firestore'
// Firestore (Kotlin)
implementation 'com.google.firebase:firebase-firestore-ktx'
// Google Play services
implementation project(path: ':mobile')
}
settings.gradle
include ':alt_acco'
I am making a release build and the error I am getting is as follows:
Task :mobile:minifyStandardBasicOptimizedReleaseWithR8
/root/.gradle/caches/transforms-3/1169397e4788b932b8c86a71183af227/transformed/jetified-time4j-android-4.2-2018i/proguard.txt:1:1-27: R8: Ignoring option: -useuniqueclassmembernames
/opt/jenkins/workspace/APP-Android-Verify/mobile/build/intermediates/module_and_runtime_deps_classes/standardBasicOptimizedRelease/base.jar: R8: Type com.google.common.util.concurrent.ListenableFuture is defined multiple times: /opt/jenkins/workspace/APP-Android-Verify/mobile/build/intermediates/module_and_runtime_deps_classes/standardBasicOptimizedRelease/base.jar:com/google/common/util/concurrent/ListenableFuture.class, /opt/jenkins/workspace/APP-Android-Verify/alt_acco/build/intermediates/module_and_runtime_deps_classes/standardBasicOptimizedRelease/feature-alt_acco.jar:com/google/common/util/concurrent/ListenableFuture.class
> Task :mobile:minifyStandardBasicOptimizedReleaseWithR8 FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':mobile:minifyStandardBasicOptimizedReleaseWithR8'.
> com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /opt/jenkins/workspace/APP-Android-Verify/mobile/build/intermediates/module_and_runtime_deps_classes/standardBasicOptimizedRelease/base.jar:com/google/common/util/concurrent/ListenableFuture.class
Help will be really appreciated.
Hey I had same issue because we had gradle cache enabled, I got it working by adding ./gradlew bundleRelease --no-build-cache, hope it helps.
After searching 2 days on the internet I wasn't able to find a way to rename the output file name of android-test.apk (for instrumented tests). I am building debug and instrumented tests apk with assembleAndroidTest and assembleDebug.
For debug build I managed to change the name and add version, but I couldn't do it for androidTest.apk. How can I add the same version and other name for test apk?
Here is my build.gradle (module)
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
project.archivesBaseName = "Automator"
defaultConfig {
applicationId "com.xxx.automator"
minSdkVersion 18
targetSdkVersion 29
versionCode 1
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
android.applicationVariants.all { variant ->
variant.outputs.all {
def appName = "Automator"
outputFileName = appName + "-${variant.versionName}.apk"
}
}
}
}
}
Add this code to your app build.gradle:
android {
// Make custom APK name for test builds
testVariants.all { testVariant ->
testVariant.outputs.all { output ->
outputFileName = "CustomNameTest.apk"
}
}
}
Put the following code in your app 'build.gradle' file
android {
buildTypes {
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "Your_Desired_Name.apk"
}
}
}
This will work with latest sdk as well as latest os version.
Could not find property jni and source set 'main'
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 21
buildToolsVersion = "22.0.1"
defaultConfig.with {
applicationId = "com.example.native_activity"
minSdkVersion.apiLevel = 9
targetSdkVersion.apiLevel = 9
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir = 'src/main/libs'
// This is not necessary unless you have precompiled libraries in your project.
}
}
Here is the stacktrace:
Caused by: org.gradle.model.internal.core.ModelRuleExecutionException: Exception thrown while executing model rule: model.android
at org.gradle.model.internal.registry.DefaultModelRegistry.fireMutation(DefaultModelRegistry.java:485)
at org.gradle.model.internal.registry.DefaultModelRegistry.access$1500(DefaultModelRegistry.java:45)
at org.gradle.model.internal.registry.DefaultModelRegistry$RunModelAction.apply(DefaultModelRegistry.java:1464)
at org.gradle.model.internal.registry.DefaultModelRegistry.transitionTo(DefaultModelRegistry.java:341)
at org.gradle.model.internal.registry.DefaultModelRegistry.transition(DefaultModelRegistry.java:419)
at org.gradle.model.internal.registry.DefaultModelRegistry.atStateOrMaybeLater(DefaultModelRegistry.java:183)
at org.gradle.model.internal.registry.DefaultModelRegistry.atStateOrLater(DefaultModelRegistry.java:175)
at org.gradle.execution.TaskNameResolver.selfClose(TaskNameResolver.java:101)
at org.gradle.execution.TaskNameResolver.selfClosedTasksNode(TaskNameResolver.java:114)
... 60 more
Caused by: org.gradle.api.internal.MissingMethodException: Could not find method main() for arguments [build_f1cmjkxjjzysskbrs6852ixyj$_run_closure1_closure2_closure7#8c09fa7] on SourceSet container.
I googled like mad for the last 2 hours...
As Awanish said - read the Experimental Plugin User Guide step by step VERY carefully. For even more clearance check the build.gradle files in the ndk-samples provided by google.
sourceSets.main { } has different syntax and should be outside the android { } block. In your case it should look something like this:
model {
android {
//...
}
android.sources {
main {
jniLibs {
source {
srcDirs 'libs'
}
}
}
}
}
In my case it was an exact opposite to tochkov answer:
This syntax - with the jniLibs outside android block - gave me an error:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.mycompany.myproject"
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
and this syntax - inside android block - fixed it:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.mycompany.myproject"
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
}
It seems like you are trying to use the grade experimental plugin!
Make sure you have grade-2.5 in your gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
and this in your project's build.gradle dependencies tag
classpath 'com.android.tools.build:gradle-experimental:0.1.0'
Instead of guessing a lot of things, let me redirect you to the user guide, where the documentation explains migration from the standard to experimental plugin in detail. Take a look here!
Try these fixes. Post more details if these don't work for you, I will try to help. :)
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.jeremy.test"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFiles.add(file('proguard-android.txt'))
}
}
ndk {
moduleName "hello-android-jni"
}
}
android.sources.main {
java.source.srcDirs = ["src/main/java", "/Users/jeremy/Repositories/hello/src/android/java"]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
The above is copied from my experimental ndk project, this works for me.
See the android.sources.main section.
I have follow the instruction to add jar library but when i am going to synchronize the build grade then getting error.
Could not find property 'file' on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated#1a9bd9bb.
I am not getting what is error. please suggest.
build gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.cws.myapplication"
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"));
}
}
}
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/layout'] } }
}
def var = dependencies {
compile 'com.android.support:appcompat-v7:22.0.0'
compile file 'libs/library-2.1.1.jar'
}
In Build.Gradle file the code to add the library is
compile files('libs/acra-4.6.0RC1.jar')
The letter 's' in 'files' is missing from the command.