Auto import namespace in IntelliJ - android

In IntelliJ you can press Ctrl-Alt-O to import namespaces for classes that are being referenced. I did this and it did import some classes but not others. I'm actually using Android Studio which is based on IntelliJ. The class I'm trying to import is IBinder but when I hover over the class name it indicates:
Cannot resolve symbol 'IBinder'
Since this is based on Gradle, am I suppose to manually include something in the build.gradle? Or does Android Studio automatically add the dependency for the classes I want to use? IBinder is a rather standard interface, so I can hardly believe I need to add something special.

You mean android.os.IBinder?
A simple import android.os.IBinder should work, shouldn't it?
BTW, on my Android Studio (default) installation, Ctrl-Alt-O organizes imports, but does not import them.

Related

Mutiple dependency declarations to missing classes in 'SQLiteDatabase' class

I imported the 'SQLiteDatabase' class present in 'android.database.sqlite' package, into my project.I went into the class declaration to find the opendatabase function, but i noticed that the class was full of errors and was importing certain undefined/undeclared classes, like:
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.database.sqlite.SQLiteDebug.DbStats;
import android.os.SystemProperties;
These classes are used throughout the 'SQLiteDatabase' class.
I tried to invalidate caches and restart the AS IDE, but the error still persisted.
I went into the sdk manager and downloaded all the required core android packages, but still nothing changed.
Finally, I checked out the documentation to see if something was wrong with my sdk. The documentation too did'nt recognize these import calls. like, android.annotation package does'nt even declare a IntDef or IntRangeclass, which has been imported in SQLiteDatabase. It gives the same, could resolve symbol error.
After doing research for a while, I found an answer to my own question. I'm answering just in case it might help others stuck in this error/problem. The classes that were imported are hidden classes, tagged with annotation #hide.
Refer to this article for more.
Hidden classes (even if they are public in the Java sense) are removed from android.jar file containing specific platform API dependencies, like API level 27. Hence you get the error when you try to import something like Nonnull, service manager, etc . Hidden classes are those that Google does not want to be part of the documented public API.

How to customize Android Studio's import ordering in Kotlin to ignore whether they are "static" imports?

Using the "Optimize Imports" in Android Studio 3.4.1, the imports are ordered similar to this:
import com.walla.walla
import com.willy.willy
import org.koin.android.ext.android.get
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.concurrent.thread // <-- note this line
import kotlin.random.Random
As you can see above, function (a.k.a. "static" import in Java) imports like kotlin.concurrent.thread and kotlin.random.Random are put under other imports.
It is not consistent with the Android Kotlin style guide:
Import statements for classes, functions, and properties are grouped together in a single list and ASCII sorted.
I couldn't find a way to make it such that Android Studio order imports irrespective of whether the import is a class or a function. Is there an option to make it so?
This seems to be a misunderstanding. In fact, kotlin.concurrent.thread is a function. Therefore, it should be grouped together with the other classes.
UPDATE: I do see that the latest version of IntelliJ 2019.1 (and Android Studio) might not be able to conform to the Android Kotlin style guide. If you have these import statements, then IntelliJ does not sort strictly by ASCII:
import org.apache.commons.lang3.StringUtils
import java.util.Base64
import kotlin.concurrent.thread
Instead, IntelliJ orders them as:
Third-party
Java
Kotlin
I do not see a way to configure IntelliJ or Android Studio to sort them this way:
import java.util.Base64
import kotlin.concurrent.thread
import org.apache.commons.lang3.StringUtils
Perhaps you should submit some feedback to IntelliJ or the authors of the Android Kotlin style guide.

Android x databinding

I migrated my project to androidx and now I'm receiving errors for every databinding generated class because all these generated classes still use import android.support.v7 instead of android x. So far I tried to delete all generated classes and rebuild project, but it generates same ones after building project. Does anyone know how to solve it?
Even though you have migrated to AndroidX, as you said your classes are still using the old imports. Android Studio doesn't seem to refactor it well enough for some reason. This happened to me too and I had to manually delete the invalid imports and add the androidx imports. (alt + enter) to add the correct imports after deleting the invalid imports.
Edit: you'll need to change the class imports as well as the full qualified names of the widgets in the xml layout, menu, etc. files.
Change import android.support.v4.app.Fragment;
To
import androidx.fragment.app.Fragment;
Change <android.support.design.widget.CoordinatorLayout>
To <androidx.constraintlayout.widget.ConstraintLayout>

android.media package missing few classes in sdk-platforms[android.jar]

I am trying to build a FM Radio kind of application and I cloned this Google Respository for the reference.
However There are few classes in the imports which are missing in android sdk. I tried changing compileSdkVersion from 18 to 24 but android.jar seems to be missing these Following imports:
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.AudioManager.OnAudioPortUpdateListener;
import android.media.AudioMixPort;
import android.media.AudioPatch;
import android.media.AudioPort;
import android.media.AudioPortConfig;
import android.media.AudioDevicePort;
import android.media.AudioDevicePortConfig;
Where exactly are these classes and how can i import them ?
The Strange thing about them is they are present as .java files in sources/android-xx but not in actual .jar file.
The sample is showing errors and I am not able to proceed. Please guide me how to resolve this issue.
You can see from the Android source code that these classes are "hidden" from the jar using the "pseudo-annotation":
/* #hide */
For example, see here and here.
This annotation controls what will appear in the runtime Android.jar.
Note, sometimes these hidden classes are later included as part of the official SDK.
Refer to:
https://commonsware.com/blog/2018/01/18/think-hard-about-hide.html
What does #hide mean in the Android source code?
What exactly does Android's #hide annotation do?

Cannot import com.android.internal.app in Eclipse

I cannot import com.android.internal.app in Eclipse. I created Android Project, but those apps are invisible. Should I change something in projet properties?
I want to import:
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
Thanks for any suggestions!
com.android.internal.* classes are internal classes of the Android Framework that are not intended to be public and thus they are not visible in the Android SDK. Those classes are subject to change from the Android Team without notice to developer. It is not recommend to use them for developing application as it is very likely that you app will be broken in the next Android release.

Categories

Resources