Using apply(from...) partial files android #1287
Comments
The accessors aren't generated for script files that aren't the project script file. |
Yes, I have the same experience with a multi-module project. I have put some common android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
} Unfortunately, I cannot convert this common file to Kotlin or I get the same |
You can declare the extension manually in your Full disclosure, I'm not an android developer.
From there, you should be able to jump to the declaration of the /**
* Configures the [android][[some long import here].AndroidExtension] project extension.
*/
fun Project.android(configure: [some long import here].AndroidExtension.() -> Unit) =
extensions.configure("checkstyle", configure) You can copy and paste that logic into the bottom of your |
When we put everything from like: All modules use the same code and we don't want to edit on all submodules, so we use one file, which is (shared) applied in all modules. In this files are just part of |
@mtrakal - please check out this documentation. When you are working in an applied script (aka a script plugin), type-safe accessors are not yet available (for standard built-ins and for applied plugins). The way kotlin-dsl makes these available ( So when you are not in a main build script (settings, init, applied) or when you apply a plugin outside of the plugins block (i.e. What @JLLeitschuh is suggesting is a way to manually pull in the code for the accessors you want. After doing this, you will be able to run the configuration code using those accessors (aka, using the the DSL, Note that just because you pull in an accessor for |
One problem I see with this is that when iterating through all |
Also related to #427 |
I've tried what @JLLeitschuh suggest but didn't work
|
@kiratheone A bit more information would be useful. |
Hellow!
It failed on:
If uncomment
because KotlinSourceSet. loaded diferent Classloader. How to fix it? |
This has nothing to do with the issue at hand... |
Why not? I can not use apply(from=) for configure my project. |
@JLLeitschuh my external gradle.kts file
I get an error
|
any update on this? when can we expect a proper fix for the accessors? |
As @JLLeitschuh, @fuzzyweapon mentioned, The issue pointed by @BulatMukhutdinov above is due to applied script plugins being evaluated in an isolated classloader. In other words they don't share the classpath of the plugins applied to the project build script, hence they cannot see If you'd use Kotlin DSL precompiled script plugins in |
Thanks for the answer @eskatos Tried to use precompiled script, but with no luck
Am i doing something wrong? Got the same error Unresolved reference: android, compileVersionSdk, defaultConfig, etc |
What version of Gradle are you using? |
Gradle version 5.3.1 |
@BOMBeR369, I can't see anything obvious in the snippet above, maybe you miss the |
@eskatos it still doesn't work for me either, same unresolved reference errors |
Same for me, unresolved reference errors |
I have the same, can't make it work. Annoying that there's so little documentation about it. |
Same. |
Same |
1 similar comment
Same |
The solution described above works for me. |
@eskatos can you publish a demo with Android? I can't solve this problem too... |
Ok, the same problem here. Is there any demo how to resolve this problem? |
Found the solution that worked for me. The only thing I need to do is to add
|
@personshelldon @eskatos This isn't working in the same scenario.. I changed my build script of buildSrc to the one @personshelldon suggested and tried moving the file with the android config closure into the buildSrc folder.. It still doesn't work.. Can you please give more instructions? A lot of us have trouble converting our groovy gradle scripts to kotlin |
I want to have a common file for com.android.application and com.android.library, so I can't have either plugin in the plugins declaration closure. Things work and build as a plugin if I have any id in the plugins closure while keeping the common gradle.kts file in the buildSrc module though. Is it possible to have kotlin in such a case? |
@Sai-Teja usually the app module is the application, so you can do an if else to check the name, if it is app, you use application, if not, library. |
@JavierSegoviaCordoba That is true. But I wanted to have something generic so that apps with multiple targets such as ones that have the same code base but different app modules for wear OS and store app can use. After spending about two days on this, I feel that it is better to stick with groovy for gradle scripting. It is just more flexible in allowing to split the code among files and better support online. I have 3 files right now. With kotlin scripting, I need to package these into a plugin.. which needs to have the android plugin compulsorily in the file for getting the android object. @eskatos correct me if I am wrong. Apart from these I have some protobuf and grpc gradle files as well.. Groovy just looks simpler as of today. |
@Sai-Teja I have multiple kts files: for Android modules, for Kotlin only modules, etc. You should can get the same behavior with kts. Have you tried to create your own plugin? |
Same issue here. Please add sample project on how to solve this problem. Also could you reopen this issue? It's actually not solved. @eskatos |
My solution: https://stackoverflow.com/a/60482635/5279996 |
ContextWe want to apply specific android block using pre-compiled scripts with Kotlin DSL in order to favor re-usability and modularization. Solution1 - We have to compile the plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}
repositories {
jcenter()
google()
}
dependencies {
implementation("com.android.tools.build:gradle:4.0.0")
} 2 - In our script file, add the plugin package scripts
plugins { id("com.android.application") apply false }
android {
buildTypes {
getByName("debug") {
isMinifyEnabled = false
applicationIdSuffix = ".debug"
isDebuggable = true
}
getByName("release") {
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro")
}
}
flavorDimensions("version")
productFlavors {
create("dev") {
dimension = "version"
applicationIdSuffix = ".dev"
versionNameSuffix = "-dev"
}
create("internal") {
dimension = "version"
applicationIdSuffix = ".internal"
versionNameSuffix = "-internal"
}
create("public") {
dimension = "version"
}
}
} 3 - Apply the pre-compiled script plugin in your android application/module: plugins {
// Application Specific plugins
id(BuildPlugins.androidApplication)
id(BuildPlugins.kotlinAndroid)
id(BuildPlugins.kotlinAndroidExtensions)
// Internal Script plugins
id(ScriptPlugins.variants)
} I hope that helps. |
@android10 Is the first script inside buildSrc? And I am assuming I am still getting a "Unresolved reference: android" inside my "android-common.gradle.kts" |
Main file, which use
apply from
build.gradle.kts
second file which includes single files for apply
include.gradle.kts
and in
exclude.gradle.kts
I have:But it failed on:
When I didn't use KTS file, but
.gradle
and Groovy and proper syntax, it works as expected. All excludes from the file are "included" into main build.gradle.kts intoandroid
and work as expected without any error.Expected Behavior
Work same as when use Groovy
Current Behavior
Failed on:
Unresolved reference: android
Your Environment
The text was updated successfully, but these errors were encountered: