如何使用 Gradle 构建系统让 PhoneGap 项目在 Android Studio 中运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21031135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get a PhoneGap Project to Run in Android Studio with Gradle Build System
提问by Matt Whetton
I'm trying to get a new PhoneGap application setup and running inside Android Studio with the Gradle build system.
我正在尝试使用 Gradle 构建系统设置一个新的 PhoneGap 应用程序并在 Android Studio 中运行。
At the moment I have successfully created the PhoneGap project and imported into Android Studio. It all appears to be working fine, but I cant work out how to move it to the Gradle build system, or even if its possible.
目前我已经成功创建了PhoneGap项目并导入到Android Studio中。一切似乎都运行良好,但我无法弄清楚如何将其移动到 Gradle 构建系统,或者即使可能。
Can anybody help?
有人可以帮忙吗?
采纳答案by springogeek
I managed to do this.
我设法做到了这一点。
You need Android Studio and the Eclipse ADT version, as well as Cordova/PhoneGap all set up.
您需要 Android Studio 和 Eclipse ADT 版本,以及 Cordova/PhoneGap 全部设置。
- Import the Cordova project into Eclipse.
- Go to File -> Export... -> Generate Gradle Build Files.
- Click next to get past the "Import Instead?" screen.
Select both your Android project and the CordovaLib project to export and click Next.
Once this completes, open Android Studio.
- Go to File -> Import Project...
- Select the build.gradle file for the main Android project, which was generated by Eclipse, and click OK.
After the import, you may get some warnings about a newer gradle version in use, just check your settings and it seems to work itself out.
At this point, you should have a project structure that is your main project, but with CordovaLib as a module.
- 将 Cordova 项目导入 Eclipse。
- 转到文件 -> 导出... -> 生成 Gradle 构建文件。
- 单击下一步跳过“改为导入?” 屏幕。
选择要导出的 Android 项目和 CordovaLib 项目,然后单击下一步。
完成后,打开 Android Studio。
- 转到文件-> 导入项目...
- 选择由 Eclipse 生成的主 Android 项目的 build.gradle 文件,然后单击 OK。
导入后,您可能会收到一些有关正在使用的较新 gradle 版本的警告,只需检查您的设置,它似乎会自行解决。
此时,您应该有一个作为主项目的项目结构,但使用 CordovaLib 作为模块。
Now you can open the build.gradle file in the main project directory and change it to this:
现在你可以打开主项目目录中的 build.gradle 文件并将其更改为:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
}
}
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':CordovaLib')
compile 'com.android.support:appcompat-v7:19.+'
}
You should now be able to convince Android Studio to compile.
您现在应该能够说服 Android Studio 进行编译。
An extra tip would be to create a script to run "cordova prepare" and add that to the module's run configuration as an external tool. Make sureto synchronise the whole project before deploying the APK to a device or emulator.
一个额外的技巧是创建一个脚本来运行“cordova prepare”,并将其作为外部工具添加到模块的运行配置中。在将 APK 部署到设备或模拟器之前,请确保同步整个项目。
回答by ReedAccess
I am new to Android Studio and still getting used to the AS project structure and Gradle.
我是 Android Studio 的新手,仍然习惯了 AS 项目结构和 Gradle。
Using Cordova 4.1.2 Android Studio 1.0.1
使用 Cordova 4.1.2 Android Studio 1.0.1
1) I created the app using the Cordova CLI:
1) 我使用 Cordova CLI 创建了应用程序:
cordova create CordovaAndroidApp
cd CordovaAndroidApp
cordova platform add android
cordova 创建 CordovaAndroidApp
cd CordovaAndroidApp
科尔多瓦平台添加android
this version of Cordova created the build.gradle and settings.gradle files.
这个版本的 Cordova 创建了 build.gradle 和 settings.gradle 文件。
2) From Android Studio splash screen I selected "Start a new Android Studio project" On the second screen I checked the Phone and Tablet box; on the third screen, I chose "Add No Activity"
2) 在 Android Studio 启动画面中,我选择了“Start a new Android Studio project” 在第二个屏幕上,我选中了 Phone and Tablet 框;在第三个屏幕上,我选择了“不添加活动”
3) In this new Android Studio application, from the Project view in the left panel with the top level of the project selected, I selected File -> Import Project. In the popup "select Eclipse or Gradle Project to Import", I chose to the Cordova project directory, clicked down to the platforms / android directory, and selected the build.gradle file, then OK.
3) 在这个新的 Android Studio 应用程序中,从左侧面板的项目视图中选择项目的顶层,我选择了文件 -> 导入项目。在弹出的“select Eclipse or Gradle Project to Import”中,我选择了Cordova项目目录,向下点击到platforms/android目录,选择build.gradle文件,然后OK。
I was able to build and run the basic Cordova project (just the splash screen) with no problem.
我能够毫无问题地构建和运行基本的 Cordova 项目(只是启动画面)。