lintOptions not being recognized by Gradle - android

So, first, some information about my setup. I'm trying to build an app, that includes different modules, in Android Studio. I have the following dependencies in my top-level build.gradle file:
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.9.3'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'org.moallemi.gradle.advanced-build-version:gradle-plugin:1.5.3'
classpath 'com.google.gms:google-services:3.0.0'
}
I tried to disable Lint in one of my modules, because I was getting Lint errors that I'll investigate later (for the moment, I just want to get a build going). I used the following setting in one of my modules:
android {
[...]
lintOptions {
abortOnError false
}
[...]
}
However, I'm getting the following error:
Error:(48, 0) No signature of method:
com.android.build.gradle.managed.AndroidConfig.lintOptions() is applicable for argument types:
(build_9pxacuo2f46evz0elkujz25xa$_run_closure1$_closure4$_closure11)
values:[build_9pxacuo2f46evz0elkujz25xa$_run_closure1$_closure4$_closure11#96dc8be]
I already researched the appropriate documentation, and things seem to be right:
https://developer.android.com/studio/build/gradle-tips.html
What could I be doing wrong here? Many thanks for any help on this matter.

Apparently I have to write things this way (it is very strange, but I'm able to builld fine now):
model {
android.lintOptions {
abortOnError false
}
android {
// Rest of the other declarations goes here...
}
}
I don't know why this is working, I only know that it works. Once I have more time (I'm in a time crunch now) I'll come back to this to see if I can investigate this further and find out the reasons for this weird way to specifying the lintOptions...

Gradle experimental plugin wraps android block into model block and the lintOptions method does not accept closure.
For use android block try this code:
model {
android {
[...]
lintOptions.abortOnError false
[...]
}
}
See the documentation here: http://tools.android.com/tech-docs/new-build-system/gradle-experimental

Related

Best way to apply a gradle plugin only to a specific build variant

What is the best way to apply a gradle plugin to only a specific build variant. For example we might have
usStage
usDebug
usProduction
but a plugin has a lot of build overhead for developers (several minutes), and has no need to run on usStage and usProduction. Normally for libraries you would just use something like releaseImplementation, but I am not aware of anything like this existing at the gradle plugin level.
So right now we have somethig like this at the top of our app/build.gradle.kts in Android
plugins {
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
id("com.heapanalytics.android")
id("com.gladed.androidgitversion") version "0.4.14"
id("jacoco-reports")
}
where id("com.heapanalytics.android") is the culprit of slow build times on debug, where it is not even needed.
Is there a way that within our build variants, to only apply this for usProduction?
I have tried something like this:
plugins {
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
id("com.gladed.androidgitversion") version "0.4.14"
id("jacoco-reports")
}
android {
defaultConfig {
...
signingConfigs {
create(release) {
...
}
getByName(debug) {
...
}
}
buildTypes {
getByName(debug) {
...
}
getByName(release) {
...
apply(plugin = "com.heapanalytics.android")
...
}
But it just applies the plugin to every variant still, I believe because that block configures all variants even when you are only building one.
if (project.hasProperty(BuildProperties.Metrics.ENABLE_HEAP)) {
apply(plugin = "com.heapanalytics.android")
}
Tried something like this too, which may work, but was not really sure it is the best way. Feels hacky.
You can do this in many ways but for the most recent solution I know and use do:
android {
productFlavors {
flavourName{
apply plugin: 'com...'
}
}
}
For more solutions please reference to here.

Cannot step into class after updating com.android.tools.build:gradle to 3.3.0 or 3.4.X

I have a simple class (KeyManagementViewModel) that has a reference to another of my classes (UserConfigRepository):
public class KeyManagementViewModel extends ViewModel {
private LiveData<ErrorWrapper<UserConfig>> userConfig;
private UserConfigRepository repo;
public LiveData<ErrorWrapper<UserConfig>> getUserConfig() {
if (userConfig == null) {
repo = new UserConfigRepository(userId); //BREAKPOINT HERE
userConfig = repo.getUserConfigLive(); //CAN'T RESOLVE "repo" here
}
return userConfig;
}
}
In my project build.gradle i have
buildscript {
repositories {
jcenter()
google()
maven {
url 'https://maven.fabric.io/public'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1' //CAN'T BE UPGRADED
classpath 'com.google.gms:google-services:4.2.0'
classpath 'io.fabric.tools:gradle:1.29.0'
}
}
With this configuration, I can put a breakpoint in KeyManagementViewModel.getUserConfig in the line i commented and everything works fine if I step into the repo's getUserConfigLive. However, when I update com.android.tools.build:gradle to 3.3.0 or 3.4.2, now the breakpoint works as before, but if i step into the repo's getUserConfigLive(), i don't see the source code of my class and any breakpoint inside the repo class is never hit. I know the code is being executed because my logs in the repo class still appear in the console. Something else i notice is that if i break in the line after my "BREAKPOINT HERE" comment, it says "repo=No such instance field:'repo'" even though repo it was instantiated in the line before. It's as if the debugger is in a weird state where it doesn't recognize my Repo class or the current state. Here is the stacktrace after stepping into getUserConfigLive(), showing that it doesn't recognize the repo class as my own or the fields as valid. Any idea what causes this or how to fix it? I have the same problem in IntellJ Idea latest and Android Studio latest. I'm using Gradle 5.1.1
The culprit was the new R8 shrinker introduced in the gradle plugin 3.3.0
https://developer.android.com/studio/releases/gradle-plugin#behavior-changes
For some reason, R8 was obfuscating my UserConfigRepository class whereas Proguard was not. This is not a long term solution but disabling minification is a good test. My long term solution was increasing the minSdkVersion to 21 so I get the new implicit multidex support and was allowed to disable minification in debug applications, which is the recommended way anyway

how to get Dagger2 Compiler Options working?

I'm trying to use 3 of the dagger2 compiler options in my android project.
but it seems none of them actually work.
I have pasted the code from here to my gradle.properties and even compiler options of AS settings.
the 3 that I'm interested in are:
-Adagger.fastInit=enabled
-Adagger.formatGeneratedSource=disabled
-Adagger.gradle.incremental
the fastinit and codeformatting just don't work (judging by the code that is generated) but the incremental cause a compile error saying:
no compiler option found.
the versions that I'm using are:
dagger : 2.18
gradle : 5.2.1
kotlin : 1.3.21
androidPlugin : 3.3.1
For projects with multiple modules, the top build.gradle can be updated with this
allprojects {
repositories {
...
}
afterEvaluate {
extensions.findByName('kapt')?.arguments {
arg( "dagger.formatGeneratedSource", "disabled" )
}
}
}
Perhaps you should try without "A"
dagger.fastInit=enabled
dagger.formatGeneratedSource=disabled
dagger.gradle.incremental=enabled
Also can try directly in build.gradle, but this should be done for each project.
kapt {
arguments {
arg('dagger.fastInit', 'enabled')
arg('dagger.formatGeneratedSource', 'disabled')
arg('dagger.gradle.incremental', 'enabled')
}
}

Google Play game services - Android samples errors

Based on Android docs (https://developers.google.com/games/services/android/quickstart#before_you_begin) for learning how to configure Google Play game APIs onto a sample app such as TypeANumber, I was wondering why I get the following resource errors from the BaseGameUtils library after importing the project, BasicSamples, from their GitHub's (https://github.com/playgameservices/android-basic-samples) source files:
... Based on the directory in the left panel, did I import it properly? All I did was import it straight from the directory: android-basic-samples/BasicSamples/build.gradle as stated in Step 1 of the link.
Here's my (untouched) Gradle file for the library, BaseGameUtils:
apply plugin: 'com.android.library'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
}
}
dependencies {
// Set defaults so that BaseGameUtils can be used outside of BasicSamples
if (!project.hasProperty('appcompat_library_version')) {
ext.appcompat_library_version = '20.0.+'
}
if (!project.hasProperty('support_library_version')) {
ext.support_library_version = '20.0.+'
}
if (!project.hasProperty('gms_library_version')) {
ext.gms_library_version = '8.1.0'
}
compile "com.android.support:appcompat-v7:${appcompat_library_version}"
compile "com.android.support:support-v4:${support_library_version}"
compile "com.google.android.gms:play-services-games:${gms_library_version}"
compile "com.google.android.gms:play-services-plus:${gms_library_version}"
}
android {
// Set defaults so that BaseGameUtils can be used outside of BasicSamples
if (!project.hasProperty('android_compile_version')) {
ext.android_compile_version = 23
}
if (!project.hasProperty('android_version')) {
ext.android_version = '23'
}
compileSdkVersion android_compile_version
buildToolsVersion android_version
}
... Did anyone else experience this issue before?
EDIT AS OF 4/7, 3:37PM:
So I ended up importing the project again, but within the AndroidStudioProjects directory this time (does that really make a difference?) and I actually ended up making some progress since then. However, for part 3 under step 1 within the docs:
... I ended up changing not only the package name in TypeANumber's manifest file, but also the package directory along with the classes in it as follows:
... So my question now is, am I on the right track so far in terms of the navigation directory panel at the left? :)
Yes. The package attribute in the manifest should always match the directories. Plain and simple. That's why changing it would require you to do a complete refactor. Just keep on going with the tutorial accordingly and I think you'll be good.

How to sign APK on Android Studio even with non-translated strings?

Background
I've recently migrated my app to Android-Studio. I had some issues doing so, but I got over them eventually.
The problem
For some reason, on Android Studio, when I try to sign an APK, I get a lot of errors that look like this:
Error:(16) Error: "..." is not translated in "de" (German), "el" (Greek), "iw" (Hebrew) [MissingTranslation]
(where "..." is a string)
At the bottom, after a lot of errors of this kind, I see this:
Error:Execution failed for task ':app:lintVitalRelease'.
> Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
...
The question
I'm not sure what's wrong and how I can fix it. On Eclipse I did it very easily. Missing translations shouldn't stop me from signing an APK...
To me it seems as if Lint is preventing the exporting of the APK, and that the reason is that I didn't translate all of the strings. Is that true?
Can anyone please help me? How can I fix this, so that Lint will show me just warnings instead? or a confirmation dialog if I'm sure I want to do it?
The cleanest way to solve the problem is to disable Lint checks of missing translations for release builds only.
To do so add "disable 'MissingTranslation'" to your build.gradle file as shown below:
android {
buildTypes {
release {
lintOptions {
disable 'MissingTranslation'
}
}
}
}
To me it seems as if Lint is preventing the exporting of the APK, and
that the reason is that I didn't translate all of the strings. Is that
true?
Yes. Default option is lintOptions.abortOnError = true
Can anyone please help me?
You should open the build.gradle file located at the main project module, or the generic folder if you do not have a module. Then add the suggested lines:
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
Some Lint warnings are by default turned to studio as errors, I don't actually know why, but in terms of translations I guess that is a way to "stop" you publishing an app that the translation is incomplete due to a last minute additions of some texts.
With the lintOptions checkReleaseBuilds abortOnError you set the checking of Lint not to run for release versions and also not stopping if an "error" is found. Below I explain where the Lint errors settings can be found, so if you want to go further you can go one step forward and read them one by one. Some of them provide helpful instructions for code optimizations.
How can I fix this, so that Lint will show me just warnings instead?
or a confirmation dialog if I'm sure I want to do it?
There is also an option at the Android Studio settings to change any Lint error to Lint warning, but I never test that. I usually turn to the gradle solution.
The option is located at Settings > Inspections > Android Lint. For easy find open Settings and at the search (located at the top) type Lint translation there you can change the translation options appear at the left from errors to warnings.
An other option if your error strings never going to be translated is to add at your XML string files tools:ignore="MissingTranslation" either at the root item or at each non-translatable string.
Simple way to solve this Error
Just add following Code To do add "disable 'MissingTranslation'" to your build.gradle file as shown below:
...
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
...
OR You can also Add this:
android {
buildTypes {
release {
lintOptions {
disable 'MissingTranslation'
}
}
}
}
You could try to open "Translations Editor" and set the string "..." as "Unstranlatable".
You also must remove all translations of this string.
FWIW: If you don't plan on supporting other languages, then you don't need to disable the lint checks at all. Sometimes your project setup (or a library you're importing) may have accidentally - or intentionally - included a config to support additional languages by declaring a values- folder for that language like this for instance:
<your project source folder>/main/res/values-ar
This was the case for me so I simply removed the folder. But if you have no control over the offending library then one choice is to disable lint abortOnError as indicated in the accepted answer, or find a way to exclude 'library-imported' folders somehow. For the latter option you can start here
there is many solution but i tried
<string name="hello" translatable="false">hello</string>
It's the ignore attribute of the tools namespace in your strings file, as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" >
<!-- your strings here; no need now for the translatable attribute -->
</resources>
and from the Gradle
release {
lintOptions {
disable 'MissingTranslation'
}
}
and
android {
lintOptions {
disable 'MissingTranslation'
}
}
Working
buildTypes {
release {
lintOptions {
checkReleaseBuilds false
abortOnError false
}
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

Categories

Resources