导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程

google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展。可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了。之前导4.0的camera那时不费吹灰之力就搞成了。下面的文字记录是我摸索了近十天终于导成功的记录,给大家分享了。
  一、开发环境的搭建
  1、 点击 http://developer.android.com/sdk/index.html 下载adt-bundle-windows-x86-20130522(426M)。尽管部分机子上有非adt-bundle搭建的开发环境,但考虑到google已不支持以往android sdk + adt + Eclipse的方式,改推adt-bundle的方式,故杂家以adt-bundle-windows-x86-20130522搭建的开发环境为基础。(注:这里默认JDK已经安装配置好了。)
  2、 点击 http://developer.android.com/sdk/installing/installing-adt.html  选择在线或离线方式升级ADT版本为22.0.5。上面的adt-bundle自带的ADT版本是22.0.1,不支持解析rs文件,会导致后面的工作功亏一篑,因此一定要升级。如果在线安装ADT失败,考虑将C:WindowsSystem32driversetchosts文件添加一行:74.125.237.1 dl-ssl.google.com 就可以正常下载安装了,包括后续的更新build-tools、SDK-tools、Platform-tools。具体参考前文
  [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found
  更新SDK-tools、Platform-tools、build-tools版本分别为22.0.5、18.0.1、18.0.1,
  二、导入前的准备
  1、 编译Android4.2的源码,目的是为了得到后面的四个classes文件,如果通过其他途径有这四个文件也可以不编译。4.2的源码编译需要在64位机子上。这四个文件稍后会上传到网上。
  2、 将F:AndroidSource4.2_r1packagesapps下的Gallery2文件夹和Camera文件夹复制到桌面一份,后续将先导入Gallery2再导入Camera。
  3、 在桌面新建一个Camera4.2Need文件夹,将/out/target/commom/obj/JAVA_LIBRARIES/core_intermediates/、/out/target/commom/obj/JAVA_LIBRARIES/framework_intermediates/、/out/target/commom/obj/JAVA_LIBRARIES/mp4parser_intermediates/、/out/target/commom/obj/JAVA_LIBRARIES/ xmp_toolkit_intermediates这四个路径下的classes.jar文件分别重命名为core_intermediates、framework_intermediates、mp4parser_intermediates、xmp_toolkit_intermediates放到Camera4.2Need文件夹,后续这个文件夹可以放到工作空间以免丢失。
  三、开始导入
  1、 打开eclipse,点击File---New----Project---Android---Android Project from Existing Code,选择桌面的那个Gallery2文件夹,test不要勾选,copy projects到工作空间要勾选。
  导入后会报错如下,不用管:

  2、 导入后首先检查gen文件夹是否解析了rs文件,正常解析的话会生成ScriptC_convolve3x3.java文件和convolve3x3.d,如果没有生成则说明当前开发环境无法解析rs文件(com.android.gallery3d.filtershow.filters包下的convolve3x3.rs文件),需要重新配置开发环境。
  3、 右击工程的属性或按alt+enter快捷键,选中Java Build Path,点击Source,选择Add Folder,将gallerycommon和src_pd勾选上。如果这两个文件夹不勾选,后续会找不到很多类和包,而这些缺失并不是缺少.classes文件导致的。如果提示重复,就按如下步骤进行.
  4、 将Android Dependenices文件夹下的那两个空包删除。找到源码Gallery2下的gallerycommon文件夹,需要将这里的文件按对应的包名,添加到gallerycommon文件夹下。新建gallerycommon文件夹,新建包名com.android.gallery3d.common;然后将gallerycommon文件夹下的java文件复制过去。其他的包类似。对源码里的src_pd也做同样的操作。【注:在java build path属性里,source目录下src_pc和gallerycommon这两个文件夹必须勾选,否则新建的包会自动跑到src目录下。】
  [注:这个过程比较灵活,只要最终将两个文件夹下的包导入进去即可,确保包名正确。]
  5、 按alt+enter快捷键,选中java build path,点Libraries---Add Library,选择User Library,并将下面的那个选项勾上,表示它的级别很高。New一个Library,名字为Camera4.2Need,将刚准备的四个.jar文件添加进来。
  6、 然后点Order and Export,将Camera4.2Need这个库的位置放在Android4.2.2的前面。
  7、 点击res文件夹,点开values文件夹,可以看到里面的filtershow_strings.xml、photoeditor_strings.xml、strings.xml有错误,原因是里面有重复定义。将报错的地方删掉即可!每删掉一个地方,对应的另一个文件报错的地方就会恢复正常。可以按照这种方法对values-xx开头的文件夹都做相同的处理,但事实上,由于我们只要简体中文,所以只保留values-zh-rCN文件夹即可,对此文件夹的报错也作上述处理。其他的values-xx文件夹统统删掉。
  8、 在导入camera文件之前需要逐一对照camera下的res文件夹和当前gallery工程下的res文件夹下的文件有无重名。如两者的values文件夹下都有atrrs.xml文件,需要将camera的那一份重命名为atrrs_camera.xml,否则会发生覆盖导致出错。导入时点击Gallery工程,注意一定是导到Gallery工程目录下。右击选择—import---General---File System,其中的.git文件夹及test文件夹、Android.mk、CleanSpec.mk不要选。
  9、 点击Clean,重新编译仍然会有错。WidgetTypeChooser.java报错,但实质上并不是这个文件出了问题。这也正是ADT-Bundle安装的eclipse没有自己下载的Eclipse好用的表现之一,当出现大量错误时,前者会乱报错。这时gen文件夹下com.android.gallery3d下的R文件已经生成。整个工程的错误在包com.android.camera.ui下的各个java文件中,这些java文件都导入了com.android.camera.R,但实质上和camera相关的资源ID都被整合到gen文件夹下的com.android.gallery3d.R文件中了。也即本来应该是camera的R文件整合到gallery的R文件中去了,com.android.camera.R这个文件根本没有生成。因此凡是import com.android.camera.R;有这句话的地方一定会报错,奇怪的是eclipse还显示不出来。用import com.android.gallery3d.R;来代替这句话即可。
  10、 在修改了com.android.camera.ui包下的各个java文件后,对com.android.camera包下的java文件也要进行修改,凡是引用了R的地方都会报错,但eclipse可能显示不出来。一定要保证com.android.camera.ui包下凡是引用R文件的地方都要导入Gallery3d.R包。
  11、 在编译的时候会将错误显示到WidgetTypeChooser.java中,并在下方输出错误为:The project was not built since its build path is incomplete. Cannot find the class file for android.view.CompatibilityInfoHolder. Fix thebuild path then try building this project GalleryUnknownJava Problem。即找不到android.view.CompatibilityInfoHolder,参考 http://124.16.139.131:24080/lxr/ ... ava?v=android-4.0.4 新建包android.view,然后将新建CompatibilityInfoHolder.java。[备注:在有的版本上并没有遇到这个问题,有可能是android4.2将这个类去除了。]
  java代码
  1. package android.view;

  2.   import android.content.res.CompatibilityInfo;
  3.   /** @hide */
  4.   public class CompatibilityInfoHolder {
  5.   private volatile CompatibilityInfo mCompatInfo = 
  6. CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
  7.   public void set(CompatibilityInfo compatInfo) {
  8.   if (compatInfo != null && (compatInfo.isScalingRequired()
  9.   || !compatInfo.supportsScreen())) {
  10.   mCompatInfo = compatInfo;
  11.   } else {
  12.   mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
  13.   }
  14.   }
  15.   public CompatibilityInfo get() {
  16.   return mCompatInfo;
  17.   }
  18.   public CompatibilityInfo getIfNeeded() {
  19.   CompatibilityInfo ci = mCompatInfo;
  20.   if (ci == null || ci == CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO) {
  21.   return null;
  22.   }
  23.   return ci;
  24.   }
  25.   }
复制代码
  12、 再次clean编译会报错:某个函数需要的api版本要大于11,当前最低版本为10.需修改AndroidManifest.xml文件,最低api版本为11,目标版本为17,因为我们的手机是4.2对应级别为17。
  13、 再次clean编译会报错:在ImageFilterSharpen.java函数里找不到forEach_root()函数。通过对源码编译的输出路径out进行搜索,在/out/target/common/obj/APPS/GALLERY2_intermediates/src/com/android/Gallery3d/filtershow/filters文件夹下找到ScriptC_convolve3x3.java一共可以找到3份同名的文件,只有这个路径下的代码有forEach_root(Allocation, Allocation)接口。将此文件复制出来一份,粘贴到com.android.gallery3d.filtershow.filters下,将名字改为MyScriptC_convolve3x3.java,否则会跟gen文件下的文件重名,并在类的构造函数等其他地方更改名字,包名也要改。
  java代码
  1. /*

  2.   * Copyright (C) 2011-2012 The Android Open Source Project
  3.   *
  4.   * Licensed under the Apache License, Version 2.0 (the "License");
  5.   * you may not use this file except in compliance with the License.
  6.   * You may obtain a copy of the License at
  7.   *
  8.   * [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
  9.   *
  10.   * Unless required by applicable law or agreed to in writing, software
  11.   * distributed under the License is distributed on an "AS IS" BASIS,
  12.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
  13. implied.
  14.   * See the License for the specific language governing permissions and
  15.   * limitations under the License.
  16.   */
  17.   /*
  18.   * This file is auto-generated. DO NOT MODIFY!
  19.   * The source Renderscript file: 
  20. packages/apps/Gallery2/src/com/ku/cameragal/filtershow/filters/convolve3x3.rs
  21.   */
  22.   package com.android.gallery3d.filtershow.filters;
  23.   import android.renderscript.*;
  24.   import android.content.res.Resources;
  25.   /**
  26.   * @hide
  27.   */
  28.   public class MyScriptC_convolve3x3 extends ScriptC {
  29.   private static final String __rs_resource_name = "convolve3x3";
  30.   // Constructor
  31.   public MyScriptC_convolve3x3(RenderScript rs) {
  32.   this(rs,
  33.   rs.getApplicationContext().getResources(),
  34.   rs.getApplicationContext().getResources().getIdentifier(
  35.   __rs_resource_name, "raw",
  36.   rs.getApplicationContext().getPackageName()));
  37.   }
  38.   public MyScriptC_convolve3x3(RenderScript rs, Resources resources, int id) 
  39. {
  40.   super(rs, resources, id);
  41.   __I32 = Element.I32(rs);
  42.   __ALLOCATION = Element.ALLOCATION(rs);
  43.   __F32 = Element.F32(rs);
  44.   __U8_4 = Element.U8_4(rs);
  45.   }
  46.   private Element __ALLOCATION;
  47.   private Element __F32;
  48.   private Element __I32;
  49.   private Element __U8_4;
  50.   private FieldPacker __rs_fp_ALLOCATION;
  51.   private FieldPacker __rs_fp_F32;
  52.   private FieldPacker __rs_fp_I32;
  53.   private final static int mExportVarIdx_gWidth = 0;
  54.   private int mExportVar_gWidth;
  55.   public synchronized void set_gWidth(int v) {
  56.   setVar(mExportVarIdx_gWidth, v);
  57.   mExportVar_gWidth = v;
  58.   }
  59.   public int get_gWidth() {
  60.   return mExportVar_gWidth;
  61.   }
  62.   public Script.FieldID getFieldID_gWidth() {
  63.   return createFieldID(mExportVarIdx_gWidth, null);
  64.   }
  65.   private final static int mExportVarIdx_gHeight = 1;
  66.   private int mExportVar_gHeight;
  67.   public synchronized void set_gHeight(int v) {
  68.   setVar(mExportVarIdx_gHeight, v);
  69.   mExportVar_gHeight = v;
  70.   }
  71.   public int get_gHeight() {
  72.   return mExportVar_gHeight;
  73.   }
  74.   public Script.FieldID getFieldID_gHeight() {
  75.   return createFieldID(mExportVarIdx_gHeight, null);
  76.   }
  77.   private final static int mExportVarIdx_gPixels = 2;
  78.   private Allocation mExportVar_gPixels;
  79.   public void bind_gPixels(Allocation v) {
  80.   mExportVar_gPixels = v;
  81.   if (v == null) bindAllocation(null, mExportVarIdx_gPixels);
  82.   else bindAllocation(v, mExportVarIdx_gPixels);
  83.   }
  84.   public Allocation get_gPixels() {
  85.   return mExportVar_gPixels;
  86.   }
  87.   private final static int mExportVarIdx_gIn = 3;
  88.   private Allocation mExportVar_gIn;
  89.   public synchronized void set_gIn(Allocation v) {
  90.   setVar(mExportVarIdx_gIn, v);
  91.   mExportVar_gIn = v;
  92.   }
  93.   public Allocation get_gIn() {
  94.   return mExportVar_gIn;
  95.   }
  96.   public Script.FieldID getFieldID_gIn() {
  97.   return createFieldID(mExportVarIdx_gIn, null);
  98.   }
  99.   private final static int mExportVarIdx_gCoeffs = 4;
  100.   private float[] mExportVar_gCoeffs;
  101.   public synchronized void set_gCoeffs(float[] v) {
  102.   mExportVar_gCoeffs = v;
  103.   FieldPacker fp = new FieldPacker(36);
  104.   for (int ct1 = 0; ct1 < 9; ct1++) {
  105.   fp.addF32(v[ct1]);
  106.   }
  107.   int []__dimArr = new int[1];
  108.   __dimArr[0] = 9;
  109.   setVar(mExportVarIdx_gCoeffs, fp, __F32, __dimArr);
  110.   }
  111.   public float[] get_gCoeffs() {
  112.   return mExportVar_gCoeffs;
  113.   }
  114.   public Script.FieldID getFieldID_gCoeffs() {
  115.   return createFieldID(mExportVarIdx_gCoeffs, null);
  116.   }
  117.   private final static int mExportForEachIdx_root = 0;
  118.   public Script.KernelID getKernelID_root() {
  119.   return createKernelID(mExportForEachIdx_root, 3, null, null);
  120.   }
  121.   public void forEach_root(Allocation ain, Allocation aout) {
  122.   // check ain
  123.   if (!ain.getType().getElement().isCompatible(__U8_4)) {
  124.   throw new RSRuntimeException("Type mismatch with U8_4!");
  125.   }
  126.   // check aout
  127.   if (!aout.getType().getElement().isCompatible(__U8_4)) {
  128.   throw new RSRuntimeException("Type mismatch with U8_4!");
  129.   }
  130.   // Verify dimensions
  131.   Type tIn = ain.getType();
  132.   Type tOut = aout.getType();
  133.   if ((tIn.getCount() != tOut.getCount()) ||
  134.   (tIn.getX() != tOut.getX()) ||
  135.   (tIn.getY() != tOut.getY()) ||
  136.   (tIn.getZ() != tOut.getZ()) ||
  137.   (tIn.hasFaces() != tOut.hasFaces()) ||
  138.   (tIn.hasMipmaps() != tOut.hasMipmaps())) {
  139.   throw new RSRuntimeException("Dimension mismatch between input and output 
  140. parameters!");
  141.   }
  142.   forEach(mExportForEachIdx_root, ain, aout, null);
  143.   }
  144.   }
复制代码

  14、 在ImageFilterSharpen.java中,将ScriptC_convolve3x3 mScript改为MyScriptC_convolve3x3 mScript,并在相应地方同步更改类名。

  经过上述步骤,Android4.2源码Gallery2、Camera模块成功导入Eclipse,并能正常编译,稍后会将编译出的apk共享出来。

from:http://bbs.9ria.com/thread-253056-1-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值