When I paste some Java code into a Kotlin project, Android Studio can convert the syntax. However, when the converter is invoked, it adds several unused imports to the file, which broke my existing code.
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.R
The pasted code snippet doesn't need these imports. Since I already imported my.application.R but AS adds android.R, references to R. will broke.
Why are these added? Is this a bug in the converter? Can I somehow disable it?
Related
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.
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>
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?
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.
I downloaded an Android Project and got some errors.
How can I use this imports in Android?
import com.android.internal.telephony.IccSmsInterfaceManager;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import com.android.internal.telephony.SMSDispatcher;
When I import this, I got errors like these:
The import com.android.internal.telephony cannot be resolved
The import com.android.internal.telephony cannot be resolved
The import com.android.internal.telephony cannot be resolved
The import com.android.internal.telephony cannot be resolved
Later in the code there are:
Phone phone = PhoneFactory.getDefaultPhone();
With the error:
Multiple markers at this line
- Phone cannot be resolved to a type
- PhoneFactory cannot be resolved
And so on...
I tried to fix it with the https://stackoverflow.com/a/16377968/3203914
but I did not work for me.
Can please someone tell me how to fix it?
Thanks!
How can I use this imports in Android?
Those are not part of the Android SDK. If you are editing the Android source code, the build process for it should resolve those imports. If you are trying to write an Android app, you cannot directly reference them. In fact, com.android suggests they are part of a separate app, not yours and not the Android framework.
CommonsWare is correct, its not in the SDK so you cannot directly reference them. You will either have to
Import all of the android source code, which will resolve these issues
Import the specific project containing this code, then set up your code as a dependency
Stop using those packages