赞
踩
Android Studio版本
Eclipse项目工程:一个主工程,一个Emojicon依赖库。
有两种方式导入Eclipse工程:
1、兼容Eclipse
2、全新的Android Gradle Project
Google推荐用全新的方式,所以此处不介绍兼容模式导入。
请先关闭Android Studio当前的Project,否则你看不到Import Project。
选择“Import project(Eclipse ADT, Gradle, etc.)”,定位到你所需要导入的工程的主目录。由于主目录和Emojicon有依赖关系(Eclipse中需先将两个工程设置关联,即设置加载好依赖库后关闭eclipse),Android Studio会自动关联进来。然后指定一个保存的目录(注:工程文件会直接生成在这目录下,所以最好文件夹以项目名称命名,同时为空文件夹),点击Next。
默认情况下,下面的选项会打上勾(如果没有勾上,手动勾上),直接Finish就行了。
一段时间转换后,你将看到以下的结构
我把主工程的名称隐藏了,此时可以看到,Emojicon确实导进来了。不过Emojicon下的java目录是我后面加进来的,默认导入时这个文件夹不存在,为什么?往下看。
Android Studio导入完毕后,会默认在窗口中打开这个import-summary.txt文件,里面记录了导入的一些注意事项。这个文件有几个需要注意的节点,如下:
Manifest Merging
Ignored Files
Replaced Jars with Dependencies
Manifest Merging中的文字如下,也就是描述你的清单文件,基本上不用管。
- Your project uses libraries that provide manifests, and your Eclipse
- project did not explicitly turn on manifest merging. In Android Gradle
- projects, manifests are always merged (meaning that contents from your
- libraries' manifests will be merged into the app manifest. If you had
- manually copied contents from library manifests into your app manifest
- you may need to remove these for the app to build correctly.
Ignored Files,这个部分很重要。它表示Android Studio在导入项目的时候把这些文件忽略了,如果需要的话,你得手动拷贝进来。因为主工程依赖Emojicon,所以把这个文件拷贝到相应的位置即可。
The following files were *not* copied into the new Gradle project; you should evaluate whether these are still needed in your project and if so manually move them: From Emojicon: * .gitignore * java\ * java\com\ * java\com\rockerhieu\ * java\com\rockerhieu\emojicon\ * java\com\rockerhieu\emojicon\EmojiAdapter.java * java\com\rockerhieu\emojicon\EmojiconEditText.java * java\com\rockerhieu\emojicon\EmojiconGridFragment.java * java\com\rockerhieu\emojicon\EmojiconHandler.java * java\com\rockerhieu\emojicon\EmojiconSpan.java * java\com\rockerhieu\emojicon\EmojiconTextView.java * java\com\rockerhieu\emojicon\EmojiconsFragment.java * java\com\rockerhieu\emojicon\emoji\ * java\com\rockerhieu\emojicon\emoji\Emojicon.java * java\com\rockerhieu\emojicon\emoji\Nature.java * java\com\rockerhieu\emojicon\emoji\Objects.java * java\com\rockerhieu\emojicon\emoji\People.java * java\com\rockerhieu\emojicon\emoji\Places.java * java\com\rockerhieu\emojicon\emoji\Symbols.java
Replaced Jars with Dependencies,提示你原来的jar包在Android Studio项目中会以何种方式存在。
- The importer recognized the following .jar files as third party
- libraries and replaced them with Gradle dependencies instead. This has
- the advantage that more explicit version information is known, and the
- libraries can be updated automatically. However, it is possible that
- the .jar file in your project was of an older version than the
- dependency we picked, which could render the project not compileable.
- You can disable the jar replacement in the import wizard and try again:
-
- android-support-v4.jar => com.android.support:support-v4:22.2.1
导入后项目报错:Gradle sync failed: Could not find com.android.tools.build:gradle:3.2.1.
原因是com.android.tools.build:gradle:3.0.1插件需要到google的maven仓库下载。
在build.gradle添加google的maven仓库,如下:
- buildscript {
- repositories {
- jcenter()
- google()
- maven { url "https://jitpack.io" }
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:3.2.1'
- }
- }
-
- allprojects {
- repositories {
- jcenter()
- google()
- maven { url "https://jitpack.io" }
- }
- }
同时修改项目中build.gradle的compile改为implementation,原先定义在清单文件manifest file中的应用版本号、sdk相关版本号移到build.gradle中。
期间遇到:Unable to resolve dependency for ':signSDK@debug/compileClasspath': Could not resolve project :mySDK.
……
网络上大体介绍这样解决:https://blog.csdn.net/qq_35427437/article/details/79835673 ,不过我自己在C盘没有找到那个多余的文件,更新了sdk依然没解决问题,重新clean项目后,Android Studio报错: - debugApiElements - debugRuntimeElements - releaseApiElements - releaseRun,查找到了该文章https://blog.csdn.net/androidzmm/article/details/80707434 ,受此启发看了链接库的apply发现问题所在:库工程没有修改为library,修改后解决。
//apply plugin: 'com.android.application' apply plugin: 'com.android.library'
一般会遇到编码问题,例如报“错误: 编码UTF-8的不可映射字符”,因为gradle是使用UTF-8,androidstudio默认GBK,所以中文可能会乱码,网络查找很多编译gradle提示的乱码处理都无法解决,我是这样处理:
1、在项目的build.gradle加
- compileOptions {
- encoding "utf-8"
- }
2、
3、工程文件右下角设置utf-8
4、拷贝原在eclipse中的文件复制黏贴到as的对应类中
在module中导入了***.jar或者第三方远程依赖库,在app中如果想引用这个jar里相关的内容时,发现根本引用不到,会提示add library/*****.jar 一大串,你回车一下或者点一下没反应,根本没有用。
只需把module下build.gradle里相关jar的implementation改为api即可,因为implementation 修饰的只能在当前module中引用,在app下是引用不了的,改完重新编译下即可进行引用了。
以前的libs文件夹哪里去了呢?
右击你的Module,Open Module Settings,显示如下:
其实Android Studio可以添加在线的库和jar,看到Maven Central字样了吧?相应的Jar会按需下载。
至此,项目可以build了。想运行成功,还没那么容易。
Error:duplicate files during packaging of APK ........
说是duplicate files copied in APK META-INF/NOTICE.txt
duplicate files copied in APK META-INF/LICENSE.txt
解决办法:在你的module下的build.gradle中的android节点中添加packagingOptions
- android {
- ……
-
- packagingOptions{
- exclude 'META-INF/NOTICE.txt'
- exclude 'META-INF/LICENSE.txt'
- }
- }
还有一个错误提示:Manifest merger failed with multiple errors, see logs
Suggestion: add ‘tools:replace=”android:icon”‘ to <application> element at AndroidManifest.xml:24:5 to override
原因:Android Studio默认使用Manifest Merger Tool,如果Library项目也定义了与主项目相同的属性(如默认生成的android:icon, android:theme),则会合并失败。
解决1:在AndroidManifest.xml节点manifest中引入
xmlns:tools="http://schemas.android.com/tools"
然后在application节点添加
tools:replace="android:icon"
或需要屏蔽theme,中间用逗号隔开。
tools:replace=”android:icon, android:theme”
解决2:在build.gradle根标签上加上useOldManifestMerger true,不过在Android Studio 2.0中提示找不到这个useOldManifestMerger ,不知是改进过了还是用了其它的字段(此法有问题,有谁知道为什么么?)。
- android {
- useOldManifestMerger true
- }
工程的编译版本需改为一致,清单文件中权限和文件声明不能重复,应用的图片标签(重复则配置
tools:replace="android:icon, android:label")
,例如我工程的清单文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.peihp.happy"
- android:installLocation="preferExternal"
- xmlns:tools="http://schemas.android.com/tools"
- >
- <!--android:sharedUserId="android.uid.system"-->
- <!-- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="24" />-->
-
-
- <!-- 在内置SDCard中创建与删除文件权限 -->
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <!-- 在外置SDCard中读写文件权限 -->
- <uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />
- <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
- <!-- 往SDCard写入数据权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <!-- 清除缓存 -->
- <uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-permission android:name="android.permission.VIBRATE" />
-
- …………
-
- <application
- android:name="com.uuzuche.lib_zxing.MyApplication"
- android:allowClearUserData="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/sec_reader"
- tools:replace="android:icon, android:label"
- android:largeHeap="true" >
- …………
- </application>
-
- <supports-screens
- android:anyDensity="true"
- android:largeScreens="true"
- android:normalScreens="true" />
-
- </manifest>
链接库的清单文件:
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.peihp.facelivedetectionsilentlib"
- >
-
- <!--<uses-sdk android:minSdkVersion="11" />-->
-
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.INTERNET" />
-
- <uses-feature
- android:name="android.hardware.camera"
- android:required="true" />
- <uses-feature
- android:name="android.hardware.camera.autofocus"
- android:required="false" />
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- ……
- </application>
-
- </manifest>
local.properties文件:
- #Fri Nov 30 15:37:00 CST 2018
- sdk.dir=E\:\\php\\androidTool\\sdk24.4_as\\sdk24.4
-
- keystore.path=E\:/php/androidTool/packSign/peihp.keystore
- keystore.password=peihp66
- keystore.alias=peihp8888
- keystore.alias_password=peihp66
工程build.gradle文件:
- apply plugin: 'com.android.application'
-
- def keystoreFilepath = ''
- def keystorePSW = ''
- def keystoreAlias = ''
- def keystoreAliasPSW = ''
- // default keystore file, PLZ config file path in local.properties
- def keyfile = file('s.keystore.temp')
-
- Properties properties = new Properties()
- // local.properties file in the root director
- properties.load(project.rootProject.file('local.properties').newDataInputStream())
- keystoreFilepath = properties.getProperty("keystore.path")
- if (keystoreFilepath) {
- keystorePSW = properties.getProperty("keystore.password")
- keystoreAlias = properties.getProperty("keystore.alias")
- keystoreAliasPSW = properties.getProperty("keystore.alias_password")
- keyfile = file(keystoreFilepath)
- }
-
-
- android {
- signingConfigs {
- config {
- keyAlias keystoreAlias
- keyPassword keystoreAliasPSW
- storeFile keyfile
- storePassword keystorePSW
- }
- }
-
- compileSdkVersion 27
- buildToolsVersion "28.0.3"
- defaultConfig {
- applicationId "com.peihp.happy"
- minSdkVersion 14
- targetSdkVersion 27
- versionCode 21
- versionName "1.2.2"
- compileOptions {
- sourceCompatibility JavaVersion.VERSION_1_7
- targetCompatibility JavaVersion.VERSION_1_7
- }
- }
- buildTypes {
- release {
- minifyEnabled true
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- debuggable false
- signingConfig signingConfigs.config
- }
- debug {
- signingConfig signingConfigs.config
- }
- }
- compileOptions {
- encoding "utf-8"
- }
- packagingOptions {
- exclude 'META-INF/DEPENDENCIES'
- }
- lintOptions {
- checkReleaseBuilds false
- abortOnError false
- }
- }
-
- dependencies {
- implementation project(':faceLiveDetectionSilentLib')
- implementation 'com.android.support:support-v4:27.1.1'
- implementation files('libs/commons-codec-1.3.jar')
- implementation files('libs/commons-collections-3.2.jar')
- implementation files('libs/commons-httpclient-3.1.jar')
- implementation files('libs/commons-logging-1.1.jar')
- implementation files('libs/commons-pool-1.2.jar')
- implementation files('libs/httpclient-4.3.6.jar')
- implementation files('libs/httpcore-4.3.2.jar')
- implementation files('libs/httpmime-4.3.5.jar')
- implementation files('libs/iTextpdf.jar')
- implementation files('libs/jedis-2.1.0.jar')
- implementation files('libs/ksoap2-android-assembly-3.6.0-jar-with-dependencies.jar')
- ……
- implementation files('libs/xUtils-2.6.14.jar')
- implementation files('libs/zxing.jar')
- }
链接库的build.gradle文件:
- apply plugin: 'com.android.library'
-
- android {
- compileSdkVersion 27
- buildToolsVersion "28.0.3"
- defaultConfig {
- minSdkVersion 11
- targetSdkVersion 27
- versionCode 1
- versionName "1.0"
- }
- buildTypes {
- release {
- minifyEnabled true
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- packagingOptions {
- exclude 'META-INF/DEPENDENCIES'
- exclude 'META-INF/NOTICE'
- exclude 'META-INF/LICENSE'
- exclude 'META-INF/LICENSE.txt'
- exclude 'META-INF/NOTICE.txt'
- }
- }
-
- dependencies {
- implementation 'com.android.support:support-v4:27.1.1'
- implementation files('libs/liveness-silent-offline-release.jar')
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。