最近在网上搜索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,软件仅做个人学习使用,严谨用于商业用途。
另:由于个人时间有限,部分功能还在完善中,敬请期待