Gradle task to generate Android manifest - android-gradle-plugin

In my Android project AndroidManifest.xml needs to be built from other files. I wrote a task called generateManifest.
What should I specify in my Gradle build script to make sure that generateManifest is executed before AndroidManifest.xml is accessed by the other Android tasks? In other words, what Android plugin tasks should I configure as .dependsOn generateManifest ?

preBuild.dependsOn generateManifest
Plus the less obvious:
android {
defaultConfig {
applicationId 'your.app.id'
}
}
Without it, the Android plugin during its initialization will try to read the package id from the (non-existing) manifest.

Related

Unable to find method 'org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension.macosX64()

I've updated kotlin from 1.3.61 to 1.3.70 in my multiplatform project with android, jvm, ios and macosx64 targets not changing other code and whenever a gradle sync is attempted I get the following message (and the project build fails, of course):
CONFIGURE FAILED in 3s
Unable to find method 'org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension.macosX64()Lorg/jetbrains/kotlin/gradle/plugin/mpp/KotlinNativeTargetWithHostTests;'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
I've tried the solutions described above, I also tried to clear .gradle/caches, but nothing changed. What might be the problem?
This happened because I had a buildSrc module where the dependencies' artifact names are declared like this:
object Deps {
object Android {
// artifacts
}
object Native {
// artifacts
}
// ...
}
and I had updated the kotlin dependency there, but I had forgotten to update the kotlin version in the build.gradle.kts of the buildSrc directory itself.

After adding new RN package I get "The SDK Build Tools revision is too low for project"

I don't understand what's going on here. I added a new package to my react-native app with these commands:
yarn add react-native-version-number
react-native link
Now when I do react-native run-android I get this error:
The SDK Build Tools revision (23.0.1) is too low for project
':react-native-version-number'. Minimum required is 25.0.0
Does this mean the package I'm adding requires SDK Tools 25.0.0 or higher? Or does it mean my app requires packages that use SDK Tools > 25.0.0? Because in my app/build/build.gradle file I have:
...
compileSdkVersion 26
buildToolsVersion "26.0.3"
...
The build.gradle file for react-native-version-number contains this:
...
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
I found that if I modify this to say "26.0.3" then that error goes away but I get the same error on a different package that was working before. It doesn't seem like I should have to modify package files. Apparently when I ran yarn add it must have changed something in my app to require a higher version of SDK Tools. How do I fix this?
Note: I'm on Windows and I'm not using Android Studio
You can try to add the following script to you android/build.gradle (not android/app/build.gradle).
This section will force all the libraries to use defined versions of compileSDK and buildTools:
subprojects {
afterEvaluate {project ->
if (project.hasProperty("android")) {
android {
compileSdkVersion 26
buildToolsVersion '26.0.3'
}
}
}
}
I found the answer here: https://stackoverflow.com/a/43954560/1715240
Basically I had to edit my top-level build.gradle file and change this line to use Gradle v2.2.3
classpath 'com.android.tools.build:gradle:2.2.3'

AirWatch seems to be incompatible with Android Plugin for Gradle 3.0.0-beta1

After changing my top level build.gradle to use the Android Plugin for Gradle 3.0.0-beta3 along with Gradle 4.1, AirWatch can't read the info (like versionCode and versionName) from the APK.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
roomVersion = '1.0.0-alpha9'
supportLibVersion = '26.0.1'
}
Is this a known issue? Any workaround?
Airwatch has an issue with the new AAPT2 tool which seems to compile the Manifest.xml file in a compressed form in contrast to AAPT. AAPT2 became the standard with Android Studio 3 but you can get back easily to AAPT by putting the line
android.enableAapt2=false
in the gradle.properties file of your project. Solved at least in our project the problem.
I don't have enough reputation to comment, but I just wanted to ask, if you have found out anything new about this topic, since our clients use AirWatch and we have run into a similar problem after updating gradle.
Specifically after updating from Android Studio 2 to Android Studio 3 and thus changing all gradle stuff coming with it.
As of this writing, Airwatch has a limitation where it will not update the app compiled with Android gradle plugin 3.0.
There are 2 work arounds.
1. Build the apk using the gradle plugin 2.3.3
or
2. Upload the apk built using gradle plugin 3.0 as a separate app on the console. Define App/ID version code manually and deploy.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

How do I make IDEA/Android Studio recognize my unit test folder when using Gradle?

So I've successfully managed to make Gradle run standard Junit tests for my Android project by following this guide, but I'm struggling getting Gradle to generate my .IML file so that IDEA recognizes my unit test folder. It is not green in the project tree.
So this is an excerpt of my build.gradle file:
apply plugin: 'android'
sourceSets {
unitTest {
java.srcDir file('src/test/java')
}
}
task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
This is the line I in the .IML file I want Gradle to generate, but it doesn't:
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
If I add it manually, it's removed by Gradle any time I make any change to build.gradle, so that sucks.
I also tried adding these lines to the build file, without any difference:
apply plugin: 'idea'
idea {
module.testSourceDirs += file('scr/test/java')
}
I press the refesh button each time I do any changes so that the IML file will be regenerated.
What am I doing wrong?
I'm using Android Studio 1.3.2 with Gradle 1.3.0 and found that I have to change the Test Artifact to Unit Tests in the Build Variants tab. After doing that the IDE works as expected. I found the information at http://tools.android.com/tech-docs/unit-testing-support.
I've had trouble when I don't use the default gradle file structure. Try putting your tests under "src/androidTest/java" and then modify your "build.gradle" file to include test information in the "defaultConfig"
android {
defaultConfig {
testPackageName "com.yourpackage.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testFunctionalTest true
}
}
This works for me - I'm able to run tests from inside Android Studio.