HybridApp环境搭建(cordova)

HybridApp环境搭建

文章目录

  • HybridApp环境搭建
    • 准备
      • cordova
      • Java环境
      • android环境
      • gradle环境
    • 构建项目
    • 问题

准备

  • cordova
  • java环境
  • android 环境
  • gradle环境
  • 构建项目,解决问题

cordova

npm的话只需执行下面这条命令即可,如果没有npm去官网下载即可
npm install -g cordova

我用的版本是9.0.0

Java环境

在这里插入代码片java环境用的是java1.8版本的jdk,在网上下载即可,也可在官网下载最新的jdk

配置到环境变量里,根据自己安装的路径配置
我这里的是JAVA_HOME = C:\Program Files\Java\jdk1.8.0_181
然后在path里添加%JAVA_HOME%\bin
打开cmd,输入java -version,如果出现版本就是配置成功了

android环境

下载android studio,让软件来帮我们下载环境

  • cordova打包需要安装gradle
  • 需添加两个环境变量,还是根据你的位置来决定,这里是我配置的
    • ANDROID_HOME="C:\Users\你的用户名\AppData\Local\Android\Sdk"
    • ANDROID_SDK_ROOT="C:\Users\你的用户名\AppData\Local\Android\Sdk"
    • 然后在path里添加两条
      • %ANDROID_HOME%\tools
      • %ANDROID_HOME%\platform-tools

ps: 我还不知道ANDROID_SDK_ROOT是干嘛的

注意:配置完环境变量后,在从cmd输入android出现这个即可
HybridApp环境搭建(cordova)_第1张图片

gradle环境

在官网下载,我这里下载到的是6.1.1版本的

构建项目

  1. 选择地方,打开cmd,输入
    cordova create 创建项目, 然后cd 项目名称
  2. 添加平台
    cordova platform add
  3. 手机连上数据线,开发者选项–打开USB调试
  4. 运行项目cordova run android

出现问题,报错信息:

> Could not resolve all artifacts for configuration ':app:classpath'.
> Could not resolve org.glassfish.jaxb:jaxb-runtime:2.2.11.
Required by:
project :app > com.android.tools.build:gradle:3.3.0 > com.android.tools.build:builder:3.3.0 > com.android.tools:sdklib:26.3.0 > com.android.tools:repository:26.3.0
> Could not resolve org.glassfish.jaxb:jaxb-runtime:2.2.11.
> Could not parse POM https://repo.maven.apache.org/maven2/org/glassfish/jaxb/jaxb-runtime/2.2.11/jaxb-runtime-2.2.11.pom
> Could not resolve com.sun.xml.bind.mvn:jaxb-runtime-parent:2.2.11.
> Could not resolve com.sun.xml.bind.mvn:jaxb-runtime-parent:2.2.11.
> Could not parse POM https://repo.maven.apache.org/maven2/com/sun/xml/bind/mvn/jaxb-runtime-parent/2.2.11/jaxb-runtime-parent-2.2.11.pom
> Could not resolve com.sun.xml.bind.mvn:jaxb-parent:2.2.11.
> Could not resolve com.sun.xml.bind.mvn:jaxb-parent:2.2.11.
> Could not parse POM https://repo.maven.apache.org/maven2/com/sun/xml/bind/mvn/jaxb-parent/2.2.11/jaxb-parent-2.2.11.pom
> Could not resolve com.sun.xml.bind:jaxb-bom-ext:2.2.11.
> Could not resolve com.sun.xml.bind:jaxb-bom-ext:2.2.11.
> Could not parse POM https://repo.maven.apache.org/maven2/com/sun/xml/bind/jaxb-bom-ext/2.2.11/jaxb-bom-ext-2.2.11.pom
> Could not resolve org.glassfish.jaxb:jaxb-bom:2.2.11.
> Could not resolve org.glassfish.jaxb:jaxb-bom:2.2.11.
> Could not parse POM https://repo.maven.apache.org/maven2/org/glassfish/jaxb/jaxb-bom/2.2.11/jaxb-bom-2.2.11.pom
> Could not resolve net.java:jvnet-parent:4.
> Could not resolve net.java:jvnet-parent:4.
> Could not get resource 'https://repo.maven.apache.org/maven2/net/java/jvnet-parent/4/jvnet-parent-4.pom'.
> Could not HEAD 'https://repo.maven.apache.org/maven2/net/java/jvnet-parent/4/jvnet-parent-4.pom'.
> Read timed out

如果默认安装的应该是在这路劲C:\Users\用户名\AppData\Local\Android\Sdk\tools\bin
在此处打开cmd窗口输入sdkmanager.bat --licenses同意协议,一直y同意即可

再次cordova build
还是有问题,图片太长

Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=C:\Users\Air\AppData\Local\Android\Sdk (recommended setting)
ANDROID_HOME=C:\Users\Air\AppData\Local\Android\Sdk (DEPRECATED)
Subproject Path: CordovaLib
Subproject Path: app

> Configure project :app
File C:\Users\Air\.android\repositories.cfg could not be loaded.
Checking the license for package Android SDK Platform 28 in C:\Users\Air\AppData\Local\Android\Sdk\licenses
License for package Android SDK Platform 28 accepted.
Preparing "Install Android SDK Platform 28 (revision: 6)".
"Install Android SDK Platform 28 (revision: 6)" ready.
Installing Android SDK Platform 28 in C:\Users\Air\AppData\Local\Android\Sdk\platforms\android-28
"Install Android SDK Platform 28 (revision: 6)" complete.
"Install Android SDK Platform 28 (revision: 6)" finished.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':CordovaLib'.
> Could not resolve all artifacts for configuration ':CordovaLib:classpath'.
   > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
     Required by:
         project :CordovaLib
      > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
         > Could not get resource 'https://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/android-maven-gradle-plugin-2.1.pom'.
            > Could not GET 'https://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/android-maven-gradle-plugin-2.1.pom'.
               > Read timed out
   > Could not resolve org.codehaus.groovy.modules.http-builder:http-builder:0.7.2.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error
   > Could not resolve org.apache.maven:maven-ant-tasks:2.1.3.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 33s
E:\Exercise\cordova\myapp\platforms\android\gradlew: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':CordovaLib'.
> Could not resolve all artifacts for configuration ':CordovaLib:classpath'.
   > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
     Required by:
         project :CordovaLib
      > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
         > Could not get resource 'https://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/android-maven-gradle-plugin-2.1.pom'.
            > Could not GET 'https://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/android-maven-gradle-plugin-2.1.pom'.
               > Read timed out
   > Could not resolve org.codehaus.groovy.modules.http-builder:http-builder:0.7.2.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error
   > Could not resolve org.apache.maven:maven-ant-tasks:2.1.3.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 33s

解决方法:在C:\Users\你的用户名.android目录下创建一个repositories.cfg,创建即可文件是空的
再次执行cordova build

Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=C:\Users\Air\AppData\Local\Android\Sdk (recommended setting)
ANDROID_HOME=C:\Users\Air\AppData\Local\Android\Sdk (DEPRECATED)
Subproject Path: CordovaLib
Subproject Path: app
> Task :app:preBuild UP-TO-DATE
> Task :CordovaLib:preBuild UP-TO-DATE
> Task :CordovaLib:preDebugBuild UP-TO-DATE
> Task :CordovaLib:checkDebugManifest UP-TO-DATE
> Task :CordovaLib:processDebugManifest UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :CordovaLib:compileDebugAidl NO-SOURCE
> Task :app:compileDebugAidl NO-SOURCE
> Task :CordovaLib:packageDebugRenderscript NO-SOURCE
> Task :app:compileDebugRenderscript UP-TO-DATE
> Task :app:checkDebugManifest UP-TO-DATE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:prepareLintJar UP-TO-DATE
> Task :app:generateDebugSources UP-TO-DATE
> Task :CordovaLib:compileDebugRenderscript UP-TO-DATE
> Task :CordovaLib:generateDebugBuildConfig UP-TO-DATE
> Task :CordovaLib:generateDebugResValues UP-TO-DATE
> Task :CordovaLib:generateDebugResources UP-TO-DATE
> Task :CordovaLib:packageDebugResources UP-TO-DATE
> Task :CordovaLib:generateDebugRFile UP-TO-DATE
> Task :CordovaLib:prepareLintJar UP-TO-DATE
> Task :CordovaLib:generateDebugSources UP-TO-DATE
> Task :CordovaLib:javaPreCompileDebug UP-TO-DATE
> Task :CordovaLib:compileDebugJavaWithJavac UP-TO-DATE
> Task :CordovaLib:processDebugJavaRes NO-SOURCE
> Task :CordovaLib:transformClassesAndResourcesWithPrepareIntermediateJarsForDebug UP-TO-DATE
> Task :app:javaPreCompileDebug UP-TO-DATE
> Task :app:mainApkListPersistenceDebug UP-TO-DATE
> Task :app:generateDebugResValues UP-TO-DATE
> Task :app:generateDebugResources UP-TO-DATE
> Task :app:mergeDebugResources UP-TO-DATE
> Task :app:createDebugCompatibleScreenManifests UP-TO-DATE
> Task :app:processDebugManifest UP-TO-DATE
> Task :app:processDebugResources UP-TO-DATE
> Task :app:compileDebugJavaWithJavac UP-TO-DATE
> Task :app:compileDebugNdk NO-SOURCE
> Task :app:compileDebugSources UP-TO-DATE
> Task :app:mergeDebugShaders UP-TO-DATE
> Task :app:compileDebugShaders UP-TO-DATE
> Task :app:generateDebugAssets UP-TO-DATE
> Task :CordovaLib:mergeDebugShaders UP-TO-DATE
> Task :CordovaLib:compileDebugShaders UP-TO-DATE
> Task :CordovaLib:generateDebugAssets UP-TO-DATE
> Task :CordovaLib:packageDebugAssets UP-TO-DATE
> Task :app:mergeDebugAssets UP-TO-DATE
> Task :app:validateSigningDebug UP-TO-DATE
> Task :app:signingConfigWriterDebug UP-TO-DATE
> Task :app:transformClassesWithDexBuilderForDebug UP-TO-DATE
> Task :app:transformDexArchiveWithExternalLibsDexMergerForDebug UP-TO-DATE
> Task :app:transformDexArchiveWithDexMergerForDebug UP-TO-DATE
> Task :app:mergeDebugJniLibFolders UP-TO-DATE
> Task :CordovaLib:compileDebugNdk NO-SOURCE
> Task :CordovaLib:mergeDebugJniLibFolders UP-TO-DATE
> Task :CordovaLib:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
> Task :CordovaLib:transformNativeLibsWithIntermediateJniLibsForDebug UP-TO-DATE
> Task :app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
> Task :app:processDebugJavaRes NO-SOURCE
> Task :app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
> Task :app:packageDebug UP-TO-DATE
> Task :app:assembleDebug UP-TO-DATE
> Task :app:cdvBuildDebug UP-TO-DATE

BUILD SUCCESSFUL in 1s
42 actionable tasks: 42 up-to-date
Built the following apk(s):
        E:\Exercise\cordova\myapp\platforms\android\app\build\outputs\apk\debug\app-debug.apk

已经成功创建apk文件

cordova build ,创建apk文件
cordova run android,用数据线连上手机,打开开发者模式中的usb安装和usb调试即可

问题

在今天打包时,又弹出错误,具体内容是:

* What went wrong:
A problem occurred configuring project ':CordovaLib'.
> Could not resolve all artifacts for configuration ':CordovaLib:classpath'.
   > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
     Required by:
         project :CordovaLib
      > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
         > Could not get resource 'https://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/android-maven-gradle-plugin-2.1.pom'.
            > Read timed out
   > Could not resolve org.codehaus.groovy.modules.http-builder:http-builder:0.7.2.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error
   > Could not resolve org.apache.maven:maven-ant-tasks:2.1.3.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 24s
E:\Exercise\cordova\MyApp\platforms\android\gradlew: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':CordovaLib'.
> Could not resolve all artifacts for configuration ':CordovaLib:classpath'.
   > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
     Required by:
         project :CordovaLib
      > Could not resolve com.github.dcendents:android-maven-gradle-plugin:2.1.
         > Could not get resource 'https://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/android-maven-gradle-plugin-2.1.pom'.
            > Read timed out
   > Could not resolve org.codehaus.groovy.modules.http-builder:http-builder:0.7.2.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error
   > Could not resolve org.apache.maven:maven-ant-tasks:2.1.3.
     Required by:
         project :CordovaLib > com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3
      > Skipped due to earlier error

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 24s

解决方法:
输入cordova clean,在进行打包,具体原理也不是很清楚,有知道的朋友可以评论一下

你可能感兴趣的:(javascript)