如何在 Android Studio 中正确加载库

How do I load libries properly in Android Studio

我是 Android Studio 的新手,但不是编程新手。我完成了 AndroidStudio 的教程并制作了我的第一个应用程序。现在我想添加一个图表。
我已经从 Maven 下载了 GraphView-4.2.2。 我把它复制到 app/libs select graphview-4.2.2-javadoc.jar 右键单击​​ "add as library" 然后我弹出 "Studio cannot determine what kind of files..." 并提供 7 个列表。我都试过了。
我想我应该在 graphview 下面得到一个 com.jjoe64.graphview 文件夹和一个 Meta-Inf 文件夹,但我只得到了 meta-inf。 我无法访问任何 GraphView 函数

我在 Win10 64 位 PC 上使用 Android Studio 3.5

无需将整个库包含在您的项目中,只需将其添加到您的依赖项中即可。在 build.gradle 文件中添加以下内容:implementation 'com.jjoe64:graphview:4.2.2'

看看 official documentation on dependencies 摘录:

Remote binary dependency implementation 'com.example.android:app-magic:12.3'

This is actually shorthand for the following:

implementation group: 'com.example.android', name: 'app-magic', version: '12.3'

This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.

Note: Remote dependencies like this require that you declare the appropriate remote repositories where Gradle should look for the library. If the library does not already exist locally, Gradle pulls it from the remote site when the build requires it (such as when you click Sync Project with Gradle Files or when you run a build).

编辑(评论后):

我自己创建了一个样例工程,并添加了上面的依赖。我继续创建标准布局并在我的 MainActivity 中添加了以下代码,它工作得很好:

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.jjoe64.graphview.GraphView
        android:id="@+id/graph"
        android:layout_width="match_parent"
        android:layout_height="200dip"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GraphView graph = (GraphView) findViewById(R.id.graph);
        LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(1, 5),
                new DataPoint(2, 3),
                new DataPoint(3, 2),
                new DataPoint(4, 6)
        });
        graph.addSeries(series);
    }

}

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.content"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-beta01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.jjoe64:graphview:4.2.2'
}

不用担心其他依赖项,它们默认出现在我的 Android Studio 版本中,它们不应该在那个库中产生影响。