问题
I created a new Android project, targeting API 15 and using androidx
. The autogenerated Hello world
is:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Adding import androidx.webkit.WebViewAssetLoader
to it fails with Cannot resolve symbol webkit
.
So I tried to create another project, this time targeting API level 27, with the same result, and I concluded that the API I needed was 28 (and gave up because I do not want to target something so restricting). Someone commenting on another question suggested that is not the case, hence this question, which is twofold.
Where can one find which API level a particular class targets? The documentation on WebViewAssetLoader does not say.
Am I missing something in my project, e.g. additional JARs which are optional?
回答1:
and I concluded that the API I needed was 28
That's not really what's going on here.
You are trying to import androidx.webkit.WebViewAssetLoader
. The first segment of that package is androidx
. Every androidx
class comes from a library. You simply don't have that library, apparently.
In an ideal world, you would be able to visit the official JavaDocs for that class and be told which Jetpack library contains it. This is not an ideal world.
So, that's why I go through the trouble of maintaining AndroidX Tech, to fill in some of these documentation gaps. If you choose "W*" in the "Classes" drop-down, you will find WebViewAssetLoader is part of the androidx.webkit:webkit library, version 1.1.0
or higher.
Right now (2020-03-17), 1.2.0
is the most recent version, so add:
implementation "androidx.webkit:webkit:1.2.0"
to your dependencies
in your module's build.gradle
, and you should be good to go.
来源:https://stackoverflow.com/questions/60722739/how-to-find-which-api-level-an-android-class-has-been-introduced