Safe Args do not generate classes - android

I have read the other threads on this but still can't get it to work.
I've added,
def nav_version = "2.3.5"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
To the Project Gradle, and:
id 'androidx.navigation.safeargs'
To the app Gradle file.
I have:
android.useAndroidX=true
android.enableJetifier=true
in the gradle.properties file
I've followed the tutorials to the letter, adding argument but still I don't get the direction or any other classes generated once I rebuild the project.
What am I missing?

Maybe you are using kotlin (as infered from your tags) and an alternative way is to use the plugin for Safe Args just in Kotlin:
build.gradle (:project):
buildscript {
ext.nav_version = "2.3.5"
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
build.gradle (:app):
plugins {
id 'androidx.navigation.safeargs.kotlin'
}
Verify:
Kotlin >= 1.4
Android Studio >= 4.2.

Ok, the answer was found, thanks to #anshul:
The problem is not that the files are not generated, they are.
But, the IDE does not find them. As I am using Kotlin, the answer is a bit different from that for Java.
You must add to your app-level Gradle
android {
...
sourceSets {
main {
kotlin {
// srcDirs += 'build/generated/source/navigation-args/'
srcDirs += 'build/generated/source/navigation-args/debug/com/example/navanddata/ui/'
}
}
In my IDE, if you use the first (commented out) line, you get a multitude of folders that appear in your IDE sidebar. But if you go for the second, you must update it to your project name and update it for DBG / REL compilation.
In both cases, you will have to look for the files for a bit as they do not appear where you would expect, rather (in the second option) under a folder called Kotlin are “root level”.

Related

Replacing a gradle dependency in Flutter library

I'm writing a Flutter app that uses the "flutter_reactive_ble" plugin. That plugin, in turn, uses "RxAndroidBle" java library. I have made a small change to RxAndroidBle, and it compiles into .aar files. But my Gradle-fu is insufficient to figure out how to tell flutter_reactive_ble to use my version instead of retrieving the latest version from the web. I'd be happiest if I could just point to my local .aar files, but if I have to serve them from my own site, that would be OK as well.
After building my app, the "android/app/build.gradle" file ends with:
dependencies {
. . .
implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
}
I assume that's what I'll need to change, but nothing I can find in the Gradle docs look like what I need.
I think, you need fork repo first
And .yaml set like this
(read this)
I think you can do something like this in gradle:
configurations.each {
c -> c.resolutionStrategy.dependencySubstitution {
all { DependencySubstitution dependency ->
if (dependency.requested.group == 'com.polidea') {
dependency.useTarget '<your grade plugin>'
}
}
}
}
It can be replaced alike this:
configurations.all {
exclude group: 'com.polidea.rxandroidble2', module: 'rxandroidble'
}
dependencies {
implementation files('libs/rxandroidble.aar')
// or depend on the module.
}
If you want your app to use your aar, then just declare it as dependency. Or you want the flutter_reactive_ble to use it? I don't think it's possible. You'd have to make pr to RxAndroidBle and then pr to flutter_reactive_ble to take that version in.
I think overriding android dependency may solve your problem.
in build.gradle file of android project
configurations.all {
resolutionStrategy {
force ‘<android library with specific version>’
}
}
in your case:
force com.polidea.rxandroidble2:rxandroidble:1.11.1
check this link for more detail.
--------------------
for adding dependency of local project
in your settings.gradle file add mapping of your local project and include it
project(':local_project_name').projectDir = new File(settingsDir, '<relative path of project>')
include ':local_project_name'
And then add a resolutionStrategy in your project's build.gradle file
configurations.all {
resolutionStrategy {
force implementation project(':local_project_name')
}
}
You need to fork flutter_reactive_ble, go to build.gradle and remove this line then open android studio and open File > Project Structure > Dependencies:
Then Add your module library(.aar files you built):
in the end, android studio will add this line to build.gradle:
implementation(project(path: ":example-library"))
Make sure to use flutter_reactive_ble from your git repository inside your app, in your .yaml file add:
flutter_reactive_ble:
git:
url: your/git/flutter_reactive_ble/repository/url
ref: tag or commit id

gradle withJavadocJar() and withSourcesJar() for android library

Is there a way to leverage the new functions withSourcesJar() and withJavadocJar() for Android library projects? Currently when I try to use it I get:
> SourceSet with name 'main' not found.
With the latest Gradle version 6.0.1, there seems to be no way to use these new methods in Android library projects. Here’s why I believe that’s the case:
The two methods withJavadocJar() and withSourcesJar() on the java extension have the default main source set hardcoded, see here and here.
There are two methods with the same names (withJavadocJar() and withSourcesJar()) which can be used in feature variant declarations. However, it seems that Android Gradle builds don’t use feature variants, i.e., these methods can’t be used either.
The documentation states, that these come from the JavaPluginExtension - but not from Android DSL. So they can only be used in conjunction with apply plugin: "java" or apply plugin: "java-library", but not with apply plugin: "com.android.application", apply plugin: "com.android.library" and alike. The name of these tasks also suggest that it's common Java (*.jar) and not Android (*.aar). On Android, this would only make sense for a *.jar library, which uses pure Java features, but no Android features at all (which is limited in functionality).
In short, apply plugin: "java-library" would permit accessing these.
With Gradle 7.2 I "recreated" withJavadocJar and withSourcesJar using Gradle Kotlin DSL:
val sourceFiles = android.sourceSets.getByName("main").java.getSourceFiles()
tasks.register<Javadoc>("withJavadoc") {
isFailOnError = false
dependsOn(tasks.named("compileDebugSources"), tasks.named("compileReleaseSources"))
// add Android runtime classpath
android.bootClasspath.forEach { classpath += project.fileTree(it) }
// add classpath for all dependencies
android.libraryVariants.forEach { variant ->
variant.javaCompileProvider.get().classpath.files.forEach { file ->
classpath += project.fileTree(file)
}
}
source = sourceFiles
}
tasks.register<Jar>("withJavadocJar") {
archiveClassifier.set("javadoc")
dependsOn(tasks.named("withJavadoc"))
val destination = tasks.named<Javadoc>("withJavadoc").get().destinationDir
from(destination)
}
tasks.register<Jar>("withSourcesJar") {
archiveClassifier.set("sources")
from(sourceFiles)
}
Put this into your gradle.build.kts file and then run ./gradlew withJavadocJar and ./gradlew withSourcesJar (or use the tasks to create artifacts for publishing).
android.sourceSets.getByName("main").java.getSourceFiles() is the Android specific part to retrieve the "main" source files.

is it possible to use kotlin on the JVM or Android without a dependency on kotlin-stdlib?

I have an android library that I distribute as a jar file; Currently it has zero dependencies (other than the things built into Java itself). I like the idea of porting it to kotlin, however I haven't been able to figure out how to do this without forcing a dependency on kotlin-stdlib (or kotlin-stdlib-jre7, etc)
I'm somewhat wary about doing this, because kotlin-stdlib gets updates very frequently (multiple times per month sometimes) and I don't want to cause a future compatibility problem.
Obviously if I use any of the kotlin specific functions or types (e.g. listOf, mapOf, etc) then of course I'd need the stdlib, but if I stick to pure java types and functions, then is this possible? And if so, how?
I am not sure this is what you are looking for but I was able to build and run run this simple program using IntelliJ Idea 2018.3
fun main() {
System.out.println("hello world")
}
and the following build.gradle file
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
apply plugin: 'application'
mainClassName = "MainKt"
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
The only thing I saw in the jar file, other than a META-INF folder was my MainKt.class
I was able to run this on the jvm using
java -classpath simple.jar MainKt
I think if you don't include the standard library you cant even use Kotlin's println function, so you have to resort to java's System.out.println method

Gradle grouping dependencies

I was experimenting with my project's build.gradle. Currently my project consist of several modules, each of these module have common dependencies, such as android support or network library. I'm Experimenting with gradle dependencies.
I've declared a group of dependencies named lib_mandatory() in file lib-group.gradle, but when I tried to include it in my app's build.gradle the gradle sync failed.
Error:Could not find method lib_mandatory() for arguments [] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
How can I fix this? or any hints about what this error means?
Update:
Here's the lib-group.gradle
def dependencyGroup(Closure closure) {
closure.delegate = dependencies
return closure
}
def lib_mandatory = dependencyGroup{
implementation libraries.rxjava
implementation libraries.rxandroid
}
and here's the app's build.gradle
apply from: '../lib-group.gradle'
dependencies {
lib_mandatory()
}
The approach that is used in that article is a bit different, the article used the function inside the lib-group.gradle and only applied (apply from ...) the lib-group.gradle in the app's build.gradle.
In your approach, you're trying to use the function in the app's build.gradle.
If you want your functions to be accessed from other files, you should use ext instead of def. You might want to read this:
Gradle def vs ext

databinding does not exist: How to solve it?

I'm working on an Android application with databinding but I've always next error:
Error: Package my.package.databinding does not exist.
Here is my build.gradle on project level:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I've also enabled binding in the build.gradle file on module level.
Now my question is, why occurs this error and how could I solve it?
This problem occurs usually if your project does not compile. Android databinding should generate code in the named package, but it can't do that if the project doesn't compile in the first place.
To solve this, bring your project to a point where it compiles. If necessary, turn databinding off for this.
check out your xml files and comment any #{} you have used unless you actually have your data ready at hand. With no data, you'll bump into this error again and again and again.
I came across this issue in a project of 4 modules in Android Studio 2.3, it is what #F43nd1r indicated, but want to document what I did to resolve this in my case.
One of the 4 modules had an older Android Support library in in the Gradle file for it, while the other 3 were current. This is what prevented the project from compiling properly and causing the databinding error.
The difficult part was that you don't know about this unless you open each build.gradle file and see if there is an error displayed. It did NOT show an error for it on compile.
Effectively I updated this area to the newer version number to match the other 3 module build.gradle files.
dependencies {
...
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
...
}
To see the error, just edit these lines of code in the app's build.gradle:
dataBinding {
enabled = false
}
In this way, the last error in your build console is the actual error. Because from the first to the penultimate error, they are all related to the non-generation of the data binding classes, precisely because we have disabled it.
Once you find the error you will enter again :
dataBinding {
enabled = true
}
dataBinding {
enabled = true
}
enabled the data binding in app build.gradle file. its worked
Based on similar issues on SO, the reasons may not be related to android data binding, and instead due to incorrectly calling variables as in this issue or some other factors like in this other issue. You should provide more details if none of these links helps.
For me doesn't work anything except one: Renamed XML binding class
What I tried before:
off/on viewBinding
renaming folders
reinstalling modules
renaming modules
After deleting the build folder for the project and submodules, making, rebuilding, etc etc etc, the only thing that worked for me was:
create a new layout
I think there's something in the generator that gets messed up and that flushes it (totally guessing here)
Remember to add
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
...
buildFeatures {
viewBinding true
dataBinding true
}
}

Categories

Resources