0
点赞
收藏
分享

微信扫一扫

[Android] 命令行创建hello world项目

夏天的枫_ 2024-01-15 阅读 11

1.从官网下载cmdline-tools 然后本地配置环境变量ANDROID_HOME和PATH中添加cmdline-tools的bin目录。然后运行sdkmanager命令 会提示需要java版本61 。如果不够需要下载新版jdk ,我下了openjdk jdk17可以用。

2.sdkmanager --list 列出服务器上提供的包。安装命令是:

sdkmanager "包名;版本号" “报名;版本号” 

上述列表并未提供tools ,而是安装build-tools会附带安装tools 最新版。我这里platform只下载了23的

3.运行tools/android 命令,创建项目的默认骨架,create project ... 提示新版已经不允许了,你只能用Studio 所以我网上下载了一份老版的tools 和build-tools ,再次运行android create project ... 创建成功,但是基于ant 的,而非gradle的。网上有ant转gradle的操作博客我嫌麻烦,下载了一个基于gradle的hello world项目

4.把wrapper版本改新一点,如8 。在根目录gradlew help

app/build.gradle文件报错:Failed to apply plugin 'com.android.application'

于是按官网提供的gradle配置改了setting和根build.gradle文件

pluginManagement {

    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {

    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "Hello"
include ':app

 

plugins {

    id 'com.android.application' version '8.1.0' apply false
    id 'com.android.library' version '8.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.22' apply false
}

再次gradlew help 上述错误消失,下一个错误是:

Could not find method implememtation() for arguments [com.android.support.constraint:constraint-layout:1.0.2] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

因为app/build.gradle中有个依赖:

implememtation 'com.android.support.constraint:constraint-layout:1.0.0'

我查看sdk目录下,有下载这个约束,为什么报错呢?网友说用androidX约束

我改完确实管用。implementation "androidx.constraintlayout:constraintlayout:2.1.0"

5.再次运行gradlew help没错了,然后运行gradlew tasks看看,发现有个check ,于是gradlew check,警告:

WARNING: The specified Android SDK Build Tools version (23.0.1) is ignored, as it is below the minimum supported version (33.0.1) for Android Gradle Plugin 8.1.0.

原来是app/build.gradle中,我把buildToolVersion和minSdk改成23了,因为我sdk只下载的23 。下载提示中的33即可。

6.再次gradlew check 报错:

Configuration :app:debugRuntimeClasspath contains AndroidX dependencies, but the android.useAndroidX property is not enabled, which may cause runtime issues.

于是在gradle.properties文件中添加,之后运行报错:

WARNING: Your project has set android.useAndroidX=true, but configuration :app:debugRuntimeClasspath still contains legacy support libraries, which may cause runtime issues

原来,还需要追加一句,android.enableJetifier=true

(后面第1步会发现这里的一个错误)

7.再check警告:

Incorrect package="com.example.admin.helloworld" found in source AndroidManifest.xml: /home/bill/gradir/proj/HelloWorld/app/src/main/AndroidManifest.xml.

Setting the namespace via the package attribute in the source AndroidManifest.xml is no longer supported.

Recommendation: remove package="com.example.admin.helloworld" from the source AndroidManifest.xml: /home/bill/gradir/proj/HelloWorld/app/src/main/AndroidManifest.xml.

于是照做,删除即可。

8.再check报错:

Android resource linking failed
     error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found.
     error: resource android:style/TextAppearance.Material.Widget.Button.Colored not found.
     com.example.admin.app-mergeDebugResources-10:/values-v26/values-v26.xml:7: error: resource android:attr/colorError not found.
     com.example.admin.app-mergeDebugResources-10:/values-v26/values-v26.xml:7: error: resource android:attr/colorError not found.
     com.example.admin.app-mergeDebugResources-10:/values-v26/values-v26.xml:11: error: resource android:attr/colorError not found.
     com.example.admin.app-mergeDebugResources-10:/values-v26/values-v26.xml:11: error: resource android:attr/colorError not found.
     com.example.admin.app-mergeDebugResources-10:/values-v26/values-v26.xml:15: error: style attribute 'android:attr/keyboardNavigationCluster' not found.
     com.example.admin.app-mergeDebugResources-10:/values-v28/values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
     com.example.admin.app-mergeDebugResources-10:/values-v28/values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
     com.example.admin.app-mergeDebugResources-10:/values-v28/values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
     com.example.admin.app-mergeDebugResources-10:/values-v28/values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3125: error: resource android:attr/layout_marginHorizontal not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3126: error: resource android:attr/layout_marginVertical not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3540: error: resource android:attr/fontStyle not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3541: error: resource android:attr/font not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3542: error: resource android:attr/fontWeight not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3543: error: resource android:attr/fontVariationSettings not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3544: error: resource android:attr/ttcIndex not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3577: error: resource android:attr/startX not found.
     com.example.admin.app-mergeDebugResources-10:/values/values.xml:3579: error: resource android:attr/startY not found.

原来,前面我改了androidX约束,还需要androidX的appcompat

 implementation "androidx.constraintlayout:constraintlayout:1.4.1

(如果用1.3.1仍然报上面的错)再次check,报错:

Dependency 'androidx.appcompat:appcompat:1.4.1' requires libraries and applications that
           depend on it to compile against version 31 or later of the
           Android APIs.
     
           :app is currently compiled against android-23

因为我只下载了23的platform,于是我改build.gradle里面的compileSDK再次check,这时,gradle会调用sdk的命令,去下载31版本。

9.再次check,上述错误消失。下一个错误是:

MainActivity.java:3: 错误: 程序包android.support.v7.app不存在

( 这样,终于改到了java代码部分了)

这句也改成androidX的样子吧:

import androidx.core.app.ActivityCompat; (网上搜的博客)

再check报错:找不到AppCompatActivity 这说明上面导入的不对,又搜到一个:

import androidx.appcompat.app.AppCompatActivity; 再check这回对了。

这回报错:程序包R文件不存在,setContentView(R.layout.activity_main);

如果是在IDE中,直接ALT+ENTER。但是在命令行,我就手工import 包名.R

10.再check报错:程序包org.junit不存在,import org.junit.Test; (终于到了测试部分了 )这是因为前面build.gradle中有testCompile的依赖会报错,所以我给注释了。改成testImplementation 就行了(前者只在测试编译有效,后者在测试的编译和运行都有效)

11.再check这回是Lint工具报错:

Error: Google Play requires that apps target API level 31 or higher.
   [ExpiredTargetSdkVersion]
          targetSdkVersion 23
          ~~~~~~~~~~~~~~~~~~~
  
     Explanation for issues of type "ExpiredTargetSdkVersion":
     As of the second half of 2018, Google Play requires that new apps and app
     updates target API level 26 or higher.
  
     Configuring your app to target a recent API level ensures that users
     benefit from significant security and performance improvements, while still
     allowing your app to run on older Android versions (down to the
     minSdkVersion).

既然它说必须大于26,而且不影响在小于26的系统上安装,那我就改成26,虽然我的sdk只安装了23 ,再次check这回lint也不报错了。

12.check任务成功后,再试试checkJettifier 任务:

Project ':app' does not use any legacy support libraries. If this is the case for all other projects, you can disable Jetifier by setting android.enableJetifier=false in gradle.properties.

恍然大悟,前面第6步是理解错了。这里改为false后,check也正常了。第6步只是警告。

修改到此这个包可以作为新建项目的模板了。分享出来方便别人复用

不过我还想熟悉一下androidX之前的那一套如何在命令模式中创建....

举报

相关推荐

0 条评论