IDEA does not always deploy latest changes - android

I tried to run both the android app and the compose for desktop app in one folder, but I ran into some problems.
MyProject
│ │└──android
│ │ │ └── app
│ │ │ └──build.gradle.kts
│ │ └── build.gradle.kts
│ └── desktop
│ └── build.gradle.kts
└── settings.gradle.kts
settings.gradle.kts
pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
plugins {
kotlin("multiplatform").version("1.7.10")
id("org.jetbrains.compose").version("1.2.0")
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "MyProject"
include("android:app")
include("desktop")
My Android app has a single module architecture, but when I run android.app in IDEA, it tries to install the apk directly on my emulator/phone without going through the building stage. If I delete the app-debug.apk in the build folder and run it again, it will not be able to install the apk.
IDEA: IntelliJ IDEA 2022.3.2 (Ultimate Edition)
AGP: 7.3.0
gradle: 7.5.1
02/06: Launching 'android.app' on <default>.
Error loading build artifacts from: C:\Users\...\android\app\build\intermediates\apk_ide_redirect_file\debug\redirect.txt
Just like the error above, IDEA seems to be forever trying to install the cached apk instead of building the latest apk

This answer worked for me, but I still don't understand why I have this problem when I change from a single-module gradle project to a multi-module gradle project.
https://stackoverflow.com/a/65955788/15486462

Related

Gradle composite builds with custom repositories in Android Studio

Our company requires we have all our dependencies downloaded from our internal repository (Artifactory). Our Android project has the main Android modules, plus composite builds.
I was able to replicate the issue creating a simple project from Android Studio, and adding a composite build (or buildSrc) into it.
More or less like this (I didn't include the other files above to keep it simple):
project
├─ app (<-- application module)
├─ plugins (<-- composite build )
│ ├─ settings.gradle.kts (<-- settings for composite build)
├─ settings.gradle.kts (<-- settings for main project)
Both settings.gradle.kts (main + composite build projects) have something like this (again, leaving irrelevant elements out of it):
pluginManagement {
repositories{
maven(url="https://artifactory.mycompany.org")
}
}
dependencyResolutionManagement {
repositories{
maven(url="https://artifactory.mycompany.org")
}
}
This works perfectly while running from terminal (e.g. ./gradlew build), but the sync operation via Android Studio seems to always trigger dependencies download from *.gradle.org. After the sync, all the other project dependencies for build operations seem to work on Android Studio. If I create a project without composite builds (or buildSrc) it seems the issue doesn't happen.
This has been creating many issues, as you can imagine, but it doesn't seem that many people have been reporting the exact same experience. Apart from the issue of downloading dependencies from a different repository, it is also downloading these dependencies multiple times during the day.
I've also checked environment variables, Java SDK, etc to match between Android Studio and terminal, and Android Studio is also configured to use gradle from "gradle-wrapper.properties".
Has anyone faced similar issue, and has any clues on what else I could be missing? Or could that be an issue on the Android Studio gradle plugin?

Apply Lint Task to All Subproject in Gradle

I have a multi-module project with a few Android apps and Android Library modules. It looks something like this:
├── awesomeapp (Android App)
├── coolapp (Android App)
└── modules
├── usefulprojectA (Android Lib)
└── usefulprojectB (Android Lib)
Both awesomeapp and coolapp depend on library modules:
implementation project(':modules:usefulprojectA:')
implementation project(':modules:usefulprojectB:')
What I would like to do is be able to say something like:
./gradlew awesomeapp:lint
or even
./gradlew awesomeapp:build
And have the lint task execute on awesomeapp and on both usefulprojectA and usefulprojectB.
Currently the lint task would only apply to the top level project, i.e., awesomeapp. So if I wanted to Lint the subprojects I'd have to call the lint task on the subproject directly:
./gradlew modules:usefulprojectA:lint
./gradlew modules:usefulprojectB:lint
Plus call the main project:
./gradlew awesomeapp:lint
But that's clumsy and will make my build script longer in both time and size.
So how does one apply the Lint task to subprojects too?
You can check module and it's dependencies by adding checkDependencies true to your lintOptions:
android {
lintOptions {
checkDependencies true
}
}

How to reuse the submodule in Gradle?

I have two following Android Studio project, structure like this:
projectA/
├----build.gradle
├----settings.gradle
├----bluewhale/
├----krill/
projectA settings.gradle file:include 'bluewhale', 'krill'
projectB/
├----build.gradle
├----settings.gradle
├----hello/
├----krill/
projectB settings.gradle file:include 'hello', 'krill'
You can see "projectA" and "projectB" contain the same module "krill". Actually, it's a library project.
My question is: how to reuse the submodule "krill" in Gradle? I don't want to include the same copy of "krill" in every project
Looking forward to your reply! Thanks!
If you have a sub-module that is used in multiple projects, you should think about extracting it to a separate project. Then you can create a dependency out of it and include it in both projects in dependencies section.
If you only use your local machine for development, without any custom repository, the best way would probably be to use the mavenLocal() repository. You can use the maven publish plugin to publish your jar into the local maven repository. It should be as simple as adding this to the new krill:
apply plugin: 'maven'
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
repositories {
mavenLocal()
}
You might want to set the group and artifact ID. See the documentation for more info.
You can also keep krill in one of the projects, let's say ProjectA, if it has some relation to it. Then you set up the maven publishing in the krill sub-module. You can also publish to maven local by running gradle :krill:publishToMavenLocal and then use it as dependency in ProjectB.
Another option is to save the submodule outside the projectA and projectB trees and add it using something like this:
include("krill")
project(":krill") {
projectDir = new File("$settingsDir/../krill")
}
But I can't recommend this because it's hacky and your IDE might have a problem with it too.
Last thing that might be possible is to create symlinks from a directory where your krill project is located to both ProjectA and ProjectB. But that is a really bad idea e.g. when you are using a version control.
Finally I found an article here: an-alternative-multiproject-setup-for-android-studio. It works for me perfectly!
It shows us another Way different from Google’s Gradle Plugin user guide recommends
Sample code below: (Add this script to your project settings.gradle file)
include ':krill'
project(':krill').projectDir = new File('../otherProject/krill')
Project structure below:
RootFolder/
├----projectA/
│ ├----build.gradle
│ ├----settings.gradle
│ └----bluewhale/
│
├----projectB/
│ ├----build.gradle
│ ├----settings.gradle
│ └----hello/
│
└----otherProject/
├----krill/
│ └----build.gradle
│
└----otherModule/
└----build.gradle
For more details, visit gradle official document: Multi Project Builds

Android Studio 1.0.2 proguard rules

I imported an Eclipse project into Android Studio and now I'm trying to revive the proguard part. I have the following in build.gradle:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
and now the questions are:
where and how do I add the file proguard-android.txt and proguard-rules.txt?
The syntax/format of this file in ADT is the same as it was in Eclipse?
I now noticed that I also have cannot resolve symbol 'getDefaultProguardFile'
Here you can find some more information on what getDefaultProguardFile() does. In essence, Android provides two Proguard files for you to use, one with optimizations, one without.
As for your custom proguard-rules.txt file, put it (if it isn't already there) in the same place where your build.gradle file of your App is located. See the SeriesGuide repository for an example.
Edit
Your directory structure (as can be seen in the SeriesGuide repo linked above) should be something like this (directories like src/ have been omitted):
.
├── app
│   ├── build.gradle # this is probably where you defined android { . . . }
│ │ # and thus `proguardFiles`
│   └── proguard-project.txt
└── build.gradle # setting up buildscript, maven repositories etc

Android Studio module that depends on another module's tests

I'm trying to get the Gradle Java plugin to execute jUnit tests that exist in an Android project. My solution was to create a second module that applies the Java plugin, and set the test sourceSet to the app module's src/test directory.
test-module's build.gradle:
apply plugin: 'java'
...
dependencies {
...
testCompile project(':app-module')
}
sourceSets {
test {
java.srcDirs += ["${appDir}/src/test/java"]
}
}
This works fine from the command line, but Android Studio refuses to import a project that has source sets outside the submodule. It throws the error: Can't register given path of type 'TEST' because it's out of content root.
.
├── app-module
│   ├── build.gradle
│   └── src
│   ├── main
│   └── test
├── test-module
│   └── build.gradle
├── build.gradle
└── settings.gradle
I tried configuring this from the parent build.gradle, but that didn't change anything. I can add app-module as a testCompile project dependency in test-module, but that doesn't cause test-module to add app-module's tests.
Any ideas for getting test-module to run app-module's tests without provoking Android Studio's limitation about remote source sets?

Categories

Resources