Skip to main content

[Android] using Dependency in Gradle Build, Android Studio

- 2013.08.05 -
 Android Studio 0.2.3 에서는 repositories {} 가 기본적으로 포함되어 있다. 따라서 아래와 같은 이슈는 나타나지 않을 것이다.

Android Studio 에서도 Maven dependency 처럼 외부 library 를 다운받아서 Build 할 수 없을까? 하는 생각에 커뮤니티에 질문한 결과 답을 얻을 수 있었다.

우선, Maven Central Repository 에서 사용하고자 하는 Library 를 검색하여 Grails repository 를 알아야 한다.

예를들어 okhttp 의 Grails repo 는 다음과 같다.

compile 'com.squareup.okhttp:okhttp:1.1.1'

이제 본격적으로 Android Studio 의 build.gradle 에 적용 하도록 하자.

기본적으로 build.gradle 는 아래와 같다.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile "com.android.support:support-v4:18.0.+"
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 17
    }
}

여기서, 검색한 Grails Repo 를 dependencies 에 추가해 주면 된다.

dependencies {
    compile "com.android.support:support-v4:18.0.+"
    compile 'com.squareup.okhttp:okhttp:1.1.1'
}

여기서 주의 할 점은, dependencies 선언한 윗 부분에 repositories 를 다시 선언 해서 mavenCentral()을 사용한다는 것을 재 명시 해 주어야 한다는 점이다. 즉 코드로 보면 아래와 같다.

repositories {
        mavenCentral()
}

dependencies {
    compile "com.android.support:support-v4:18.0.+"
    compile 'com.squareup.okhttp:okhttp:1.1.1'
}

결과적으로 전체 코드는 아래와 같다.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {

    compile "com.android.support:support-v4:18.0.+"
    compile 'com.squareup.okhttp:okhttp:1.1.1'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 17
    }
}

이렇게 하면 Maven 에서 dependency 를 사용하는 것 처럼 Gradle 에서 도 사용할 수 있다.

Comments

Popular posts from this blog

[Android Application Testing Guide] Chapter3 jar file

Android Application Testing Guide(에이콘) 3장 Sample 을 실행시키기 위해서는 libdummy-0.0.1.jar 가 필요한데, sample file 에는 프로젝트만 존재한다. 결국 jar 를 만들어야 되는데.. ant 빌드다. 어허허... jar 파일이 없으면 예제 진행이 어려우므로. ant build 한 jar 를 첨부. libdummy-0.0.1.jar download

[번역] A journey on the Android Main Thread - Part 1

본 문서는 square engineering blog 에 기재된 A journey on the Android Main Thread - Part 1 기사를 번역한 것 입니다. coding horrer 에 왜 우리는 소스 읽는 법 을 배워야 하는가 에 대한 기사 가 있습니다. 안드로이드의 가장 큰 특징 중 하나는 오픈소스 생태계 라는 점 입니다. PSVM (public static void main) public class BigBang {   public static void main ( String ... args ) {     // The Java universe starts here.   } } 모든 자바 프로그램은 public static void main() 메소드를 호출하면서 시작합니다. 이는 자바 데스크탑 프로그램, JEE 서블릿 컨테이너, 안드로이드 애플리케이션 이 모두 동일 합니다. 안드로이드 시스템은 부팅 단계에서 ZygoyteInit 이라 불리는 리눅스 프로세스를 실행합니다. 이 프로세스는 달빅VM 으로, 쓰레드에 안드로이드 SDK 의 대부분의 클래스 를 로드 하고 대기합니다. 새로운 안드로이드 애플리케이션을 시작할 때, 안드로이드 시스템은 ZygoteInit 프로세스를 포크 하게 됩니다. 포크된 자식 프로세스의 쓰레드는 대기를 해제하고, ActivityThread.main() 메소드를 호출합니다. 위키피디아 에 정의된 zygote 란 수정란을 의미합니다. Loopers 계속 진행하기 앞서, 우리는 Looper (이하 루퍼) 클래스를 살펴 볼 필요가 있습니다. 루퍼를 사용하는 것은 하나의 쓰레드가 메시지들을 연속해서 실행하도록 하는 좋은 방법 입니다. 각각의 루퍼는 메시지 객체의 큐를 지니고 있습니다. (이를 메시지 큐 MessageQueue 라고 합니다.) 루퍼는...

[Cobertura] cobertura 2.0.2 script fix

cobertura 2.0.2 버전을 다운받고 실행 시키려고 하면 엄청난 에러가 뿜어져 나온다. ㅠ 달리 설정을 틀리게 한 것도 없는데.. 하면서 script 를 열어보니 아래와 같이 작성되어 있다. - cobertura-instrument.sh java -cp `dirname $0`/cobertura.jar:`dirname $0`/lib/asm-3.3.1.jar:`dirname $0`/lib/asm-tree-3.3.1.jar:`dirname $0`/lib/asm-commons-3.3.1.jar:`dirname $0`/lib/log4j-1.2.9.jar:`dirname $0`/lib/jakarta-oro-2.0.8.jar net.sourceforge.cobertura.instrument.Main $* - cobertura-instrument.bat java -cp "%COBERTURA_HOME%cobertura.jar;%COBERTURA_HOME%lib\asm-3.3.1.jar;%COBERTURA_HOME%lib\asm-tree-3.3.1.jar;%COBERTURA_HOME%lib\asm-commons-3.3.1.jar;%COBERTURA_HOME%lib\log4j-1.2.9.jar;%COBERTURA_HOME%lib\jakarta-oro-2.0.8.jar" net.sourceforge.cobertura.instrument.Main %CMD_LINE_ARGS% 그런데 막상 다운받은 folder 구조를 보면.. cobertura-2.0.2.jar /lib     - asm-4.1.jar     - asm-commons-4.1.jar     - sam-tree-4.1.jar     - oro-2.0.8.jar .... script 왜 이렇게 되있지... 결국 cobertura 를 실행 시키기 위해서는 script 에서 버전 및 파일명을 다운받은 폴더...