Hide Gradle pre-compiled plugins generated sources in Android Studio - android-studio

I have a gradle pre-compiled script plugin inside the build-logic project, which I use as an included build on android-project. The Android project tree view displays also the generated sources for some-plugin which I want to hide.
With this default setup, AS can resolve a reference for the extension detekt because is a generated accessor.
I can hide the generated sources by applying the idea plugin to some-plugin and excluding the generated-sources directory as follows:
// build-logic/settings.gradle.kts
gradle.beforeProject {
pluginManager.apply("idea")
configure<org.gradle.plugins.ide.idea.model.IdeaModel> {
module {
// Exclude generated sources from AS project tree
excludeDirs = setOf(
file("${buildDir}/generated-sources/kotlin-dsl-plugins/kotlin"),
file("${buildDir}/generated-sources/kotlin-dsl-accessors/kotlin"),
file("${buildDir}/generated-sources/kotlin-dsl-external-plugin-spec-builders/kotlin")
)
}
}
}
Which effectively now take away the generated sources from the Android view:
But as you can see, the detekt extension is not longer resolved by AS. Build compiles successfully.
How can I hide the generated-sources files but still allowing AS to resolve references to the hidden files?

You can create your custom files scope and even associate with a color where you can exclude the needed directories and set it for the Project tool window.

Related

What is the use of com.diffplug.spotless plugin in Android Studio?

I am working with Jetpack Compose and I came across "com.diffplug.spotless" plugin in the app's build.gradle file of many examples, but I am not sure if I need it in my project. Can anyone explain the purpose of using it in the Jetpack compose projects?
''' apply plugin: "com.diffplug.spotless" '''
Spotless: Keep your code spotless with Gradle
While working, Many a times you will get formatting issues at the stage of every commit like removing empty lines, cutting white spaces correct, indentation and other minor formatting mistakes.
Using tool/plugin called “Spotless” will reduce time in addressing code review comments.
Spotless provides support for a variety of languages.
Spotless consists of a list of format and each format has:-
1.a target (the files to format), which you set with target.
2.a list of FormatterStep, which are just String -> String functions, such as replace, replaceRegex, trimTrailingWhitespace, custom, prettier, eclipseWtp, licenseHeader etc.
To start integration with Gradle:-
1.Add the following dependency to your build.gradle file
classpath(“com.diffplug.spotless:spotless-plugin-gradle:$spotlessVersion”)
2.Apply the following plugin
apply plugin: ‘com.diffplug.gradle.spotless’
3.Applying spotless to your gradle file in Android Java source
spotless {
java {
// ...
target '**/*.java'
// ...
}
}
Note:- Be sure to add target '**/*.java' otherwise spotless will not detect Java code
inside Android modules.
For more detail you can refer this link : 1

How to import 3rd party binary library in Android Studio 3.4?

Backgroud:
I would like to use a third-party library in Android Studio 3.4. The library includes three files:see pics
arm64-v8a/libAnalyticsLib.so
armeabi-v7a/libAnalyticsLib.so
StrideAnalyticsLib.jar.
The class files within "StrideAnalyticsLib.jar" show that they seem to be generated by using SWIG.
I've tried two ways to import this library but still cannot
import StrideAnalyticsLib.*;
But this doesn't allow to access the classes and shows "cannot
resolve symbol ...".
the .so files are with jniLibs
~/main/jniLibs/
~/main/jniLibs/arm64-v8a/libAnalyticsLib.so
~/main/jniLibs/armeabi/libAnalyticsLib.so
the .so files are within libs; At the same time, i added "sourceSets { main{ jniLibs.srcDirs = ['libs']}}" in build.gradle;
~/libs/
~/libs/arm64-v8a/libAnalyticsLib.so
~/libs/armeabi/libAnalyticsLib.so
Both attempts are followed by cleaning and rebuilding the project.I'm very new to Android and couldn't get it work. Could anyone please provide help? Great Thanks!
After I studied the process using SWIG to wrap c/c++ library into the .jar and .so., I realised that the .jar and .so will have same package name so that the .jar can call the .so file.
Because my .jar doesn't work when it's saved in /libs, I instead copied all classes within the .jar and pasted them into the /main folder. Remember to put them outside your package, otherwise the package name of these classes will be changed, which cause my problem. The way of importing .so is correct. Just put the .so files inside /main/jniLibs.

Turn on compiler optimization for Android Studio debug build via Cmake

I am using Android Studio 3.0 for my NDK based app.
For the C++ code, I use CMake as the external builder.
This works well, I can create debug and release binaries.
However, I would like to turn on compiler optimizations (say -O3) for a part of the C++ code (the physics engine), not just for the release build, but also for the debug build.
So create the bulk of the debug build as is, without optimizing, yet, I want one of the static library targets to be built with the compiler optimization enabled.
How can I go about this?
I have a CMakeLists for a static library target that gets included using add_subdirectory() directive in the top level CMakeLists file.
Note that I point to the top level CMakeLists in my app's build.gradle file like this:
externalNativeBuild {
cmake {
path '../../Android/jni/CMakeLists.txt'
}
}
It turns out that you can use the target_compile_options() macro in your CMakeLists.txt with a config specification like this:
target_compile_options(opende PRIVATE
"$<$<CONFIG:RELEASE>:-O3>"
"$<$<CONFIG:DEBUG>:-O3>"
)
This macro adds to the existing compile options.

How to customize destination path of each dependency

I'm trying to use Gradle to manage dependencies in non-Java projects. The idea is to have a single, generic plugin that along with a project's gradle.build file will bring into the project any dependencies the project needs, placing each dependency where the project expects the files to reside. Currently, it is working by placing them all in a /libs/ folder in the project, but that is not enough. What I'd like to be able to do is to specify in the gradle.build file where to put the dependency in the project.
Here is a simple example: I have a project that has been used for years as a component in other projects. It is a real pain to update all projects when that core component code has been updated... each project repository has to have the new files committed (using SVN, specifically). The files must reside in a particular directory so the ColdFusion framework (FW/1) correctly interacts with the code.
So what has been done is that core component is now in Artifactory and the gradle.build file pulls it down into the projects. That would be the end of the story if it was the only dependency, but there are others that need to be pulled down and the code expects those to be in a different directory than that one component. Each project will have different dependencies, and potentially different file structures (for example, our older apps are using the Fusebox framework). So the ability to control where a dependency ends up as specified in the gradle.build file is what I'm after.
This is what I was hoping to be able to do:
dependencies {
// exploded is a configuration that is added to this plugin
exploded('com.foo:bar:2.0-SNAPSHOT#zip') {
ext {
moveWhat = ['app']
moveWhere = 'assets'
}
}
exploded('com.foo2:bar2:1.0-SNAPSHOT#zip') {
ext {
moveWhat = ['*']
moveWhere = 'lib'
}
}
...
The hope was that I could pass directories/files into moveWhat that would then get placed into the directory specified by moveWhere, but I'm having trouble figuring out how to associate properties with each dependency. I'm having trouble figuring out if this is even possible.
Can anyone point me in the right direction?
I would suggest you use a separate task to extract what you need from the configuration. Something like this:
task copyFromExploded(type: Copy) {
into new File(project.buildDir, "assets")
from (configurations.exploded)
include "**/app"
}

Android Studio - Include ResourceBundles in Module

I currently switched from eclipse to android studio. In eclipse I had 2 projects, one android application project and one java project which I included in the android project as library. This java project uses ResourceBundles to create internationalized error messages for it's own errors. This has been my project structure:
/MyApp
/src
/res
...
/MyLibrary
/src
/res (added as source folder to build path)
/loc
Bundle_en.properties
This worked when loading the RessourceBundles as following:
ResourceBundle.getBundle("loc.Bundle", Locale.ENGLISH);
Now I switched to android studio and my new project structure looks like this (added the java library as module):
/MyProject
/MyApp
...
/MyLibrary
/src
/main
/java
...
/res
/loc
Bundle_en.properties
But I'm not able to load the ResourceBundles anymore, it's just throwing a java.util.MissingResourceException. I tried a lot of different locations for the ResourceBundles and different paths but I'm going to get crazy because nothing seems to work. Could anybody explain where to put those bundles and how to load them?
Thank you!
Faced exactly the same problem. To make it work I finally had to create a resorces folder in my project module's main folder.
here multiple files starting with the same name (as messages in this picture) gets bundled as a resource bundle.
Finally had to call it using
ResourceBundle.getBundle("org.eclipse.paho.client.mqttv3.internal.nls.logcat")
or
ResourceBundle.getBundle("org.eclipse.paho.client.mqttv3.internal.nls.messages")
to get the required resource.
If you include the second project as a library, you might not want to create a new resource folder as suggested in a previous answer (which does work). Instead, you can simply add the library's resource folder to your resource directories in your module's build.gradle: to the android section add
sourceSets {
main.resources.srcDirs += 'path/to/your/libs/res'
}
If now the added res folder contains org/mypackage/Bundle.properties you can refer to it using
ResourceBundle.getBundle("org.mypackage.Bundle")
Actually adding a new resource folder does nothing more then adding it as a resource directory in build.gradle.
I never tried but Intellij comes with very good integration of Resource Bundles.
Refer this link
http://www.jetbrains.com/idea/webhelp/resource-bundle.html
From the link above
Resource bundle is a set of properties files that have same base name
with different language-specific suffixes. A resource bundle contains
at least two properties files with similar base name, for example
file_en.properties and file_de.properties.
IntelliJ IDEA recognizes properties files, and if two or more
properties files with the names that differ only in suffix, are
encountered, joins them into a resource bundle. The new node Resource
Bundle '(base name)' appears in the Project Tool Window:
You can have these files inside your module or on root as well.
First please ensure your resource folder (where the property file is localted) is in the classpath and you can easily find that by calling the following.
URLClassLoader ldr = (URLClassLoader)ClassLoader.getSystemClassLoader();
URL[] urls = ldr.getURLs();
for(URL url : urls)
{
System.out.println(url.getPath());
}
Now if you find your resources folder in the classpath then you can simply call the bundle base name, in your case ResourceBundle.getBundle("Bundle"), no need for a fully qualified path. Assuming you are using English locale, it should find it. You can further add en_US, en_NZ, en_GB etc if needed.
If you do not find your property folder then make sure it is in the classpath and if you need to add it dynamically follow this thread.
How do you change the CLASSPATH within Java?
Remember the only addition for loading property files dynamically is that you MUST call findResource or findResources API on the class loader to load the property file. Hope this helps.

Resources