Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Android Studio에서 JAR 파일을 라이브러리로 추가하는 방법

개요

이 예제는 Android Studio에서 JAR 파일을 라이브러리로 추가하는 방법을 단계별로 설명합니다. 외부 라이브러리를 프로젝트에 통합해야 할 때 유용하게 활용할 수 있습니다.

1단계 — 새 프로젝트 생성

Android Studio에서 새 프로젝트를 만듭니다. 상단 메뉴에서 File → New Project로 이동한 후, 프로젝트 생성에 필요한 모든 세부 정보를 입력합니다.

2단계 — JAR 파일 추가

사용하려는 JAR 파일을 프로젝트의 app/libs/ 폴더에 복사하여 넣습니다.

Android Studio에서 JAR 파일을 라이브러리로 추가하는 방법

3단계 — 의존성(Dependencies) 설정

File → Project Structure 메뉴를 열고 해당 JAR 라이브러리에 대한 의존성을 추가합니다.

Android Studio에서 JAR 파일을 라이브러리로 추가하는 방법

Android Studio에서 JAR 파일을 라이브러리로 추가하는 방법

참고로, app 수준의 build.gradle 파일에 아래와 같이 직접 의존성을 선언하는 방법도 있습니다.

dependencies {
    implementation files('libs/your-library.jar')
}

4단계 — 레이아웃 XML 작성

res/layout/activity_main.xml에 아래 코드를 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

5단계 — MainActivity.java 작성

src/MainActivity.java에 아래 코드를 추가합니다.

package com.app.sample;
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);
    }
}

6단계 — AndroidManifest.xml 작성

Manifests/AndroidManifest.xml에 아래 코드를 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

앱 실행하기

이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘을 클릭합니다. 기기 선택 목록에서 자신의 모바일 기기를 고르면, 연결된 기기 화면에 아래와 같은 기본 화면이 표시됩니다.

Android Studio에서 JAR 파일을 라이브러리로 추가하는 방법