Android Studio 4.1.3 将工程作为模块导入另一个工程中 Github代码下载、查看阅读器

最近在网上搜索Github代码浏览App,发现没有自己满意的,很多App都只能在线看,且由于Github被墙的原因,体验经常特别差,于是就萌生了自己写一个App的想法,通过搜索发现

https://hub.fastgit.org/mingjunli/GithubApp可以展示Github上的热门Repo,方便进行个Repo及User之间的关联查看,且使用MVP架构,方便进行查看,但该App不能下载代码,只能在线查看,查看时代码返回不是按目录逐级返回,且使用的Trending源已不可用和Github源容易被墙。
https://hub.fastgit.org/maks/MGit可以进行Clone、Pull等丰富的操作,但是界面不够友好,需要手动输入repo地址,代码查看界面不友好,于是决定使用GithubApp为基础框架,将MGit整合进GithubApp中。
下面进入主题:
将一个工程MGit作为模块导入到另一个工程GithubApp中。

一、 导入工程MGit

选择File->New->Import Module
在这里插入图片描述

导入之后还不能自动检测为模块
在这里插入图片描述

模块标识还未显示。

二、 修改已导入工程MGit的AndroidManifest.xml

注销application节点下的启动标志

<!--            <intent-filter>-->
<!--                <action android:name="android.intent.action.MAIN" />-->

<!--                <category android:name="android.intent.category.LAUNCHER" />-->
<!--            </intent-filter>-->

三、 修改已导入工程MGit的的build.gradle

注销根节点下的apply plugin标志 及defaultConfig
标志下的applicationId

//apply plugin: 'com.android.application'
//applicationId "com.manichord.mgit"

在根节点下增加apply plugin: 'com.android.library'
然后sync一下。

//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion '29.0.3'


    defaultConfig {
//        applicationId "com.manichord.mgit"
        minSdkVersion 21
        targetSdkVersion 29

四、 导入工程

选择File->Project Structure导入模块
在这里插入图片描述
在这里插入图片描述

成功后显示模块标识
在这里插入图片描述

五、 修改工程GithubApp的AndroidManifest.xml

manifest 节点添加:xmlns:tools="http://schemas.android.com/tools"
application节点中增加

tools:replace="android:name,android:icon,android:authorities,android:theme"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.anly.githubapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".GithubApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_github"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:authorities="com.anly.githubapp.fileprovider"
        tools:replace="android:name,android:icon,android:authorities,android:theme">

否则回报如下错误

Attribute application@icon value=(@drawable/ic_github) from AndroidManifest.xml:13:9-43
	is also present at [:mgit] AndroidManifest.xml:19:9-43 value=(@mipmap/ic_launcher).
	Suggestion: add 'tools:replace="android:icon"' to <application> element at AndroidManifest.xml:10:5-92:19 to override.

将模块中的权限申请加入工程A中

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

六、修改资源文件styles.xml

模块MGit中的Layout有如下代码

<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:paddingBottom="4dp"
    android:background="?pager_title_strip_background"
    android:paddingTop="4dp"
    android:textColor="#fff" />

其中"?pager_title_strip_background"为预定义属性,需要将工程MGit中styles.xml内的预定义属性复制到工程GithubApp中的styles.xml内,否则模块MGit的Layout中使用预定义属性回出现错误。

<attr name="pager_title_strip_background" format="reference"/>
<attr name="bt_branch_name_bg" format="reference"/>
<attr name="bt_branch_name_text_color" format="reference"/>
<attr name="mgit_drawer_text_color"  format="reference" />
<attr name="mgit_drawer_background" format="reference" />
<attr name="ic_file_fl" format="reference" />
<attr name="ic_folder_fl" format="reference" />
<attr name="ic_branch_l" format="reference" />
<attr name="ic_tag_l" format="reference" />

在这里插入图片描述

七、 若两个工程的Activity Layout有重名需进行修改。

八、 修改Application类

工程MGit中使用kotlin建立了一个MGitApplication类,继承自Application

open class MGitApplication : Application()

工程GithubApp中建立了一个GithubApplication类,继承自MultiDexApplication

public class GithubApplication extends MultiDexApplication

当从GithubApp中启动模块MGit的Activity时,Application对象之间不能从GithubApplication转换到MGitApplication中。

为此将类继承进行修改
使MGit中MGitApplication继承自MultiDexApplication

open class MGitApplication : MultiDexApplication()

GithubApp中GithubApplication继承自MGitApplication

public class GithubApplication extends  MGitApplication

同时在MGit的build.gradle中引入

implementation 'com.android.support:multidex:1.0.1'

即可完成类型转换。

由于MGit采用MVVM结构,使用了DataBinding因此需在GithubApp的`app/build.gradle 中的android 下添加

dataBinding {
        enabled = true
    }

最后附上新App的一些截图及下载地址,方便大家使用。

https://download.csdn.net/download/qq_43522781/18250712

链接:https://pan.baidu.com/s/13C0DkT5Ui26mdmNuEjxr_g
提取码:ubca
复制这段内容后打开百度网盘手机App,操作更方便哦

声明:App主体来源于Github,软件仅做个人学习使用,严谨用于商业用途。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
另:由于个人时间有限,部分功能还在完善中,敬请期待

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值