Android Studio 및 gradle을 사용하여 Android 라이브러리를 작성하는 방법은 무엇입니까?
Eclipse에서 프로젝트를 마이그레이션하려고하는데 시도한 것이 아무것도 없습니다. 이클립스에는 3 개의 프로젝트 (2 개의 안드로이드 앱 프로젝트 및 1 개의 안드로이드 라이브러리 프로젝트)가 있습니다. 2 개의 앱 프로젝트는 라이브러리 프로젝트에 따라 다릅니다. gradle 내보내기를 수행하면 작동하지 않는 3 개의 프로젝트가 생성됩니다. 프로젝트를 재구성 할 수는 있지만 어떻게 해야하는지에 대한 문서를 찾지 못했습니다.
이클립스 익스포트에서 3 개의 프로젝트를 함께 작동시키는 방법이 있습니까? 내가 구조 조정을하는 것이 더 낫습니까? 그렇다면 어떻게 해야하는지에 대한 문서가 있습니까?
최신 정보
전체 프로젝트를 GitHub https://github.com/respectTheCode/android-studio-library-example에 업로드했습니다.
업데이트 1
Padma Kumar의 제안을 바탕으로 이것이 내가 시도한 것입니다.
- 라는 새 프로젝트를 만듭니다
MyApp
- 을 클릭
File > New Module
하고 선택Android Library
하고 이름을 지정하십시오.MyLib
- 딸깍 하는 소리
Build > Make Project
이 오류로 빌드가 실패합니다
Module "MyLib" was fully rebuilt due to project configuration/dependencies changes
Compilation completed with 1 error and 0 warnings in 19 sec
1 error
0 warnings
/.../MyApp/MyLib/build/bundles/debug/AndroidManifest.xml
Gradle: <manifest> does not have package attribute.
그런 다음 package
매니페스트에 속성을 추가하여 만들었습니다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mylib" >
빌드 후이 오류가 발생합니다
Module "MyApp" was fully rebuilt due to project configuration/dependencies changes
Compilation completed with 2 errors and 0 warnings in 13 sec
2 errors
0 warnings
/.../MyApp/MyLib/src/main/java/com/example/mylib/MainActivity.java
Gradle: package R does not exist
Gradle: package R does not exist
종속성을 추가해도 오류에 영향을 미치지 않는 것 같습니다. 위에서 계속
- 딸깍 하는 소리
File > Project Structure > Modules > MyApp-MyApp
Dependencies
탭으로 전환- 클릭
+ > Module Dependency
하여 선택MyLib
- 클릭
Apply
하고OK
- 딸깍 하는 소리
Build > Make Project
업데이트 2
이단의 제안에 따르면 이것은 우리가 얻는 곳입니다.
2 하위 프로젝트 build.gradle
에는 올바른 부분이 모두있는 것 같으며 플러그인 라인은 다음과 같습니다 MyApp/build.gradle
.
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
루트 프로젝트 build.gradle
가 비어 있으므로 다음과 같이 두 개의 프로젝트를 추가했습니다.
dependencies {
compile project(":MyLib")
compile project(":MyApp")
}
빌드 할 때이 오류가 발생합니다.
Gradle:
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/kevin/GitHub/AppPress/MyApp/build.gradle' line: 2
* What went wrong:
A problem occurred evaluating root project 'MyApp'.
> Could not find method compile() for arguments [project ':MyLib'] on root project 'MyApp'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
업데이트 3
이 문제를 해결해 준 Ethan에게 감사합니다.
- 추가
compile project(':SubProjects:MyLib')
받는 사람MyLib/build.gradle
- 제거
compile files('libs/android-support-v4.jar')
으로부터MyLib/build.gradle
- 프로젝트를 닫고 gradle에서 루트 프로젝트 가져 오기
업데이트 4
0.1.2부터는 compile "com.android.support:support-v4:13.0.0"
대신에 포함 할 수 있습니다 compile files('libs/android-support-v4.jar')
. mavin에서 나왔으므로 이제 여러 프로젝트에 문제없이 포함시킬 수 있습니다.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile "com.android.support:support-v4:13.0.0"
compile project(':SubProjects:MyLib')
}
업데이트 5
0.1.3부터 툴바에 "프로젝트 동기화"버튼이 있습니다. .gradle
파일 을 변경 한 후 프로젝트를 다시 가져 오는 대신 클릭 할 수 있습니다 .
참고 :이 답변은 순수한 Gradle 답변입니다 .IntelliJ에서 정기적으로 사용하지만 Android Studio와의 통합 방법을 모르겠습니다. 나는 나에게 무슨 일이 일어나고 있는지 아는 신자이므로 Gradle과 Android를 사용하는 방법입니다.
TL; DR 풀 예 - https://github.com/ethankhall/driving-time-tracker/
Disclaimer: This is a project I am/was working on.
Gradle has a defined structure ( that you can change, link at the bottom tells you how ) that is very similar to Maven if you have ever used it.
Project Root
+-- src
| +-- main (your project)
| | +-- java (where your java code goes)
| | +-- res (where your res go)
| | +-- assets (where your assets go)
| | \-- AndroidManifest.xml
| \-- instrumentTest (test project)
| \-- java (where your java code goes)
+-- build.gradle
\-- settings.gradle
If you only have the one project, the settings.gradle file isn't needed. However you want to add more projects, so we need it.
Now let's take a peek at that build.gradle file. You are going to need this in it (to add the android tools)
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
}
}
Now we need to tell Gradle about some of the Android parts. It's pretty simple. A basic one (that works in most of my cases) looks like the following. I have a comment in this block, it will allow me to specify the version name and code when generating the APK.
build.gradle
apply plugin: "android"
android {
compileSdkVersion 17
/*
defaultConfig {
versionCode = 1
versionName = "0.0.0"
}
*/
}
Something we are going to want to add, to help out anyone that hasn't seen the light of Gradle yet, a way for them to use the project without installing it.
build.gradle
task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.4'
}
So now we have one project to build. Now we are going to add the others. I put them in a directory, maybe call it deps, or subProjects. It doesn't really matter, but you will need to know where you put it. To tell Gradle where the projects are you are going to need to add them to the settings.gradle.
Directory Structure:
Project Root
+-- src (see above)
+-- subProjects (where projects are held)
| +-- reallyCoolProject1 (your first included project)
| \-- See project structure for a normal app
| \-- reallyCoolProject2 (your second included project)
| \-- See project structure for a normal app
+-- build.gradle
\-- settings.gradle
settings.gradle:
include ':subProjects:reallyCoolProject1'
include ':subProjects:reallyCoolProject2'
The last thing you should make sure of is the subProjects/reallyCoolProject1/build.gradle has apply plugin: "android-library"
instead of apply plugin: "android"
.
Like every Gradle project (and Maven) we now need to tell the root project about it's dependency. This can also include any normal Java dependencies that you want.
build.gradle
dependencies{
compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4'
compile project(":subProjects:reallyCoolProject1")
compile project(':subProjects:reallyCoolProject2')
}
I know this seems like a lot of steps, but they are pretty easy once you do it once or twice. This way will also allow you to build on a CI server assuming you have the Android SDK installed there.
NDK Side Note: If you are going to use the NDK you are going to need something like below. Example build.gradle file can be found here: https://gist.github.com/khernyo/4226923
build.gradle
task copyNativeLibs(type: Copy) {
from fileTree(dir: 'libs', include: '**/*.so' ) into 'build/native-libs'
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File('build/native-libs')
}
Sources:
- http://tools.android.com/tech-docs/new-build-system/user-guide
- https://gist.github.com/khernyo/4226923
- https://github.com/ethankhall/driving-time-tracker/
I just had a very similar issues with gradle builds / adding .jar library. I got it working by a combination of :
- Moving the libs folder up to the root of the project (same directory as 'src'), and adding the library to this folder in finder (using Mac OS X)
- In Android Studio, Right-clicking on the folder to add as library
- Editing the dependencies in the build.gradle file, adding
compile fileTree(dir: 'libs', include: '*.jar')}
BUT more importantly and annoyingly, only hours after I get it working, Android Studio have just released 0.3.7, which claims to have solved a lot of gradle issues such as adding .jar libraries
http://tools.android.com/recent
Hope this helps people!
Here is my solution for mac users I think it work for window also:
First go to your Android Studio toolbar
Build > Make Project (while you guys are online let it to download the files) and then
Build > Compile Module "your app name is shown here" (still online let the files are
download and finish) and then
Run your app that is done it will launch your emulator and configure it then run it!
That is it!!! Happy Coding guys!!!!!!!
Gradle Build Tools 2.2.0+ - Everything just works
This is the correct way to do it
In trying to avoid experimental and frankly fed up with the NDK and all its hackery I am happy that 2.2.x of the Gradle Build Tools came out and now it just works. The key is the externalNativeBuild
and pointing ndkBuild
path argument at an Android.mk
or change ndkBuild
to cmake
and point the path argument at a CMakeLists.txt
build script.
android {
compileSdkVersion 19
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 19
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'x86'
}
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
}
externalNativeBuild {
cmake {
path 'src/main/jni/CMakeLists.txt'
}
//ndkBuild {
// path 'src/main/jni/Android.mk'
//}
}
}
For much more detail check Google's page on adding native code.
After this is setup correctly you can ./gradlew installDebug
and off you go. You will also need to be aware that the NDK is moving to clang since gcc is now deprecated in the Android NDK.
'IT' 카테고리의 다른 글
Chrome에서 개발자 모드 확장 프로그램 팝업 사용 중지 (0) | 2020.06.18 |
---|---|
스택 샘플링을 넘어서 : C ++ 프로파일 러 (0) | 2020.06.18 |
2012 년 허드슨 vs 젠킨스 (0) | 2020.06.18 |
Express 4.x에서“./bin/www”는 무엇입니까? (0) | 2020.06.18 |
메타 태그 안에 속성 property =“og : title”이 무엇입니까? (0) | 2020.06.18 |